C# ECDsaCng.SignData 在 OpenSSL 中使用签名? [英] C# ECDsaCng.SignData use signature in OpenSSL?

查看:108
本文介绍了C# ECDsaCng.SignData 在 OpenSSL 中使用签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道什么编码

https://msdn.microsoft.com/en-us/library/bb347054(v=vs.110).aspx

ECDsaCng.SignData Method (Byte[])

默认使用它的字节数组以及如何将其转换为接受的DER格式

is using for it's byte array by default and how to convert it to a DER format accepted by

https://www.openssl.org/docs/manmaster/crypto/i2d_ECDSA_SIG.html

以便我可以使用方法 ECDSA_do_verify 在 OpenSSL 中验证我的 C# 生成的 ECDSA 签名.

so that I can verify my C# generated ECDSA signature in OpenSSL using the method ECDSA_do_verify.

我知道它的 SHA1 并且我知道如何加载该摘要,我只是不知道 ECDsaCng.SignData 方法的字节编码是什么,也不知道如何将其转换为 DER 格式,如果我什至需要这样做.

I know its SHA1 and I know how to load that digest, I just don't know what the byte encoding is for the ECDsaCng.SignData method, nor how to convert it to DER format, if I even need to do that.

推荐答案

ECDSA 签名是值对 (r, s).

An ECDSA signature is the value-pair (r, s).

Windows CNG 将此作为两个值的串联发出,并且由于 .NET 不会重新解释数据,因此这是事实上的 .NET 格式.(r 是数组的前半部分,s 是后半部分)

Windows CNG emits this as a concatenation of the two values, and since .NET does not reinterpret the data, this is the de facto .NET format. (r is the first half of the array, s is the second half)

OpenSSL 需要 SEQUENCE(INTEGER(r), INTEGER(s)) 的 DER 编码结构.这大致意味着 { 0x30, payload_length, 0x02, r_length, r_bytes[0]...r_bytes[r_length-1], 0x02, s_length, s_bytes[0]...s_bytes[s_length-1] };虽然比这稍微复杂一些,因为当 r_bytes[0] 或 s_bytes[0] >= 0x80 时需要填充字节(因为这使得它显示为负数).

OpenSSL expects a DER encoded structure of SEQUENCE(INTEGER(r), INTEGER(s)). This loosely means { 0x30, payload_length, 0x02, r_length, r_bytes[0]...r_bytes[r_length-1], 0x02, s_length, s_bytes[0]...s_bytes[s_length-1] }; though it's slightly trickier than that because padding bytes are required when r_bytes[0] or s_bytes[0] >= 0x80 (since that makes it appear as a negative number).

不幸的是,默认情况下,框架中没有公开通用 DER 编码器.

Unfortunately, there's not a general purpose DER encoder exposed in the framework by default.

如果您尝试在 Linux 上运行,使用 .NET Core 可能会更好,因为 Linux 的 ECDSA 实现 已经进行了这种数据转换.

If you're trying to run on Linux, you might be better served by using .NET Core, since the ECDSA implementation for Linux already does this data translation.

这篇关于C# ECDsaCng.SignData 在 OpenSSL 中使用签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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