本文介绍了基于BouncyCastle SHA-256和PLAIN-ECDSA的确定性签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用如下所示的代码进行签名。我正在试图找到一种创建确定性签名的方法。BouncyCastle可以做到这一点吗?var curve = SecNamedCurves.GetByName("secp256r1");
var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);
var p = new Org.BouncyCastle.Math.BigInteger(privateKey);
var keyParameters = new ECPrivateKeyParameters(
new Org.BouncyCastle.Math.BigInteger(1,privateKey),
domain);
ISigner signer = SignerUtilities.GetSigner("SHA-256withPLAIN-ECDSA");
signer.Init(true, keyParameters);
signer.BlockUpdate(message, 0, message.Length);
var signature = signer.GenerateSignature();
推荐答案
是的。下面显示了使用曲线P-256(也称为secp256r1)和RFC6979, A.2.5中的测试向量生成具有确定性ECDSA的签名:
- 测试向量:
私钥:
X=C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721使用SHA-256,Message=";Sample&Quot;: R=EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716
S=F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8
- C#/BC代码
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Signers;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.Text;
...
string messageToSign = "sample";
var digest = new System.Security.Cryptography.SHA256Managed();
byte[] messageHash = digest.ComputeHash(Encoding.UTF8.GetBytes(messageToSign));
X9ECParameters x9Params = SecNamedCurves.GetByName("secp256r1"); ;
ECDomainParameters ecParams = new ECDomainParameters(x9Params.Curve, x9Params.G, x9Params.N, x9Params.H);
ECPrivateKeyParameters privKey = new ECPrivateKeyParameters(new BigInteger(1, Hex.Decode("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721")), ecParams);
ECDsaSigner signer = new ECDsaSigner(new HMacDsaKCalculator(new Sha256Digest()));
signer.Init(true, privKey);
var signature = signer.GenerateSignature(messageHash);
Console.WriteLine(signature[0].ToString(16).ToUpper()); // r: EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716
Console.WriteLine(signature[1].ToString(16).ToUpper()); // s: F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8
输出:
EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716
F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8
与测试向量的签名匹配。
这篇关于基于BouncyCastle SHA-256和PLAIN-ECDSA的确定性签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!