首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在没有私钥(NBitcoin)的情况下验证

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

Stack Overflow用户
提问于 2017-11-28 05:52:19
回答 1查看 422关注 0票数 0

我正在使用NBitcoin nuget包。我尝试创建私钥和公钥(地址)。在此之后,我尝试对一些消息进行签名,然后使用发布密钥验证此签名。但是NBitcoin,对于使用作为具有私钥对象的BitcoinSecret对象的验证。那么,为什么要使用这个对象来验证NBitcoin呢?如何在没有私钥的情况下验证签名,仅使用地址(PubKey)、签名和消息?谢谢

代码语言:javascript
复制
     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();

           }
EN

回答 1

Stack Overflow用户

发布于 2018-02-16 06:57:58

没有私钥,公钥就不能存在,因为公钥是通过利用某种单向函数从私钥导出的。如果您想使用公钥而不使用私钥,那么就像您一样,从私钥生成它

代码语言:javascript
复制
var pubKey = privateKey.PubKey;

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

代码语言:javascript
复制
File.WriteAllBytes("some/public/location/MyPubKey.key", pubKey.ToBytes());

让验证者读取公钥而不知道私钥

代码语言:javascript
复制
var pubKeyForVerification = File.ReadAllBytes("some/public/location/MyPubKey.key");

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47520438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档