2

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-----
2
  • That error has nothing whatsoever to do with the key file. Commented Feb 17, 2011 at 0:12
  • Yea, I figured that out, hence my solution below. Thanks for trying to help though. Commented Feb 17, 2011 at 15:15

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Can't be, your changing multiple things at the same time and finally something worked so you attribute it to the last thing you did.
What? How did you come up with this idea: "your changing multiple things at the same time"?
Wouldn't that have thrown a "NoSuchAlgorithmException" instead?
@John may you show us an example of the final code ? I'm particularly interested by the "adding it as a provider" part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.