Create RSA Public Key from b64 encoded string
Good Evening,
Can anyone point me in the right direction to take the modulus and exponet that have been b64 encoded and convert them back to a key?
For example, how you you turn the key below into an RSA public key?
<Modulus>13c5sP/ocFDNTIOU6GdITBumBKyPee5uluUnCQoVmxZWqj9lIyvNhkjxg3X2XFwEJjBcRqp7EmHQgeDyh14QpvsvB2fPWpEsrLtVNpOq9ZWbzcUNiUkVTL1N8QBdpB4ndtFzGeWTxJdKKGNlezzoUjh1nKdHq/+mLL4hWhInajk=</Modulus>
<Exponent>AQAB</Exponent>
Thanks,
Doug
[513 byte] By [
DougJrSa] at [2007-11-27 1:07:34]

# 1
1) Extract the Base64 modulus and exponent from the XML.
2) Base64 decode the modulus and exponent to bytes.
3) Convert the bytes of the modulus and exponent to BigIntegers making sure you force them to be positive (read the Javadoc).
4) Create an RSAPublicKeySpec object from BigInteger versions of the modulus and exponent.
5) Create an RSA KeyFactory.
6) Use the KeyFactory with the RSAPublicKeySpec to generate the RSAPublicKey.
# 2
byte[] key_bytes= Base64.decode(parameter);
X509EncodedKeySpec es= new X509EncodedKeySpec(key_bytes);
KeyFactory keyFactory=null;
try {
keyFactory = KeyFactory.getInstance("RSA");
} catch (NoSuchAlgorithmException e1) {
throw new KeyManagerClientExceptions(e1);
}
try {
sspublickey= keyFactory.generatePublic(es);
} catch (InvalidKeySpecException e1) {
throw new KeyManagerClientExceptions(e1);
}
}
# 4
I was pointed to this article http://forum.java.sun.com/thread.jspa?threadID=756349&messageID=4351981
that answered my question.
I also discovered that Base64 in Java in not always compatable with Base64 in .net. The Base64 encoder/decoder in sun.misc is not able to corretly decode a message from .net and is not able to encode a message that .net can decode.
# 5
> I also discovered that Base64 in Java in not always
> compatable with Base64 in .net. The Base64
> encoder/decoder in sun.misc is not able to corretly
> decode a message from .net and is not able to encode
> a message that .net can decode.
I have used many Base64 encoders and decoders and have not found any major compatibility problems. What .net message could you not decode?