使用 AES 解密时填充无效且无法删除

问题描述 投票:0回答:1

从 RijndaelManaged 更新到 AES 后,我的代码停止工作并抛出异常“填充无效且无法删除”。

当我使用 RijndaelManaged 时,我的代码曾经可以工作。

            
            public static string DecryptStringAES(string encryptedText, string key)
            {
                var keybytes = Encoding.UTF8.GetBytes(key);
                var iv = keybytes;

                var encryptStringToBytes = EncryptStringToBytes_Aes(encryptedText, keybytes, iv);

                // Decrypt the bytes to a string.  here encryptStringToBytes is byte[80]
                var roundtrip = DecryptStringFromBytes_Aes(encryptStringToBytes, keybytes, iv);

                //DECRYPT FROM CRIPTOJS
                var encrypted = Convert.FromBase64String(roundtrip);

                //here encryptStringToBytes is byte[48]
                //This Line throws the exception --Padding is invalid and cannot be removed
                var id = DecryptStringFromBytes_Aes(encrypted, keybytes, iv);

                return id;
            }
            
              private static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
            {
                if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText");
                if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV");

                string plaintext = null;

                try
                {
                    using (Aes aesAlg = Aes.Create())
                    {
                        aesAlg.Mode = CipherMode.CFB;
                        aesAlg.Padding = PaddingMode.PKCS7;
                        aesAlg.Key = Key;
                        aesAlg.IV = IV;

                        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                        using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                        {
                            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                            {
                                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                                {
                                    plaintext = srDecrypt.ReadToEnd();
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                    throw;
                }

                return plaintext;
            }
            
             private static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
            {
                if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText");
                if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV");

                byte[] encrypted;

                try
                {
                    using (Aes aesAlg = Aes.Create())
                    {
                        aesAlg.Mode = CipherMode.CFB;
                        aesAlg.Padding = PaddingMode.PKCS7;
                        aesAlg.Key = Key;
                        aesAlg.IV = IV;

                        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                        using (MemoryStream msEncrypt = new MemoryStream())
                        {
                            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                            {
                                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                                {
                                    swEncrypt.Write(plainText);
                                }

                                encrypted = msEncrypt.ToArray();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string er = ex.Message;
                    throw ex;
                }

                return encrypted;
            }

如果你看我的代码,我已经调用了 **DecryptStringFromBytes_Aes ** 方法两次。

当我第一次调用该方法时,第一个参数接收 byte[80] 的 byte[]。这按预期工作。并按我的预期返回字符串。

           ` var roundtrip = DecryptStringFromBytes_Aes(encryptStringToBytes, keybytes, iv);`

在转换我收到的字符串值并将其第二次传递给方法后的下一行。我得到了例外。

我尝试删除 Padding 或 Mode,但这没有帮助。

c# aes rijndaelmanaged
1个回答
0
投票

您需要将

encrypted = msEncrypt.ToArray();
移到
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
之外:

using (MemoryStream msEncrypt = new MemoryStream())
{
    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
    {
        swEncrypt.Write(plainText);
    }

    encrypted = msEncrypt.ToArray();
}

这意味着

CryptoStream
已被刷新(这提示它将最终块写入
MemoryStream
在将 MemoryStream
 复制到 
encrypted
 之前。

© www.soinside.com 2019 - 2024. All rights reserved.