這個異常通常發生在以下幾種情況:
1.密文損壞:密文在傳輸或存儲過程中被篡改或損壞。
2.密鑰不匹配:用于解密的密鑰與加密時使用的密鑰不同。
3.填充模式不匹配:加密時使用的填充模式與解密時指定的填充模式不一致。
4.使用了不正確的IV(如果加密模式需要IV的話):雖然您的代碼中沒有設置IV(在ECB模式下這是允許的,但ECB不安全),但如果您在其他地方加密了數據并且使用了IV,那么在解密時也必須提供相同的IV。
由于您使用的是ECB模式,我們可以排除IV不匹配的問題。但是,其他三個原因仍然可能是問題的根源
1.會拋異常
public bool Decrypt(byte[] cipherText, byte[] Key, ref string ret){if (cipherText == null || cipherText.Length <= 0)throw new ArgumentNullException(nameof(cipherText));if (Key == null || Key.Length <= 0)throw new ArgumentNullException(nameof(Key));string plaintext = null;bool success = false;using (Aes aesAlg = Aes.Create()){aesAlg.Key = Key;// 不設置IV(不推薦)// aesAlg.IV = ...; // 不使用IVaesAlg.Mode = CipherMode.ECB; // 使用ECB模式(不推薦,因為它不安全)aesAlg.Padding = PaddingMode.PKCS7;ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, null); // 傳入null作為IVusing (MemoryStream msDecrypt = new MemoryStream(cipherText))using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))using (StreamReader srDecrypt = new StreamReader(csDecrypt)){try{plaintext = srDecrypt.ReadToEnd(); // 行1,嘗試讀取數據ret = plaintext;success = true;}catch (CryptographicException ex){// 處理解密時可能遇到的加密異常Console.WriteLine("解密時發生錯誤: " + ex.Message);//throw; // 可以選擇重新拋出異常,或者根據需要處理return false;}catch (FormatException ex){// 處理可能的格式異常,例如字符編碼問題Console.WriteLine("數據格式錯誤: " + ex.Message);//throw;return false;}catch (Exception ex){// 處理其他任何可能的異常Console.WriteLine("發生未知錯誤: " + ex.Message);//throw;return false;}}}return success;}
- 修正后
public bool Decrypt(byte[] cipherText, byte[] key, ref string ret)
{// 參數驗證if (cipherText == null || cipherText.Length <= 0)throw new ArgumentNullException(nameof(cipherText), "Cipher text cannot be null or empty.");if (key == null || key.Length <= 0)throw new ArgumentNullException(nameof(key), "Key cannot be null or empty.");string plaintext = null;bool success = false;try{using (Aes aesAlg = Aes.Create()){// 設置密鑰(注意:這里假設密鑰長度是有效的AES密鑰長度)aesAlg.Key = key;// 不設置IV(不推薦,因為ECB模式本身就不安全,而且不使用IV進一步降低了安全性)// aesAlg.IV = ...; // 如果使用其他模式,如CBC,則需要設置IV// 使用ECB模式(不推薦,因為它不安全)aesAlg.Mode = CipherMode.ECB;aesAlg.Padding = PaddingMode.PKCS7;// 創建解密器(不傳入IV,因為我們沒有設置它)ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, null);using (MemoryStream msDecrypt = new MemoryStream(cipherText))using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))using (StreamReader srDecrypt = new StreamReader(csDecrypt, Encoding.UTF8)) // 假設加密時使用了UTF8編碼{// 讀取解密后的文本plaintext = srDecrypt.ReadToEnd();ret = plaintext;success = true;}}}catch (CryptographicException ex){// 處理解密時可能遇到的加密異常(例如,密鑰不匹配或數據損壞)Console.WriteLine("Decryption failed: " + ex.Message);// 可以選擇記錄日志、拋出更具體的異常或采取其他恢復措施}catch (FormatException ex){// 處理可能的格式異常(例如,如果解密后的數據不是有效的文本格式)// 注意:這通常發生在StreamReader嘗試讀取非文本數據時Console.WriteLine("Format error: " + ex.Message);// 同樣,可以選擇記錄日志、拋出異常或采取其他措施}catch (Exception ex){// 處理其他任何可能的異常(例如,資源訪問問題或內部錯誤)Console.WriteLine("An unexpected error occurred: " + ex.Message);// 記錄日志、拋出異常或采取其他適當的錯誤處理措施}return success;
}