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开发者_如何学Go the appropriate headers for a public key.
-----BEGIN PUBLIC KEY-----
...STUFF
...STUFF
...STUFF
...STUFF
...STUFF
...STUFF
-----END PUBLIC KEY-----
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.
精彩评论