从ECDSA签名签名c#提取R和S

发布于 2025-01-30 23:35:55 字数 125 浏览 4 评论 0原文

我正在使用.NET 5中的signedxml类用于使用ECDA生成签名 我需要R和S的价值,但我无法提取它,另一个问题是 签名长度。我的签名总是64个字节,但ECDSA签名长度为71 而且我不知道为什么这段时间会改变。请帮助我提取R和S

I'm using Signedxml class in .net 5 to generate signature using ECDSA
and I need the value of r and s but I can not extract it and another problem is
signature length . My signature always 64 bytes but ECDSA signature length is 71
and I do not know why this length change . Please help me extract r and s

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2025-02-06 23:35:55

将ECDA签名从ASN.1/der转换为p1363(r | s)格式时,必须考虑以下内容:

  • 以ASN.1/der格式r和s包含为签名, Big Endian阵列,以p1363格式为 unsigned ,Big Endian Arrays。
  • 在ASN.1/der格式中,将r和s作为最小尺寸阵列包含,在p1363中,两者都填充到其最大尺寸(发电机点的顺序长度)中带领先的0x00值。示例:对于NIST P-256,R和S的最大尺寸为32字节。

使用asnreader class class

using System.Formats.Asn1;
...
public static byte[] DERtoP1363(byte[] derSignature, int maxSize)
{
    AsnReader sequence = new AsnReader(derSignature, AsnEncodingRules.DER).ReadSequence();
    byte[] rBytes = sequence.ReadInteger().ToByteArray(true, true); // convert to unsigned, big endian
    byte[] sBytes = sequence.ReadInteger().ToByteArray(true, true); // convert to unsigned, big endian
    byte[] rsBytes = new byte[2 * maxSize];
    Buffer.BlockCopy(rBytes, 0, rsBytes, maxSize - rBytes.Length, rBytes.Length);     // resize to maximum size
    Buffer.BlockCopy(sBytes, 0, rsBytes, 2 * maxSize - sBytes.Length, sBytes.Length); // resize to maximum size
    return rsBytes;
}

class

自从.NET 5。对于完整性以来,可以 类实现。 org.bouncycastle.asn1名称空间)。为此,dertop1363()中的前三行必须由以下方式代替:

Asn1Sequence sequence = Asn1Sequence.GetInstance(derSignature);
byte[] rBytes = DerInteger.GetInstance(sequence[0]).PositiveValue.ToByteArrayUnsigned();
byte[] sBytes = DerInteger.GetInstance(sequence[1]).PositiveValue.ToByteArrayUnsigned();

When converting the ECDSA signature from ASN.1/DER to P1363 (r|s) format, the following must be taken into account:

  • In ASN.1/DER format r and s are contained as signed, big endian arrays, in P1363 format as unsigned, big endian arrays.
  • In ASN.1/DER format r and s are included as minimal sized arrays, in P1363 both are padded to their maximum size (length of the order of the generator point) with leading 0x00 values. Example: For NIST P-256, the maximum size of r and s is 32 bytes each.

Possible implementation with .NET 5+ using the AsnReader class:

using System.Formats.Asn1;
...
public static byte[] DERtoP1363(byte[] derSignature, int maxSize)
{
    AsnReader sequence = new AsnReader(derSignature, AsnEncodingRules.DER).ReadSequence();
    byte[] rBytes = sequence.ReadInteger().ToByteArray(true, true); // convert to unsigned, big endian
    byte[] sBytes = sequence.ReadInteger().ToByteArray(true, true); // convert to unsigned, big endian
    byte[] rsBytes = new byte[2 * maxSize];
    Buffer.BlockCopy(rBytes, 0, rsBytes, maxSize - rBytes.Length, rBytes.Length);     // resize to maximum size
    Buffer.BlockCopy(sBytes, 0, rsBytes, 2 * maxSize - sBytes.Length, sBytes.Length); // resize to maximum size
    return rsBytes;
}

AsnReader is available since .NET 5.

For completeness: In other .NET versions BouncyCastle can be applied (using classes from the Org.BouncyCastle.Asn1 namespace). For this, the first three lines in DERtoP1363() must by replaced by:

Asn1Sequence sequence = Asn1Sequence.GetInstance(derSignature);
byte[] rBytes = DerInteger.GetInstance(sequence[0]).PositiveValue.ToByteArrayUnsigned();
byte[] sBytes = DerInteger.GetInstance(sequence[1]).PositiveValue.ToByteArrayUnsigned();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文