解決方案: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() != "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;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM