基于BouncyCastle SHA-256和PLAIN-ECDSA的确定性签名

deterministic signatures using BouncyCastle SHA-256withPLAIN-ECDSA(基于BouncyCastle SHA-256和PLAIN-ECDSA的确定性签名)
本文介绍了基于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的确定性签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)