在C#中创建ASN1编码签名以发送到Java [英] Creating ASN1 encoded signature in C# to send to Java

查看:640
本文介绍了在C#中创建ASN1编码签名以发送到Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个私人/公共安全证书。我的Java对应的公钥。我需要采取一个字符串,签名,并将其发送到Java,然后验证数据和签名。

I have a private/public secure cert. My Java counterparts have the public key. I have the need to take a string, sign it, and send it along to Java to then verify the data and signature.

似乎有一个众所周知的问题,如何微软和世界其他地方编码/标志数据,一些关于字节的处理方式。它是如此众所周知,我找不到一个解决方案。如果他们拿我的字符串和私钥,他们可以明显地正确地签名,并验证它。如果我拿着我的字符串,我可以在.Net内签名和验证。我看到了一些方法从ASN1转换为微软的格式(我认为P1363),但不是从Microsoft,C#转换为ASN1为Java。

There appears to be a well known issue with how Microsoft and the rest of the world encodes/signs data, something about the way bytes are handled. It's so well known, that I can't find a solution. If they take my string and the private key, they can obviously sign it correctly, and verify it. If I take my string, I can sign and verify it within .Net fine. I have seen a slew of methods for converting from ASN1 to Microsoft's format (I think P1363), but not converting from Microsoft, C#, to ASN1 for Java. I don't what is going on well enough to understand how to reverse engineer.

我已经探索过 http://www.codeproject.com/Articles/25487/Cryptographic-Interoperability-Keys 但最终的结果不是什么java侧需要。我可以签署一个字符串,我得到一个签名,但Java家伙告诉我它需要从MC开始,第一个字节是指标。我没有看到这个。

I've explored http://www.codeproject.com/Articles/25487/Cryptographic-Interoperability-Keys but the final result wasn't what the java side needed. I can sign a string, and I get a signature, but Java guys are telling me it needs to start with MC, first bytes are indicators. I am not seeing this.

谢谢!

推荐答案

已经找到,看起来像一些其他的例子,我已经看到,但由于某种原因,这更好:(方法命名后的家伙谁解决了它为我;)

A solution has been found, and looks like some of the other examples I've been seeing, but for some reason this works better: (method 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;
            }

这篇关于在C#中创建ASN1编码签名以发送到Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆