c# - 为什么我得到 "Invalid algorithm specified"异常

标签 c# cryptography

这是我的代码。

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/

相关文章:

c# - 在 C# 中使用多个键优化缓存 - 删除对象的重复

c# - linq搜索法语字符

c# - 创建 ICryptoTransform 时出现 System.Security.CryptpgraphicUnexpectedOperationException

asp.net - bcrypt/Bcrypt.net 的优势和替代方案

security - 非对称和对称加密方法之间的区别?

c# - 部署到 GAC 的 Web 部件不起作用

c# - 相对路径在 Windows 服务中不起作用

javascript - 加载 Javascript 文件的问题

hash - 在 Erlang 中将数据 block 签名为哈希不起作用。基于ec的 key 推导

node.js - PBKDF2 的所有实现都应该从相同的输入生成相同的 key 吗?