954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

AES Encryption/Decryption Padding is invalid error

Hello everyone,
Im developing an application in C# for a school project that encrypts data in text form using a password and vice versa, and i' d need some help with something.

The program encrypts fine taking as input 1)the data (reading it from textbox) and 2)the password, and stores the encrypted data to a text file.

When decrypting it asks for a password. Here is the case: If the password is correct, it decrypts the text file and shows the original data. If the password is wrong, normally the algorithm should work and return wrong data... but in my case, it doesnt.
I get the following error (when wrong pass for decryption) : "Padding is invalid and cannot be removed"

I would need some help, here is the source code for the encrypt/decrypt methods:

public static string EncryptData(string text, string password)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();

            rijndaelCipher.Mode = CipherMode.CBC;

            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 128;

            rijndaelCipher.BlockSize = 128;

            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);

            byte[] keyBytes = new byte[16];

            int len = pwdBytes.Length;

            if (len > keyBytes.Length) len = keyBytes.Length;

            System.Array.Copy(pwdBytes, keyBytes, len);

            rijndaelCipher.Key = keyBytes;

            rijndaelCipher.IV = keyBytes;

            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

            byte[] plainText = Encoding.UTF8.GetBytes(text);

            byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);

            return Convert.ToBase64String(cipherBytes);

        }




        public static string DecryptData(string text, string password)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();

            rijndaelCipher.Mode = CipherMode.CBC;

            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 128;

            rijndaelCipher.BlockSize = 128;

            byte[] encryptedData = Convert.FromBase64String(text);

            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);

            byte[] keyBytes = new byte[16];

            int len = pwdBytes.Length;

            if (len > keyBytes.Length) len = keyBytes.Length;

            System.Array.Copy(pwdBytes, keyBytes, len);

            rijndaelCipher.Key = keyBytes;

            rijndaelCipher.IV = keyBytes;

            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();

            byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); <strong>//Debugger shows error is here</strong>

            return Encoding.UTF8.GetString(plainText);

        }

I would appreciate some help,
Thanks,
Perry

perryleros
Newbie Poster
1 post since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

I have the same problem!!

if You find the solution please report it for me here.

Thanks!

mvpc
Newbie Poster
1 post since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Id have thought you would have to have followed the reverse route of the encryption more when decrypting, it doesnt seem to be in the same order...

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

It might not be the cleanest solution, but error when decrypting is almost always the wrong password. It's not too bad to assume that an error meant the incorrect password was entered (error = alert user wrong password).

I think this is normal based on how the encryption algorithm works.

vckicks
Junior Poster in Training
58 posts since Jun 2008
Reputation Points: 11
Solved Threads: 9
 

like vckicks, I do think that its not uncommon for it to error in someways, as if you think about it, it has expectations if you type in rubbish, how can you expect it to feel thats the same?

For passwords however using a decryptable method is unusual, normally you encrypt using a non decryptable method and then compare the encrypted versions, not the real passwords.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

Use it this way

public static string AES_ECN(string message, string passKey)
        {
            RijndaelManaged aes256 = new RijndaelManaged();

            aes256.KeySize = 256;
            aes256.BlockSize = 256;
            aes256.Padding = PaddingMode.Zeros;
            aes256.Mode = CipherMode.ECB;
            aes256.Key = Encoding.ASCII.GetBytes(passKey);
            aes256.GenerateIV();
       
            ICryptoTransform encryptor = aes256.CreateEncryptor();
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
            StreamWriter mSWriter = new StreamWriter(cs);
            mSWriter.Write(message);
            mSWriter.Flush();
            cs.FlushFinalBlock();
            byte[] cypherTextBytes = ms.ToArray();

            ms.Close();
            return Convert.ToBase64String(cypherTextBytes);
        }

        public static string AES_DEC(string text, string password)
        {
            RijndaelManaged aes256 = new RijndaelManaged();

            aes256.KeySize = 256;
            aes256.BlockSize = 256;
            aes256.Padding = PaddingMode.Zeros;
            aes256.Mode = CipherMode.ECB;
            aes256.Key = Encoding.ASCII.GetBytes(password);
            aes256.GenerateIV();

            byte[] encryptedData = Convert.FromBase64String(text);
            ICryptoTransform transform = aes256.CreateDecryptor();
            byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);

            return Encoding.UTF8.GetString(plainText);

        }
tsunamy_boy
Newbie Poster
1 post since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

Nice one tsunamy_boy. After hours with the padding problem and other code samples, these two straightforward methods worked for me. Thanks.

overcanyon
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You