尝试验证签名,抛出异常:java.io.IOException:序列标记错误

发布于 2024-10-17 17:49:00 字数 1311 浏览 2 评论 0原文

我正在尝试验证消息的签名,但抛出以下异常。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2024-10-24 17:49:00

有点奇怪,因为我以为这会随 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文