SHA256CryptoServiceProvider and related possible to use on WinXP?

旧城冷巷雨未停 提交于 2019-12-05 05:40:28

It seems that MSDN documentation is right in the sense that it should be supported in XP SP3 by design, and if it is not, it's only because of a bug in .NET 3.5.

Both AesCryptoServiceProvider and SHA256CryptoServiceProvider use the same cryptograhics service named "Microsoft Enhanced RSA and AES Cryptographic Provider". Under XP, the name of the service is slightly different: "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)". The constructor of AesCryptoServiceProvider performs a simple check:

string providerName = "Microsoft Enhanced RSA and AES Cryptographic Provider";
if(Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 1)
{
    providerName = "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)";
}

The constructors of SHAxxxCryptoServiceProvider classes do not check the (Prototype) name, and this is why they fail in XP. If they did, they would succeed.

There is a simple workaround on a given PC. Go to registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\Defaults\Provider, find its subkey named "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)", export it to .reg, edit this .reg and delete " (Prototype)" from its name. When you import it back, the original key will be duplicated to the new key without (Prototype), with the same contents. From now on, SHA256CryptoServiceProvider will work on this XPSP3 machine.

I've had success with the following snippet, although I'm not really satisfied with it and nearly posted an SO question concerning the various seemingly haphazard ways to instantiate SHA512 at the time. This is tested on Windows XP, 7, and possibly Vista (can't remember).

using System.Security.Cryptography;

        SHA512 hash;
        try
        {
            hash = new SHA512Cng( );
        }
        catch ( PlatformNotSupportedException )
        {
            hash = SHA512.Create( );
        }

I think this should work the same with SHA256.

Also, comparing the output of both versions with a unix sha2 utility suggested that they both correctly implement SHA512.

From MSDN: SHA256CryptoServiceProvider Class

Platforms: Windows Vista, Windows XP SP2, Windows Server 2003

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!