2

我正在尝试按照此处的教程进行操作。我的代码如下:

static void Main(string[] args)
{
    try
    {
        Key privateKey = new Key(); // generate a random private key
        PubKey publicKey = privateKey.PubKey;
        Console.WriteLine(publicKey); // 0251036303164f6c458e9f7abecb4e55e5ce9ec2b2f1d06d633c9653a07976560c

        Console.WriteLine(publicKey.GetAddress(Network.Main)); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY
        Console.WriteLine(publicKey.GetAddress(Network.TestNet)); // n3zWAo2eBnxLr3ueohXnuAa8mTVBhxmPhq

        var publicKeyHash = publicKey.Hash;
        Console.WriteLine(publicKeyHash); // f6889b21b5540353a29ed18c45ea0031280c42cf
        var mainNetAddress = publicKeyHash.GetAddress(Network.Main);
        var testNetAddress = publicKeyHash.GetAddress(Network.TestNet);

        Console.WriteLine(mainNetAddress); // 1PUYsjwfNmX64wS368ZR5FMouTtUmvtmTY
        Console.WriteLine(testNetAddress); // n3zWAo2eBnxLr3ueohXnuAa8mTVBhxmPhq

        Console.ReadLine();
    }
    catch (Exception)
    {    
        throw;
    }   
}

但是在第一行,我收到了这个错误:

您必须在生成随机数之前设置 RNG (RandomUtils.Random)

4

1 回答 1

4

您需要初始化加密库将使用的随机数生成器。对于测试,您可以使用UnsecureRandom您正在使用的 NBitcoin 库附带的类。

RandomUtils.Random = new UnsecureRandom(); // set the random number generator.
Key privateKey = new Key(); // generate a random private key
于 2017-07-28T20:26:13.643 回答