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
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