1 year ago
#384786
Gavriel
pem to PrivateKey without bouncycaslte
I have the following bountycastle code and I need to switch it to use plain java only:
private PrivateKey getPrivateKeyFromCertificate(String certificate) throws Exception {
PEMParser parser = new PEMParser(new StringReader(certificate));
return new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) parser.readObject());
}
I tried this:
private PrivateKey getPrivateKeyFromCertificate(String certificate) throws Exception {
String pemBase64 = certificate
.replace("-----BEGIN PRIVATE KEY-----", "")
.replaceAll("\r", "")
.replaceAll("\n", "")
.replace("-----END PRIVATE KEY-----", ""));
byte[] pemData = Base64.decodeBase64(pemBase64);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(pemData));
return privateKey;
}
But I get the following error: java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: Invalid RSA private key
java
certificate
private-key
pem
0 Answers
Your Answer