1. 程式人生 > >解決方案:System.InvalidOperationException: 此實現不是 Windows 平臺 FIPS 驗證的加密算法的一部分。

解決方案:System.InvalidOperationException: 此實現不是 Windows 平臺 FIPS 驗證的加密算法的一部分。

lsa ont static for ret md5 orm 能開 dialog

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() != "0") { MessageBoxButtons mbs = MessageBoxButtons.OKCancel; DialogResult dre = MessageBox.Show("報名系統運行時發生錯誤,是否嘗試修復(會更改註冊表項目)?", "提示", mbs); if (dre == DialogResult.OK) { FipsAlgorithmPolicy.SetValue(k, 0); } 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; }

解決方案:System.InvalidOperationException: 此實現不是 Windows 平臺 FIPS 驗證的加密算法的一部分。