Thursday, January 28, 2016 Only RSAPrivate(Crt)KeySpec and PKCS8EncodedKeySpec supported for RSA private keys

public PrivateKey getPrivateKey(String fileName) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException
byte[] keyFileBytes = readKeyFile(fileName);
X509EncodedKeySpec spec =
new X509EncodedKeySpec(keyFileBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(spec);

The reason is this line, This is used to read public key spec and not private key. The private key is read via

PKCS8EncodedKeySpec spec =
      new PKCS8EncodedKeySpec(keyBytes);
So as the error says it needs to be in PKCS8 spec format for java to understand.

No comments:

Post a Comment