我正在使用NBitcoin nuget包。我尝试创建私钥和pub键(地址)。然后,我尝试签署一些消息,然后使用pub键验证此签名。但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();
}
答案 0 :(得分:0)
如果没有私钥,公钥就不存在,因为公钥是通过利用某种单向函数从私钥派生的。如果你想使用没有私钥的公钥,那么就像你做的那样从私钥生成
var pubKey = privateKey.PubKey;
将公钥存储到验证者可以访问的某个位置
File.WriteAllBytes("some/public/location/MyPubKey.key", pubKey.ToBytes());
让验证者在不知道私钥的情况下阅读公钥
var pubKeyForVerification = File.ReadAllBytes("some/public/location/MyPubKey.key");
这就是它的全部。将公钥存储在任何你想要的位置是安全的,因为从中学习私钥几乎是不可能的。