0

I am trying to generate a RSAPublicKey from the publickey.pem file but it is throwing me exception .

https://github.com/cerner/cds-hooks-sandbox/blob/master/ecpublickey.pem

Invalid token :::java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format

 KeyFactory factory = KeyFactory.getInstance("RSA");


 PemReader pemReader = new PemReader(new InputStreamReader(new 
 FileInputStream(filename)));
 PemObject pemObject = pemReader.readPemObject();

 byte[] content = pemObject.getContent();
 X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(content);
 RSAPublicKey publicKey =  factory.generatePublic(pubKeySpec);

It works fine if I generate and save my own pem file.

vijay
  • 1
  • 2
    As the name `ecpublickey.pem` indicates, that is an EC (Elliptic Curve, also spelled ECC Elliptic Curve Cryptography) public key, in PEM format. It is not an RSA public key. RSA is entirely different from EC, and it is completely impossible to convert an EC public key to an RSA public key. – dave_thompson_085 Jan 21 '18 at 08:45
  • That said, `"ECDSA"` and `ECPublicKey` would probably work if the generation of `X509EncodedKeySpec` didn't throw any exceptions. – Maarten Bodewes Jan 30 '18 at 01:12

0 Answers0