Generating ASN1 encoded signature in C # to send to Java

I have a private / public secure certificate. My Java partners have a public key. I need to take a string, sign it and send it to Java so that I can then verify the data and signature.

There seems to be a known issue with how Microsoft and the rest of the world encode / sign data, something about how bytes are handled. This is so well known that I cannot find a solution. If they take my string and private key, they can obviously sign and verify it correctly. If I take my string, I can sign and verify it. I've seen many methods for converting from ASN1 to Microsoft (think P1363), but not converting from Microsoft, C # to ASN1 for Java. I am not doing what is happening well enough to figure out how to reverse engineer.

I researched http://www.codeproject.com/Articles/25487/Cryptographic-Interoperability-Keys but the end result was not what the Java side wanted. I can sign the string and I get the signature, but Java guys tell me it needs to start with MC, the first bytes are indicators. I don't see that.

Thank!

+3


source to share


1 answer


The solution is found and looks like some of the other examples I've seen, but for some reason this works better: (the method is named after the guy who solved it for me;)



            private static byte[] Rays(byte[] sigBytes)
            {
                bool highMsbR = (sigBytes[0] & 0x80) != 0;
                bool highMsbS = (sigBytes[20] & 0x80) != 0;  

                MemoryStream stream = new MemoryStream();
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Write((byte)0x30);
                    int len = 44 + (highMsbR ? 1 : 0) + (highMsbS ? 1 : 0);
                    writer.Write((byte)len);

                    // r
                    writer.Write((byte)0x02);
                    writer.Write((byte)(highMsbR ? 21 : 20));
                    if (highMsbR)
                        writer.Write((byte)0); 
                    for (int i = 0; i < 20; i++)
                        writer.Write(sigBytes[i]); 

                    // s
                    writer.Write((byte)0x02);
                    writer.Write((byte)(highMsbS ? 21 : 20));
                    if (highMsbS)
                        writer.Write((byte)0);
                    for (int i = 20; i < 40; i++)
                        writer.Write(sigBytes[i]);
                }

                byte[] bytes = stream.ToArray();
                return bytes;
            }

      

+2


source







All Articles