pem 到 PrivateKey,无需 bouncycaslte
我有以下 bountycastle 代码,我需要将其切换为仅使用纯 java:
private PrivateKey getPrivateKeyFromCertificate(String certificate) throws Exception {
PEMParser parser = new PEMParser(new StringReader(certificate));
return new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) parser.readObject());
}
我尝试了以下操作:
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;
}
但出现以下错误: java.security.spec.InvalidKeySpecException:java.security.InvalidKeyException:无效的 RSA 私钥
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论