Question

I'm doing about verifying digital signature. And when I was trying to use SignedCms.Decode() , it caused System.Security.Cryptography.CryptographicException {"ASN1 bad tag value met.\r\n"}

I generate the signature data in Java, and try to verify it in C#.

Here is C# code for verifying signature.

//base64 signature data
string encodedMessage_b64 = "ahXwmjFNUVxxxxxx==";
byte[] encodedMessage = Convert.FromBase64String(encodedMessage_b64);
SignedCms signedCms = new SignedCms();
//throw exception
signedCms.Decode(encodedMessage);

And here is Java code for generating signature.

Signature rsaSig = Signature.getInstance("SHA1withRSA");
//privateKey from keytore
rsaSig.initSign(priKey);
rsaSig.update(data.getBytes());
//org.apache.commons.codec.binary.Base64;
String signedData = Base64.encodeBase64String(rsaSig.sign());

The problem is carriage return and line feed, I don't know where they come from.

Any advice would be greatly appreciated :)

Was it helpful?

Solution

CR and LF are no problem; the \r\n in the exception message is just part of the exception message. The real symptom is "ASN1 bad tag". That's because dotnet SignedCMS is for Cryptographic Message Syntax (CMS) data, which is way different and MUCH more complicated than a simple RSA signature as you've created. See RFC 3369 et al for details.

If you really want to use a near-raw signature (with PKCS#1 padding as nearly everybody including Java does, but not algid, entity/key identification, additional data, certs, type identification, etc.) you'll need to do something different to receive it in Csharp. I can't help with that part.

If you want to generate CMS SignedData in Java, see Sign data using PKCS #7 in JAVA (BouncyCastle) or https://security.stackexchange.com/questions/13910/pkcs7-encoding-in-java-without-external-libs-like-bouncycastle-etc (sun extension).

If you're not wedded to Java, openssl (in highly portable C) also does a good job on CMS/PKCS#7.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top