我正在使用NBitcoin nuget包。我尝试创建私钥和公钥(地址)。在此之后,我尝试对一些消息进行签名,然后使用发布密钥验证此签名。但是NBitcoin,对于使用作为具有私钥对象的BitcoinSecret对象的验证。那么,为什么要使用这个对象来验证NBitcoin呢?如何在没有私钥的情况下验证签名,仅使用地址(PubKey)、签名和消息?谢谢
static void Main(string[] args)
{
Key Key = new Key(); //Create private key
//We can take private key
var privateKey = Key.GetBitcoinSecret(Network.Main);
//Get the public key, and derive the address on the Main network
BitcoinAddress address = privateKey.PubKey.GetAddress(Network.Main);
For the sign to data , create secret object.
BitcoinSecret secret = new BitcoinSecret(Key, Network.Main);
string message = $"I am Nicolas";
Console.WriteLine("Message:" + message + "\n");
sign message with private key.
string signature = secret.PrivateKey.SignMessage(message);
Console.WriteLine("Signature:" + signature + "\n");
/* Now I dont understand this code. For the verify , I know that we need
to signature message , message and pub or adres value.\n But in this code using
again private secret object which has got private key. How we can verify
signature messaga with pub or address and message (dont include private key)*/
if (secret.PubKey.VerifyMessage(message, signature))
{
Console.WriteLine("thats okey");
}
Console.Read();
}发布于 2018-02-16 06:57:58
没有私钥,公钥就不能存在,因为公钥是通过利用某种单向函数从私钥导出的。如果您想使用公钥而不使用私钥,那么就像您一样,从私钥生成它
var pubKey = privateKey.PubKey;将公钥存储到验证者有权访问的某个位置
File.WriteAllBytes("some/public/location/MyPubKey.key", pubKey.ToBytes());让验证者读取公钥而不知道私钥
var pubKeyForVerification = File.ReadAllBytes("some/public/location/MyPubKey.key");这就是它的全部。将公钥存储在您想要的任何位置都是安全的,因为几乎不可能从其中学习私钥。
https://stackoverflow.com/questions/47520438
复制相似问题