简体   繁体   中英

Padding is invalid and cannot be removed with AES

Can you help please...

I want to encrypt / decrypt the file using Aes. Encryption part is successful working. But my problem is when decrypting the text encrypted error is occurred : Padding is invalid and cannot removed

My error is occurred on line (private void FileDecrypt) :

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

My code :

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);                            
                        }
                    }                    
                }
            }
        } 

Thank you very much for help.

Replace this:

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

With this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM