这是我的代码。
X509Certificate pXCert = new X509Certificate2(@"keyStore.p12", "password");
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)pXCert.PrivateKey;
string id = CryptoConfig.MapNameToOID("SHA256");
return csp.SignData(File.ReadAllBytes(filePath), id);
在最后一行我得到了异常:
System.Security.Cryptography.CryptographicException "Invalid algorithm specified."
我做错了什么?
更新:
id = 2.16.840.1.101.3.4.2.1
最佳答案
对于 dot net framework 4.7.0 或更高版本不采用 sha1,因此在应用程序启动时配置以下内容。对我来说效果很好。
AppContext.SetSwitch("Switch.System.Security.Cryptography.Xml.UseInsecureHashAlgorithms", true);
AppContext.SetSwitch("Switch.System.Security.Cryptography.Pkcs.UseInsecureHashAlgorithms", true);
关于c# - 为什么我得到 "Invalid algorithm specified"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7433074/