ArgumentException“值无效”调用 RSACryptoServiceProvider.SignData 时
使用以下代码调用 RSACryptoServiceProvider.SignData 时,出现 System.ArgumentException“值无效”:
var csp = (RSACryptoServiceProvider)_certificate.PrivateKey;
string simpleName = CryptoConfig.MapNameToOID("SHA256");
return csp.SignData(data, simpleName);
该代码来自第三方示例,因此它应该在正确的情况下工作。看来这可能是由我的操作系统引起的,但我还没有找到解决方案,所以我想在这里发帖看看是否有人可以提供帮助?
.NET框架:3.5 操作系统:Microsoft Windows Server 2003 R2 企业版 Service Pack 2
I'm getting a System.ArgumentException "Value was invalid" when calling RSACryptoServiceProvider.SignData with the following code:
var csp = (RSACryptoServiceProvider)_certificate.PrivateKey;
string simpleName = CryptoConfig.MapNameToOID("SHA256");
return csp.SignData(data, simpleName);
The code is from a third party sample so it should work in the right circumstances. It seems it could be caused by my OS but I haven't found a solution so thought I'd post here to see if someone can help?
.NET Framework: 3.5
OS: Microsoft Windows Server 2003 R2 Enterprise Edition Service Pack 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过更多的挖掘后,我设法找到了比编辑 machine.config 文件更合适的解决方案。您可以为自己感兴趣的算法指定 OID,然后将该值传递给 RSA 提供程序,如下所示:
After doing more digging around, I've manged to find a more suitable solution than editing the machine.config file. You can specify the OID for the algorithm that you're interested in using yourself, and then pass that value to the RSA provider like so:
从提供的链接来看:
“此问题的根本原因是 CryptoConfig 不理解 SHA256CryptoServiceProvider。它是作为 .NET 3.5 中绿色位的一部分添加的,并且由于分层限制,红色位(例如 mscorlib.dll,其中 RSACryptoServiceProvider生活)不知道它的存在...
如果您使用的是.Net Framework 4.0,则解决方案是修改“machine.config”文件:
如果您使用的是.Net Framework 3.5,则解决方案是修改“ machine.config” 文件:
以下是您需要在“machine.config”文件中创建的条目,以支持 SHA256CryptoServiceProvider、SHA256Cng、SHA384CryptoServiceProvider、SHA384Cng、SHA512CryptoServiceProvider 和 SHA512Cng。
链接:http://blogs.msdn.com/b/winsdk/archive/2010/08/18/getting-a-system-argumentexception-value-was-invalid -当尝试使用-sha256cryptoserviceprovider.aspx签署数据时
From the link provided:
"The root cause for this is that the CryptoConfig does not understand SHA256CryptoServiceProvider. It was added as part of the green bits in .NET 3.5, and due to layering restrictions the red bits (such as mscorlib.dll where RSACryptoServiceProvider lives) does not know about its existence...
If you are using .Net Framework 4.0 then the resolution is to modify the “machine.config” file at:
If you are using .Net Framework 3.5 then the resolution is to modify the “machine.config” file at:
Here is the entry that you need to make at the "machine.config" file for supporting SHA256CryptoServiceProvider, SHA256Cng, SHA384CryptoServiceProvider, SHA384Cng, SHA512CryptoServiceProvider, and SHA512Cng."
Link: http://blogs.msdn.com/b/winsdk/archive/2010/08/18/getting-a-system-argumentexception-value-was-invalid-when-trying-to-sign-data-using-sha256cryptoserviceprovider.aspx