【发布时间】:2020-01-12 08:16:59
【问题描述】:
我尝试使用 myCert.pfx 文件的私钥和公钥对我的签名数据进行签名和验证。但是在签署数据时,我得到“指定的算法无效”。异常
我们使用的.Net框架是4.5,代码如下
public static void CallMainMethod()
{
string str = "Sign and verify the data";
X509Certificate2 certificate = LoadPrivateKey();
byte[] hashBytes = GetDataHash(str);
byte[] signature = GetDigitalSignature(hashBytes);
}
private static X509Certificate2 LoadPrivateKey()
{
return new X509Certificate2(@"d:\Keys\myCert.pfx", "Pass#@123");
}
private static byte[] GetDataHash(string sampleData)
{
//choose any hash algorithm
SHA256Managed managedHash = new SHA256Managed();
return managedHash.ComputeHash(Encoding.Unicode.GetBytes(sampleData));
}
private static byte[] GetDigitalSignature(byte[] data)
{
X509Certificate2 certificate = LoadPrivateKey();
RSACryptoServiceProvider provider = (RSACryptoServiceProvider)certificate.PrivateKey;
return provider.SignHash(data, "SHA256");
}
【问题讨论】:
标签: c# x509certificate sha256 x509certificate2 pfx