解决方案:System.InvalidOperationException: 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分。

时间:2023-03-10 00:45:06
解决方案:System.InvalidOperationException: 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分。

System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.

引发该问题的原因是系统启动了FIPS,导致.NET Framework平台中的MD5加密及其他一些加密方法需要调用FIPS验证,但FIPS又不支持这些方法,故引发如上异常。

解决方法:

注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy项目中,将Enabled值设置为0即可

也可以在程序启动时加入检查和修复的代码,如下

        /// <summary>
/// 测试MD5加密可用性
/// </summary>
public static void GeneratingMD5Test()
{
try
{
MD5CryptoServiceProvider get_md5 = new MD5CryptoServiceProvider();
}
catch (InvalidOperationException)
{
CloseFIPS();
}
catch (Exception) { }
} /// <summary>
/// 关闭操作系统FIPS功能(该功能开启会导致.NET Framework中的MD5加密功能出现错误)
/// </summary>
/// <returns></returns>
private static bool CloseFIPS()
{
bool res = false;
try
{
RegistryKey localMachine = Registry.LocalMachine;
RegistryKey FipsAlgorithmPolicy = localMachine.CreateSubKey(@"SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy");
string[] vks = FipsAlgorithmPolicy.GetValueNames();
foreach (string k in vks)
{
if (k.ToUpper() == "ENABLED")
{
if (FipsAlgorithmPolicy.GetValue(k).ToString() != "")
{
MessageBoxButtons mbs = MessageBoxButtons.OKCancel;
DialogResult dre = MessageBox.Show("报名系统运行时发生错误,是否尝试修复(会更改注册表项目)?", "提示", mbs);
if (dre == DialogResult.OK)
{
FipsAlgorithmPolicy.SetValue(k, );
}
break;
}
}
}
FipsAlgorithmPolicy.Close();
localMachine.Close();
res = true;
}
catch (Exception ex)
{
MessageBox.Show(String.Format("修复失败,发生错误:{0}{1}{0}详细情况请查看日志文件",Environment.NewLine,ex.Message), "错误");
LogException(ex);
}
return res;
}