尝试验证签名,抛出异常:java.io.IOException:序列标记错误
我正在尝试验证消息的签名,但抛出以下异常。
Exception in thread "main" java.security.SignatureException: Signature encoding error
at sun.security.rsa.RSASignature.engineVerify(RSASignature.java:185)
at java.security.Signature$Delegate.engineVerify(Signature.java:1140)
at java.security.Signature.verify(Signature.java:592)
... 4 more
Caused by: java.io.IOException: Sequence tag error
at sun.security.util.DerInputStream.getSequence(DerInputStream.java:280)
at sun.security.rsa.RSASignature.decodeSignature(RSASignature.java:209)
at sun.security.rsa.RSASignature.engineVerify(RSASignature.java:174)
... 6 more
这是我的 Java 代码
Signature sig = Signature.getInstance("SHA1withRSA");
sig.initVerify(publicKey);
sig.update(message);
return sig.verify(signature);
JCERSAPublicKey 的实例
publicKey
是我在这个 网站 表明此错误是由于 证书 缺少正确的页眉和页脚标记(即 -----BEGIN证书-----
和-----END CERTIFICATE-----
)。 但是,我没有使用证书,而是使用公钥。查看我的公钥 PEM 文件,我可以看到它具有适合公钥的标头。
-----BEGIN PUBLIC KEY-----
...STUFF
...STUFF
...STUFF
...STUFF
...STUFF
...STUFF
-----END PUBLIC KEY-----
I'm trying to verify the signature of a message, but I am getting the following exception being thrown.
Exception in thread "main" java.security.SignatureException: Signature encoding error
at sun.security.rsa.RSASignature.engineVerify(RSASignature.java:185)
at java.security.Signature$Delegate.engineVerify(Signature.java:1140)
at java.security.Signature.verify(Signature.java:592)
... 4 more
Caused by: java.io.IOException: Sequence tag error
at sun.security.util.DerInputStream.getSequence(DerInputStream.java:280)
at sun.security.rsa.RSASignature.decodeSignature(RSASignature.java:209)
at sun.security.rsa.RSASignature.engineVerify(RSASignature.java:174)
... 6 more
Here is my Java code
Signature sig = Signature.getInstance("SHA1withRSA");
sig.initVerify(publicKey);
sig.update(message);
return sig.verify(signature);
publicKey
is an instance of JCERSAPublicKey
I read on this website that this error is being caused by the certificate missing the proper header and footer tags (i.e., -----BEGIN CERTIFICATE-----
and -----END CERTIFICATE-----
). However, I am not using a certificate, but a public key. Looking at my public key PEM file, I can see it has the appropriate headers for a public key.
-----BEGIN PUBLIC KEY-----
...STUFF
...STUFF
...STUFF
...STUFF
...STUFF
...STUFF
-----END PUBLIC KEY-----
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有点奇怪,因为我以为这会随 Java 一起提供,但签名算法没有服务提供者。我下载了 BouncyCastle,并将其添加为提供程序后,代码不再抛出异常。
A bit odd, because I thought this would have shipped with Java, but there was no Service Provider for the Signature Algorithm. I downloaded BouncyCastle and after adding it as a provider, the code no longer throws up an exception.