开发者

ArgumentException "Value was invalid" when calling RSACryptoServiceProvider.SignData

开发者 https://www.devze.com 2023-02-14 08:49 出处:网络
I\'m getting a System.ArgumentException \"Value was invalid\" when calling RSACryptoServiceProvider.SignData with the following code:

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 h开发者_JS百科elp?

.NET Framework: 3.5 OS: Microsoft Windows Server 2003 R2 Enterprise Edition Service Pack 2


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:

public static string Sign(string data)
{   
    RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(); 
    HashAlgorithm algorithm = new SHA256CryptoServiceProvider();
    const string sha256Oid = "2.16.840.1.101.3.4.2.1";

    byte[] dataBytes = Encoding.ASCII.GetBytes(data);
    byte[] hashBytes= algorithm.ComputeHash(dataBytes);
    byte[] signedBytes = rsaProvider.SignHash(hashBytes, sha256Oid);
    string signature = Convert.ToBase64String(signedBytes);

    return signature;
}


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:

%WINDIR%\Microsoft.NET\Framework\v4.0.xxxxx\ CONFIG  -> for x86
%WINDIR%\Microsoft.NET\Framework64\v4.0.xxxxx\CONFIG -> for x64

If you are using .Net Framework 3.5 then the resolution is to modify the “machine.config” file at:

%WINDIR%\Microsoft.NET\Framework\v2.0.xxxxx\ CONFIG  -> for x86
%WINDIR%\Microsoft.NET\Framework64\v2.0.xxxxx\CONFIG -> for x64

Here is the entry that you need to make at the "machine.config" file for supporting SHA256CryptoServiceProvider, SHA256Cng, SHA384CryptoServiceProvider, SHA384Cng, SHA512CryptoServiceProvider, and SHA512Cng."

<mscorlib>
    <cryptographySettings>
      <cryptoNameMapping>
        <cryptoClasses>
          <cryptoClass SHA256CSP="System.Security.Cryptography.SHA256CryptoServiceProvider, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          <cryptoClass SHA256CNG="System.Security.Cryptography.SHA256Cng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          <cryptoClass SHA384CSP="System.Security.Cryptography.SHA384CryptoServiceProvider, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          <cryptoClass SHA384CNG="System.Security.Cryptography.SHA384Cng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          <cryptoClass SHA512CSP="System.Security.Cryptography.SHA512CryptoServiceProvider, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          <cryptoClass SHA512CNG="System.Security.Cryptography.SHA512Cng, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </cryptoClasses>
        <nameEntry name="SHA256" class="SHA256CSP" />
        <nameEntry name="SHA256CryptoServiceProvider" class="SHA256CSP" />
        <nameEntry name="System.Security.Cryptography.SHA256CryptoServiceProvider" class="SHA256CSP" />
        <nameEntry name="SHA256Next" class="SHA256CNG" />
        <nameEntry name="SHA256Cng" class="SHA256CNG" />
        <nameEntry name="System.Security.Cryptography.SHA256Cng" class="SHA256CNG" />
        <nameEntry name="SHA384" class="SHA384CSP" />
        <nameEntry name="SHA384CryptoServiceProvider" class="SHA384CSP" />
        <nameEntry name="System.Security.Cryptography.SHA384CryptoServiceProvider" class="SHA384CSP" />
        <nameEntry name="SHA384Next" class="SHA384CNG" />
        <nameEntry name="SHA384Cng" class="SHA384CNG" />
        <nameEntry name="System.Security.Cryptography.SHA384Cng" class="SHA384CNG" />
        <nameEntry name="SHA512" class="SHA512CSP" />
        <nameEntry name="SHA512CryptoServiceProvider" class="SHA512CSP" />
        <nameEntry name="System.Security.Cryptography.SHA512CryptoServiceProvider" class="SHA512CSP" />
        <nameEntry name="SHA512Next" class="SHA512CNG" />
        <nameEntry name="SHA512Cng" class="SHA512CNG" />
        <nameEntry name="System.Security.Cryptography.SHA512Cng" class="SHA512CNG" />
      </cryptoNameMapping>
      <oidMap>
        <oidEntry OID="2.16.840.1.101.3.4.2.1" name="SHA256" />
        <oidEntry OID="2.16.840.1.101.3.4.2.1" name="SHA256Next" />
        <oidEntry OID="2.16.840.1.101.3.4.2.2" name="SHA384" />
        <oidEntry OID="2.16.840.1.101.3.4.2.2" name="SHA384Next" />
        <oidEntry OID="2.16.840.1.101.3.4.2.3" name="SHA512" />
        <oidEntry OID="2.16.840.1.101.3.4.2.3" name="SHA512Next" />
      </oidMap>
    </cryptographySettings>
  </mscorlib> 

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

0

精彩评论

暂无评论...
验证码 换一张
取 消