mardi 3 février 2015

How to convert from String to PublicKey?


Vote count:

0




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 public key to private key. So I used 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.



asked 1 min ago







How to convert from String to PublicKey?

Aucun commentaire:

Enregistrer un commentaire