Answer the question
In order to leave comments, you need to log in
How to export RSA private key from Java as XML?
Hi all!
Help me out, already tormented. I was banned on Google - I can not find any answer.
In general, the question is this. In Java (Android) I generate a private-public key pair. In Android, everything goes fine - it encrypts and decrypts the strings as it should. But now I need to export this pair to .NET (C#). Public is exported as needed in the form
<RSAKeyValue><Modulus>AKfPOw...OZASeVM0qkuWp3</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
String xmlPublic = getPublicKeyAsXml(publicKey);
String xmlPrivate = getPrivateKeyAsXml(privateKey);
public String getPrivateKeyAsXml(PrivateKey privateKey) throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec ks = kf.getKeySpec(privateKey, RSAPrivateKeySpec.class);
BigInteger modulus = ks.getModulus();
BigInteger exponent = ks.getPrivateExponent();
byte[] modByte = modulus.toByteArray();
byte[] expByte = exponent.toByteArray();
String encodedModulus = Base64.encodeToString(modByte, Base64.NO_WRAP);
String encodedExponent = Base64.encodeToString(expByte, Base64.NO_WRAP);
String publicKeyAsXML = "<RSAKeyValue>" +
"<Modulus>" + encodedModulus + "</Modulus>" +
"<Exponent>AQAB</Exponent>" +
"<P></P>" +
"<Q></Q>" +
"<DP></DP>" +
"<DQ></DQ>" +
"<InverseQ></InverseQ>" +
"<D>" + encodedExponent + "</D>" +
"</RSAKeyValue>";
Log.d(TAG, "getPrivateKeyAsXml: " + publicKeyAsXML);
return publicKeyAsXML;
}
Answer the question
In order to leave comments, you need to log in
I forgot to write that I solved the problem
public String getPrivateKeyAsXml(PublicKey publicKey, PrivateKey privateKey) throws Exception
{
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKeySpec ksPublic = kf.getKeySpec(publicKey, RSAPublicKeySpec.class);
RSAPrivateKeySpec ksPrivate = kf.getKeySpec(privateKey, RSAPrivateKeySpec.class);
BigInteger n = ksPublic.getModulus();
BigInteger e = ksPublic.getPublicExponent();
BigInteger d = ksPrivate.getPrivateExponent();
BigInteger p = findFactor(e, d, n);
BigInteger q = n.divide(p);
if (p.compareTo(q) > 1) {
BigInteger t = p;
p = q;
q = t;
}
BigInteger dp = d.mod(p.subtract(BigInteger.ONE));
BigInteger dq = d.mod(q.subtract(BigInteger.ONE));
BigInteger inverseQ = q.modInverse(p);
String publicKeyAsXML = "<RSAKeyValue>" +
"<Modulus>" + convertToString(n) + "</Modulus>" +
"<Exponent>" + convertToString(e) + "</Exponent>" +
"<P>" + convertToString(p) + "</P>" +
"<Q>" + convertToString(q) + "</Q>" +
"<DP>" + convertToString(dp) + "</DP>" +
"<DQ>" + convertToString(dq) + "</DQ>" +
"<InverseQ>" + convertToString(inverseQ) + "</InverseQ>" +
"<D>" + convertToString(d) + "</D>" +
"</RSAKeyValue>";
Log.d(TAG, "getPrivateKeyAsXml: " + publicKeyAsXML);
return publicKeyAsXML;
}
private String convertToString(BigInteger n) {
return Base64.encodeToString(n.toByteArray(), Base64.NO_WRAP);
}
If on the java side it is possible to make the key in the form of pem, pass it in this form.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question