Bouncy Castle convert ASN.1 to r and s byte arrays

徘徊边缘 提交于 2021-01-28 13:31:52

问题


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;
}

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?


回答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.



来源:https://stackoverflow.com/questions/62851660/bouncy-castle-convert-asn-1-to-r-and-s-byte-arrays

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