Algid 解析错误,不是序列
当尝试使用该方法从文件中读取 RSA 私钥时,
public PrivateKey getPrivateKey()
throws NoSuchAlgorithmException,
InvalidKeySpecException, IOException {
final InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream("privatekey");
byte[] privKeyBytes = null;
try {
privKeyBytes = IOUtils.toByteArray(inputStream);
} catch (final IOException exception) {
LOGGER.error("", exception);
IOUtils.closeQuietly(inputStream);
}
LOGGER.debug("privKeyBytes: {}", privKeyBytes);
String BEGIN = "-----BEGIN RSA PRIVATE KEY-----";
String END = "-----END RSA PRIVATE KEY-----";
String str = new String(privKeyBytes);
if (str.contains(BEGIN) && str.contains(END)) {
str = str.substring(BEGIN.length(), str.lastIndexOf(END));
}
KeyFactory fac = KeyFactory.getInstance("RSA");
EncodedKeySpec privKeySpec =
new PKCS8EncodedKeySpec(Base64.decode(str.getBytes()));
return fac.generatePrivate(privKeySpec);
}
遇到异常
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:200) ~[na:1.6.0_23]
at java.security.KeyFactory.generatePrivate(KeyFactory.java:342) ~[na:1.6.0_23]
我在 fac.generatePrivate(privKeySpec) 调用中
。这个错误是什么意思?
谢谢
德米特里
When trying to read a RSA private key from a file using the method
public PrivateKey getPrivateKey()
throws NoSuchAlgorithmException,
InvalidKeySpecException, IOException {
final InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream("privatekey");
byte[] privKeyBytes = null;
try {
privKeyBytes = IOUtils.toByteArray(inputStream);
} catch (final IOException exception) {
LOGGER.error("", exception);
IOUtils.closeQuietly(inputStream);
}
LOGGER.debug("privKeyBytes: {}", privKeyBytes);
String BEGIN = "-----BEGIN RSA PRIVATE KEY-----";
String END = "-----END RSA PRIVATE KEY-----";
String str = new String(privKeyBytes);
if (str.contains(BEGIN) && str.contains(END)) {
str = str.substring(BEGIN.length(), str.lastIndexOf(END));
}
KeyFactory fac = KeyFactory.getInstance("RSA");
EncodedKeySpec privKeySpec =
new PKCS8EncodedKeySpec(Base64.decode(str.getBytes()));
return fac.generatePrivate(privKeySpec);
}
I get the exception
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:200) ~[na:1.6.0_23]
at java.security.KeyFactory.generatePrivate(KeyFactory.java:342) ~[na:1.6.0_23]
at the fac.generatePrivate(privKeySpec) call.
What does this error mean?
Thanks
Dmitri
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在 Spring Boot REST API 中遇到了类似的问题。我从 Spring Boot 代码调用 DocuSign API。它在本地环境中工作正常,但在 Tomcat 9 服务器中部署 war 文件后,它开始抛出错误:
然后我重新启动 Tomcat 服务,它开始按预期工作。也许 Tomcat 无法将
BouncyCastleProvider
加载到类路径中。我希望这对将来访问这个问题的人有所帮助。
I faced a similar issue in Spring Boot REST API. I was calling DocuSign APIs from Spring Boot code. It was working fine in the local environment but after deploying the war file in Tomcat 9 server, it started throwing an error:
Then I restarted the Tomcat service, and it started working as expected. Maybe Tomcat failed to load
BouncyCastleProvider
into the classpath.I hope this will be helpful to someone who visits this question in the future.
我遇到了同样的问题,密钥的格式不是实际的问题。
要摆脱该异常,我所要做的就是致电
并且一切正常
I was having this same issue, and the format of the key was NOT the actual problem.
All I had to do to get rid of that exception was to call
and everything worked
这意味着您的密钥不是 PKCS#8 格式。最简单的方法是使用
openssl pkcs8 -topk8 <...other options...>
命令转换一次密钥。或者,您可以使用PEMReader
< Bouncycastle 轻量级 API 的 /a> 类。It means your key is not in PKCS#8 format. The easiest thing to do is to use the
openssl pkcs8 -topk8 <...other options...>
command to convert the key once. Alternatively you can use thePEMReader
class of the Bouncycastle lightweight API.您必须用您的私钥制作 PCKS8 文件!
private.pem =>私钥文件名
public_key.pem =>公钥文件名
private_key.pem => PCKS8格式的私钥名称!你可以在java中读取这种格式
You must make your PCKS8 file from your private key!
private.pem => name of private key file
public_key.pem => name of public key file
private_key.pem => name of private key with PCKS8 format! you can just read this format in java