AES Decryption: if the decrypted file has a typo the Application crashes

43 views Asked by At

So I recently added encryption to my application. Every time I start the application, the program checks if there are file to encrypt in a certain directory, encrypts them and then moves them to another folder. Then it decrypts all the files in said folder, deserializes them and shows them to the user. My problem is that if any of the files to encrypt has a typo, on deserialization it fails, and instead of logging the error and moving on it gives me the error 'Padding is invalid and cannot be removed.' and then crashes.

My encryption:


                    DirectoryInfo d = new DirectoryInfo(dir);
                    FileInfo[] Files = d.GetFiles("*.xml");
                    if(Files.Count() > 0)
                    {
                        foreach (FileInfo f in Files)
                        {
                            using (FileStream fsInput = new FileStream(f.FullName, FileMode.Open))
                            {
                                var newFile = dir + "\\" + f.Name;
                                newFile = newFile.Replace("\\Decrypted", "");
                                using (FileStream fsOutput = new FileStream(newFile, FileMode.Create))
                                {
                                    // Perform encryption
                                    Aes.Padding = PaddingMode.PKCS7;
                                    ICryptoTransform encryptor = Aes.CreateEncryptor();
                                    using (CryptoStream cs = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write))
                                    {
                                        try
                                        {
                                            fsInput.CopyTo(cs);
                                        }
                                        catch (Exception e)
                                        {
                                            _logger.Warn(e);
                                        }
                                    }
                                }
                            }
                        }
                    }

My Decryption:


            using FileStream myFile = new FileStream(path, FileMode.Open);
            {
                using (AesManaged aes = new AesManaged())
                {
                    aes.Padding = PaddingMode.PKCS7;
                    aes.Key = _dec.GetKey();
                    aes.IV = _dec.GetIv();
                    ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
                    using (CryptoStream cs = new CryptoStream(myFile, decryptor, CryptoStreamMode.Read))
                        try
                        {
                            return Deserialize<T>(cs);
                        }
                        catch (Exception e)
                        {
                            _logger.Warn(e, "Reading XML file {0} failed", path);
                            return default;
                        }
                }
            }

The error is showed on the 'return default;' in the try-catch.

I have tried specifying the padding in multiple points of the process adding the CryptoStream FlushLastBlock and other possible fixes I found in other posts but nothing works.

0

There are 0 answers