Creating ASN1 encoded signature in C# to send to Java

烂漫一生 提交于 2019-12-05 19:22:45

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;
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!