How to recover a RSA public key from a byte[] array?

JavaCryptographyRsa

Java Problem Overview


I'm wondering if it's possible to recover a RSA public key that I have converted to byte array previously.

byte[] keyBytes = publicKey.getEncoded();

Thanks for the help.

Java Solutions


Solution 1 - Java

PublicKey publicKey = 
    KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));

For more info http://docs.oracle.com/javase/tutorial/security/apisign/vstep2.html">see this tutorial

Solution 2 - Java

For others who want to get private key instead of public key from byte array:

PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
				

Solution 3 - Java

Great answer. Thanks for the link. Just for complete, I found this https://stackoverflow.com/questions/5364338/converted-secret-key-into-bytes-how-to-convert-it-back-to-secrect-key

SecretKey key2 = new SecretKeySpec(data, 0, data.length, "DES");

and just worked very well.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkiewicView Question on Stackoverflow
Solution 1 - JavaBozhoView Answer on Stackoverflow
Solution 2 - JavaMarko KotarView Answer on Stackoverflow
Solution 3 - JavaAdrienView Answer on Stackoverflow