密钥类构造函数NBitcoin错误:EC 密钥的大小应为 32

问题描述

我正在使用 NBitcoin 库、C# 和 Visual Studio 2019。

想法是创建一个哈希集,将比特币地址解码为字节数组,使用具有此签名的 Key 类构造函数

Key(byte[] data,int count = -1,bool fCompressedIn = true)

在我的程序中是这样的:

var key = new Key(byteArray,-1,true)

byteArray 是:

byte[] byteArray = Encoders.base58.DecodeData("15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe");

最后,我将密钥添加到哈希集中,如下所示:

keys.Add(key);

糟糕的是,编译器卡住了

var key = new Key(byteArray,true) 

错误

System.ArgumentException: 'EC 密钥的大小应为 32'

我尝试用 Base32 编码:

byte[] byteArray = Encoders.Base32.EncodeData("15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe"); 

但我收到另一个错误

无法从字符串转换为字节[]

这个问题困扰我很久了,没有任何解决办法...

代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using NBitcoin;
using NBitcoin.DataEncoders;

namespace MyCoins
{
    static class Program
    {        
        static void Main()
        {
            // Create a HashSet:
            var keys = new HashSet<Key>();

            // I get the hash of the address:
            byte[] byteArray = Encoders.base58.DecodeData("1123UAmo4xoNK2oXWfrrdiCwA6kcUQz9aa");

            // I pass the byteArray that I created above
            var key = new Key(byteArray,true);
            // When I do this,I get the error: 'The size of an EC key should be 32 Parameter name: data'
            
            // Add the key to the HashSet:
            keys.Add(key);
         }
     }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)