Welcome to MSDN Blogs Sign in | Join | Help

Using RSACryptoServiceProvider for RSA-SHA256 signatures

Earlier this month, we released .NET 3.5 SP 1.  One of the new features available in this update is that RSACryptoServiceProvider has gained the ability to create and verify RSA-SHA256 signatures.

Since RSACryptoServiceProvider relies on the underlying CAPI APIs to do its work, this feature will only be enabled on versions of Windows which support SHA-256 algorithms in CAPI.  At this point, that translates to Windows Server 2003 and higher.

The code to create and verify a signature is basically the same as it was for doing RSA-SHA1:

byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 };

using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())

{

    byte[] signature = rsa.SignData(data, "SHA256");

 

    if (rsa.VerifyData(data, "SHA256", signature))

    {

        Console.WriteLine("RSA-SHA256 signature verified");

    }

    else

    {

        Console.WriteLine("RSA-SHA256 signature failed to verify");

    }

}

The second parameter should be either the string "SHA256", the type of the SHA256Managed object, or an instance of a SHA256Managed object.

Note that this means, somewhat counter-intuitively, that passing either the type of or an instance of the SHA256CryptoServiceProvider object will not work.  If you do use the SHA256CryptoServiceProvider type, you'll end up with an error like this:

Unhandled Exception: System.ArgumentException: Value was invalid.

   at System.Security.Cryptography.Utils.ObjToOidValue(Object hashAlg)

   at System.Security.Cryptography.RSACryptoServiceProvider.SignData(Byte[] buffer, Object halg)

The reason for this is the same reason that 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.

Also note that this functionality was added only to the RSACryptoServiceProvider type, so upstack functionality such as XML digital signatures are not yet enabled for RSA-SHA256 digital signatures.  However, this does provide the base building block for those upstack crypto technologies, so that they can begin adding support in the future.

Published Monday, August 25, 2008 11:09 AM by shawnfa
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Using RSACryptoServiceProvider for RSA-SHA256 signatures

Thursday, April 30, 2009 7:41 PM by Balazs

Could you tell me, why an actually existing private key wont't work with this method (on any current OS)

X509Store _windowsStore = new X509Store( StoreName.My , StoreLocation.CurrentUser );

_windowsStore.Open(OpenFlags.MaxAllowed); // for the sake of testing

X509Certificate2 _cachedCert = _windowsStore.Certificates[0];

RSACryptoServiceProvider _provider = null;

byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 };

// CASE A, I'd like it to work

if (_cachedCert.HasPrivateKey)

{

_provider = (RSACryptoServiceProvider)_cachedCert.PrivateKey;

// This will throw {System.Security.Cryptography.CryptographicException}

// "Invalid algorithm specified.\r\n"

byte[] sigedBytes = _provider.SignData(data, "SHA256");

}

// CASE B, This works, this was your example

_provider = new RSACryptoServiceProvider();

byte[] sigedBytes2 = _provider.SignData(data, "SHA256");

# re: Using RSACryptoServiceProvider for RSA-SHA256 signatures

Tuesday, May 19, 2009 3:19 AM by Ronak jain

We can also print in a message box , why to use console ?

# re: Using RSACryptoServiceProvider for RSA-SHA256 signatures

Thursday, May 21, 2009 5:10 PM by shawnfa

I tend to use the console in my sample code since it doesn't require any additional dependencies.

-Shawn

# re: Using RSACryptoServiceProvider for RSA-SHA256 signatures

Thursday, May 21, 2009 5:16 PM by shawnfa

You need to make sure that the RSA key is stored in the PROV_RSA_AES crypto service provider.  If your certificate is using PROV_RSA_FULL, then that CSP doesn't understand SHA-256, and the signature process won't work.

-Shawn

# re: Using RSACryptoServiceProvider for RSA-SHA256 signatures

Sunday, June 28, 2009 2:43 PM by Pete R

Hi Shawn,

It seems this might still run afoul of this knowledgebase issue with regard to delays incurred while calling the SignData method: http://support.microsoft.com/kb/948080. I need to sign cookies for a very high-traffic website and such delays would be unacceptable. Is there a way to do RSA-SHA256 while still avoiding this issue? Thanks!

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker