Can RSACryptoServiceProvider (.NET's RSA) use SHA256 for encryption (not signing) instead of SHA1?

前端 未结 6 1576
渐次进展
渐次进展 2020-12-15 08:11

When encrypting, can RSACryptoServiceProvider (or any other RSA encryptor available from .NET) use SHA256 instead of SHA1?

SHA1 appears to be hard coded with no way

6条回答
  •  太阳男子
    2020-12-15 08:32

    As of .NET 3.5 SP1 on any Windows Server 2003 and higher OS, yes, the RSACryptoServiceProvider does support RSA-SHA256 for signing, but not encrypting.

    From the blog post Using RSACryptoServiceProvider for RSA-SHA256 signatures:

    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");
        }
    }
    

    You should read the original post though, as there are some gotcha's to be aware of.

提交回复
热议问题