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); [B]//Debugger shows error is here[/B]

            return Encoding.UTF8.GetString(plainText);

        }

I would appreciate some help,
Thanks,
Perry

I have the same problem!!

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

Thanks!

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...

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.

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.

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

        }

Edited 5 Years Ago by tsunamy_boy: n/a

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

This article has been dead for over six months. Start a new discussion instead.