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

问题描述 投票:0回答:2

我正在使用 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();

           }
c# cryptography digital-signature blockchain nbitcoin
2个回答
0
投票

没有私钥,公钥就不可能存在,因为公钥是通过利用某种单向函数从私钥派生出来的。如果您想在没有私钥的情况下使用公钥,请像您一样从私钥生成它

var pubKey = privateKey.PubKey;

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

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

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

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

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


0
投票

抱歉,我这次谈话迟到了。 OP 得出的结论是完全正确的,即您不需要私钥来验证签名。签名过程中唯一需要私钥的时刻是私钥所有者签署文档时。您可以仅使用公钥来验证签名。 OP 使用的示例从签署文档的私钥中获取公钥。这不是获得 NBitcoin PubKey 的唯一方法。

但是你需要知道二进制公钥。仅仅知道公钥的地址是不够的。但二进制公钥可以自由共享。知道二进制公钥后,您可以通过两种方式实例化 NBitcoin PubKey:

如果您知道公钥的十六进制字符串:

PubKey pubKey = new PubKey(publicKeyHexString);

如果您有二进制数组中的公钥:

PubKey pubKey = new PubKey(publicKeyBinaryArray);

我想指出,我不再看到(私钥)Key.SignMessage() 或(公钥)PubKey.VerifyMessage() 方法。我只找到(私钥)Key.Sign() 和(公钥)PubKey.Verify() 方法。我怀疑这些已被删除,因此实施者可以完全控制签名过程中文档的哈希算法。

© www.soinside.com 2019 - 2024. All rights reserved.