Padding is invalid and cannot be removed even with the same paramaters

Home / Uncategorized / Padding is invalid and cannot be removed even with the same paramaters

Question:
I have been searching for hours and cannot find a fix that works. I have my encryption setup as so:public String EncryptData(Byte[] data, String password)
{

RijndaelManaged aes = new RijndaelManaged();
aes.Mode = CipherMode.CBC;
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Padding = PaddingMode.PKCS7;
aes.Key = Encoding.UTF8.GetBytes(password);
aes.IV = Encoding.UTF8.GetBytes(password.Substring(0, 16));
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream writer = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
writer.Write(data, 0, data.Length);
writer.FlushFinalBlock();
Byte[] msBytes = ms.ToArray();
String b64encrypted = Convert.ToBase64String(msBytes);
return b64encrypted;
}
}
}

My decryption method:private String decryptData(String password, String data)
{
RijndaelManaged aes = new RijndaelManaged();
aes.Mode = CipherMode.CBC;
aes.BlockSize = 128;
aes.KeySize = 256;
aes.Padding = PaddingMode.PKCS7;
aes.Key = Encoding.UTF8.GetBytes(password);
aes.IV = Encoding.UTF8.GetBytes(password.Substring(0, 16));
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream reader = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(reader))
{
String plaintext = sr.ReadToEnd(); //PADDING IS INVALID AND CANNOT BE REMOVED
return plaintext;
}
}
}
}

I cannot see where I am going wrong, I get the error where it is marked with the comment. I have checked and the input password/iv is the same each time. I have checked and tried all answers I could find to no avail. Any help would be appreciated, thanks.

This isn’t a duplicate as I have tried everything in all threads/questions I could find and nothing has worked for me.


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *