繁体   English   中英

如何在没有私钥的情况下进行验证(NBitcoin)

[英]How can verify without private key (NBitcoin)

我正在使用NBitcoin nuget包。 我尝试创建私钥和发布密钥(地址)。 之后,我尝试签署一些消息,然后使用pub key验证此签名。 但是NBitcoin,使用具有私钥对象的BitcoinSecret对象进行验证。 那么,为什么要使用该对象来验证NBitcoin? 而且如何仅使用address(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();

           }

没有私有密钥就不能存在公共密钥,因为公共密钥是通过利用某种单向功能从私有密钥派生的。 如果要使用不带私钥的公钥,则像您一样从私钥生成它

var pubKey = privateKey.PubKey;

将公钥存储到验证者可以访问的某个位置

File.WriteAllBytes("some/public/location/MyPubKey.key", pubKey.ToBytes());

让验证者在不知道私钥的情况下读取公钥

var pubKeyForVerification = File.ReadAllBytes("some/public/location/MyPubKey.key");

这就是全部。 将公钥存储在您想要的任何地方都是安全的,因为从中学习私钥几乎是不可能的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM