22

I've used the following code to convert the public and private key to a string

KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048);
KeyPair          keyPair    = keyPairGen.genKeyPair();
PublicKey        publicKey  = keyPair.getPublic();
PrivateKey       privateKey = keyPair.getPrivate();
String publicK = Base64.encodeBase64String(publicKey.getEncoded());
String privateK = Base64.encodeBase64String(privateKey.getEncoded());

Now I'm trying to convert it back to public ad private key

PublicKey publicDecoded = Base64.decodeBase64(publicK);

I'm getting error of cannot convert from byte[] to public key. So I tried like this

PublicKey publicDecoded = new SecretKeySpec(Base64.decodeBase64(publicK),"RSA");

This leads to error like below

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: Neither a public nor a private key

Looks like I'm doing wrong key conversion here. Any help would be appreciated.

The Coder
  • 2,562
  • 5
  • 33
  • 62
  • possible duplicate of [Load stored RSA public/private key from disk?](http://stackoverflow.com/questions/19366498/load-stored-rsa-public-private-key-from-disk) – Artjom B. Feb 03 '15 at 08:51
  • 1
    possible duplicate of [Converting Strings to encryption keys and vice versa java](http://stackoverflow.com/questions/9755057/converting-strings-to-encryption-keys-and-vice-versa-java) – Sufiyan Ghori Feb 03 '15 at 08:54
  • Thanks guys. Now I'm able to successfully convert it back to PublicKey using X509EncodedKeySpec. I do searched before posting the question, but what I've seen was mostly involves conversion of SecretKey but not public and private keys. – The Coder Feb 03 '15 at 09:00

1 Answers1

50

I don't think you can use the SecretKeySpec with RSA.

This should do:

byte[] publicBytes = Base64.decodeBase64(publicK);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pubKey = keyFactory.generatePublic(keySpec);

And to decode the private use PKCS8EncodedKeySpec

weston
  • 54,145
  • 21
  • 145
  • 203
  • 1
    thanks. I forgot about private key. I'll use PKCS8EncodedKeySpec for private key. – The Coder Feb 03 '15 at 09:16
  • 7
    Which `import` does the above `Base64` come from? – Hooli Aug 19 '16 at 12:04
  • 6
    @Hooli I think it's from org.apache.commons.codec.binary.Base64. also, you could use Base64.getDecoder().decode(publicK) in the java.util if you use Java 1.8 – gybandi Oct 12 '16 at 11:27
  • 3
    I am trying to do the same thing but am getting java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException ... when it tries to execute the last line .. keyfactory.generatePublic(keySpec) – Haya Raed Apr 12 '17 at 14:02
  • @HayaRaed ask a new question, you'll get far more attention. – weston Apr 12 '17 at 15:45
  • How do you do this with javascript? – Renz Carlo May 14 '23 at 03:47
  • @RenzCarlo Ask a new question. By asking on an answer only that person (me in this case) gets notified about your comment. So you're asking 1 person rather than the whole of stack overflow and I am 100% not your guy when it comes to JS. – weston May 15 '23 at 16:49