Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 206
kiewic Avatar asked Mar 09 '10 17:03

kiewic


People also ask

How do I recover my public key from a private key?

Presently the only way of "recovering" the Private key from the Public key is by exhaustive search (brute force). The system was specifically designed this way so that you could issue your public key to anyone without worrying about them being able to figure out your private key.

Does RSA private key include public key?

The private key always includes the public key. What you might really want is Signing. Using the same . NET classes, you can sign data with your private key and verify the signature on the other party's side with the public key (which obviously doesn't contain the private key).


2 Answers

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

For more info see this tutorial

like image 53
Bozho Avatar answered Sep 19 '22 19:09

Bozho


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

PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes)); 
like image 39
Marko Kotar Avatar answered Sep 19 '22 19:09

Marko Kotar