kiewic
kiewic

Reputation: 16450

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

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.

Upvotes: 59

Views: 63166

Answers (3)

Adrien
Adrien

Reputation: 375

Great answer. Thanks for the link. Just for complete, I found this 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.

Upvotes: -1

Marko Kotar
Marko Kotar

Reputation: 459

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

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

Upvotes: 46

Bozho
Bozho

Reputation: 597362

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

For more info see this tutorial

Upvotes: 120

Related Questions