【问题标题】:Padding is invalid and cannot be removed with AES填充无效,无法使用 AES 删除
【发布时间】:2020-12-26 13:48:11
【问题描述】:

你能帮忙吗...

我想使用 Aes 加密/解密文件。加密部分成功运行。但我的问题是在解密文本时发生加密错误:Padding is invalid and cannot remove

我的错误是在线发生的(private void FileDecrypt):

while ((read = cryptoStream.Read(buffer, 0, buffer.Length)) > 0)

我的代码:

private void FileEncrypt(string inputFile, string outputFile, string password)
        {
            byte[] salt = GenerateSalt();
            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            RijndaelManaged AES = new RijndaelManaged();
            AES.KeySize = 256;//AES 256 bits
            AES.BlockSize = 128;//AES 128 bits
            AES.Padding = PaddingMode.PKCS7;
            //AES.Padding = PaddingMode.Zeros;            
            var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
            AES.Key = key.GetBytes(AES.KeySize / 8);
            AES.IV = key.GetBytes(AES.BlockSize / 8);            
            AES.Mode = CipherMode.CFB;            
            
            using (FileStream filestreamCrypt = new FileStream(outputFile, FileMode.Create))
            {
                filestreamCrypt.Write(salt, 0, salt.Length);
                using (CryptoStream cs = new CryptoStream(filestreamCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    using (FileStream filestreamIn = new FileStream(inputFile, FileMode.Open))
                    {
                        int readLength = (int)filestreamIn.Length;
                        byte[] buffer = new byte[readLength];                        
                        int read;
                        while ((read = filestreamIn.Read(buffer, 0, buffer.Length)) > 0)
                        {                            
                            cs.Write(buffer, 0, read);
                            cs.FlushFinalBlock();
                        }
                    }
                }
                filestreamCrypt.Dispose();
            }
        }

        private void FileDecrypt(string inputFileName, string outputFileName, string password)
        {            
            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);            

            byte[] salt = new byte[32];
            using (FileStream filestreamCrypt = new FileStream(inputFileName, FileMode.Open))
            {
                filestreamCrypt.Read(salt, 0, salt.Length);
                RijndaelManaged AES = new RijndaelManaged();
                AES.KeySize = 256;//AES 256 bits
                AES.BlockSize = 128;//AES 128 bits                
                var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
                AES.Key = key.GetBytes(AES.KeySize / 8);
                AES.IV = key.GetBytes(AES.BlockSize / 8);
                AES.Padding = PaddingMode.PKCS7;
                //AES.Padding = PaddingMode.Zeros;
                AES.Mode = CipherMode.CFB;
                using (CryptoStream cryptoStream = new CryptoStream(filestreamCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read))
                {
                    using (FileStream filestreamOut = new FileStream(outputFileName, FileMode.Create))
                    {
                        int read;
                        int readLength = (int)filestreamCrypt.Length;
                        byte[] buffer = new byte[readLength];                        
                        //var fullCipher = Convert.FromBase64String(filestreamOut.ToString());
                        while ((read = cryptoStream.Read(buffer, 0, buffer.Length)) > 0) **//ERROR**
                        {
                            filestreamOut.Write(buffer, 0, read);                            
                        }
                    }                    
                }
            }
        } 

非常感谢您的帮助。

【问题讨论】:

    标签: c# encryption aes padding


    【解决方案1】:

    替换这个:

    while ((read = filestreamIn.Read(buffer, 0, buffer.Length)) > 0)
    {                            
        cs.Write(buffer, 0, read);
        cs.FlushFinalBlock();  <--- Flush final block every time around loop !
    }
    

    有了这个:

    while ((read = filestreamIn.Read(buffer, 0, buffer.Length)) > 0)
    {                            
        cs.Write(buffer, 0, read);
    }
    cs.FlushFinalBlock();  
    

    【讨论】:

    • 谢谢你的帮助 它第一次工作,但它不能工作......我必须停止我的程序并重新加载!谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多