.NET核心-如何用命名曲线验证ECDSA签名



我试图用命名曲线secP256k1和字节数组中的公钥来验证签名。我不知道如何将公钥添加到我的ECDsaCng对象中。数据的散列是SHA256散列。

static byte[] publicKey = new byte[] {
0x04, 0xD3, ..., 0x20
};
public static bool VerifySignature(byte[] hash, byte[] signature)
{
using (ECDsaCng dsa = new ECDsaCng(ECCurve.CreateFromFriendlyName("secP256k1")))
{
// How to add the public key?
bool result = dsa.VerifyHash(hash, signature);
return result;
}
}

我尝试使用ImportSubjectPublicKeyInfo,但我得到了一个异常"ASN1损坏数据">

public static bool VerifySignature(byte[] hash, byte[] signature)
{
using (ECDsaCng dsa = new ECDsaCng(ECCurve.CreateFromFriendlyName("secP256k1")))
{
int bytesRead;
dsa.ImportSubjectPublicKeyInfo(publicKey, out bytesRead);
bool result = dsa.VerifyHash(hash, signature);
return result;
}
}

我希望任何人都有解决问题的想法,或者能给我一个不同的方法。

我的解决方案如下:

public static bool VerifySignature(byte[] hash, byte[] signature)
{
var dsa = ECDsa.Create(new ECParameters
{
Curve = ECCurve.CreateFromFriendlyName("secP256k1"),
Q =
{
X = publicKey.Take(32).ToArray(),
Y = publicKey.Skip(32).ToArray()
}
});
bool result = dsa.VerifyHash(hash, signature);
return result;
}

在ECDsaCng上的示例中,他们有接收器/您的验证

public class Bob
{
public byte[] key;
public void Receive(byte[] data, byte[] signature)
{
using (ECDsaCng ecsdKey = new ECDsaCng(CngKey.Import(key, CngKeyBlobFormat.EccPublicBlob)))
{
if (ecsdKey.VerifyData(data, signature))
Console.WriteLine("Data is good");
else
Console.WriteLine("Data is bad");
}
}
} 

我的假设是;dsa";可以与其他过载一起实例化

CngKey.Import(publicKey, CngKeyBlobFormat.EccPublicBlob)

而不是创建新的曲线。

最新更新