有弹性的城堡将ASN.1转换为r和s字节数组 [英] Bouncy Castle convert ASN.1 to r and s byte arrays

查看:99
本文介绍了有弹性的城堡将ASN.1转换为r和s字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码会生成数字签名:

I have the following code that generates a digital signature:

byte[] GetSignature(byte[] message, byte[] privateKey)
{
    var ecParams = NistNamedCurves.GetByName("P-256");
    var domainParameters = new ECDomainParameters(ecParams.Curve, ecParams.G, ecParams.N, ecParams.H, ecParams.GetSeed());
    var d = new BigInteger(1, privateKey);
    var privateKeyParameters = new ECPrivateKeyParameters(d, domainParameters);

    var signer = SignerUtilities.GetSigner("SHA-256withECDSA");
    signer.Init(true, privateKeyParameters);
    signer.BlockUpdate(message, 0, message.Length);

    var signature = signer.GenerateSignature();
    return signature;
}

此代码返回ASN.1格式的71字节数组,但是我使用的代码期望签名为64字节,由r和s值组成.我想提取r和s字节数组.我该怎么做?

This code returns a 71 byte array in ASN.1 format, however my consuming code expects the signature to be 64 bytes, composed of the r and s values. I would like to extract the r and s byte arrays. How do I do this?

推荐答案

在最新版本的bc-csharp中,您只需更改"SHA-256withECDSA"即可.到具有PLAIN-ECDSA的SHA-256";然后GenerateSignature将直接产生64字节的签名,而不是ASN.1值.

In recent versions of bc-csharp, you can just change "SHA-256withECDSA" to "SHA-256withPLAIN-ECDSA" and then GenerateSignature will directly produce the 64 byte signature instead of the ASN.1 value.

这篇关于有弹性的城堡将ASN.1转换为r和s字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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