Lasts 16 bytes of Rijndael encryption aren't correct

142 Views Asked by At

I need help with encryption, I have an encrypted file and a program to decrypt it, what I'm trying to do is to encrypt it again from its decrypted version.

So I pretty much reverse what happens in the decrypt part, and it works well EXCEPT the last 16 bytes. I dont know why but they are the only bytes that differ between the original encrypted file and the reproducted encrypted file. I tried to show stuff related to it in the console and everything is the same...

When I try to decrypt the reproducted encrypted file I get this Exception : System.Security.Cryptography.CryptographicException : 'Padding is invalid and cannot be removed.' but the padding are the same...

And I dont know a lot about C# it's one of the first time I use it

This is what I use to encrypt

public static byte[] DeriveKeyInitVectorEncrypt(string password, byte[] salt, byte[] decrypted_data)
        {
            byte[] iv = new byte[0x10];
            byte[] key;
            Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, salt);
            var k = keyGenerator.GetBytes(8);
            key = keyGenerator.GetBytes(0x20);
            Array.Copy(k, 0, iv, 0, 8);
            Array.Copy(salt, 8, iv, 8, 8);
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(key, iv))
            {
                using (MemoryStream memoryStream = new MemoryStream(decrypted_data))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Read))
                    {
                        Console.WriteLine("\n///// ENCRYPT /////");
                        Console.WriteLine("Block Size : " + Convert.ToString(rijndaelCipher.BlockSize));
                        Console.WriteLine("FeedbackSize : " + Convert.ToString(rijndaelCipher.FeedbackSize));
                        Console.Write("IV : ");
                        ByteArrayToString(iv);
                        Console.Write("Key : ");
                        ByteArrayToString(key);
                        Console.WriteLine("Mode : " + rijndaelCipher.Mode);
                        Console.WriteLine("Padding : " + rijndaelCipher.Padding);
                        byte[] plainText = new byte[decrypted_data.Length];         
                        int encryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
                        return plainText;
                    }
                }
            }
        }

And this is what I use to decrypt

public static byte[] DeriveKeyInitVectorDecrypt(string password, byte[] salt, byte[] encryptedData)
        {
            byte[] iv = new byte[0x10];
            byte[] key;
            Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, salt);
            var k = keyGenerator.GetBytes(8);
            key = keyGenerator.GetBytes(0x20);
            Array.Copy(k, 0, iv, 0, 8);
            Array.Copy(salt, 8, iv, 8, 8);
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Key = key;
            rijndaelCipher.IV = iv;
            using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(key, iv))
            {
                using (MemoryStream memoryStream = new MemoryStream(encryptedData))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        Console.WriteLine("///// DECRYPT /////");
                        Console.WriteLine("Block Size : " + Convert.ToString(rijndaelCipher.BlockSize));
                        Console.WriteLine("FeedbackSize : " + Convert.ToString(rijndaelCipher.FeedbackSize));
                        Console.Write("IV : ");
                        ByteArrayToString(iv);
                        Console.Write("Key : ");
                        ByteArrayToString(key);
                        Console.WriteLine("Mode : " + rijndaelCipher.Mode);
                        Console.WriteLine("Padding : " + rijndaelCipher.Padding);
                        byte[] plainText = new byte[encryptedData.Length];
                        int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
                        return plainText
                        //byte[] decrypted_salt = new byte[48];
                        //Array.Copy(plainText, decrypted_salt, 48);
                        //return decrypted_salt;
                    }
                }
            }
        }

Console output hex of the last 16 bytes of the encrypted file

0

There are 0 best solutions below