Can anyone help me on the below code, error is padding is invalid and cannot be removed. I am searching all over SO and applied all their suggestions, but still padding is invalid. I am using NET-2.0
Below is my code
static public String AES_encrypt(String Input)
{
string AES_Key = "PSVJQRk9QTEpNVU1DWUZCRVFGV1VVT0ZOV1RRU1NaWQ=";
string AES_IV = "YWlFLVEZZUFNaWlhPQ01ZT02qWU5HTFJQVFNCRUJZVA=";
var aes = new RijndaelManaged();
aes.KeySize = 256;
aes.BlockSize = 256;
aes.Padding = PaddingMode.PKCS7;
aes.Key = Convert.FromBase64String(AES_Key);
aes.IV = Convert.FromBase64String(AES_IV);
var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
byte[] xBuff = null;
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
{
byte[] xXml = Encoding.UTF8.GetBytes(Input);
cs.Write(xXml, 0, xXml.Length);
}
xBuff = ms.ToArray();
}
String Output = Convert.ToBase64String(xBuff);
return Output;
}
static public String AES_decrypt(String Input)
{
string AES_Key = "PSVJQRk9QTEpNVU1DWUZCrOFGV1VVT0ZOV1RRU1NaWQ=";
string AES_IV = "YWlFLVEZZUFNaWlhPQ01ZT02qWU5HTFJQVFNCRUJZVA=";
RijndaelManaged aes = new RijndaelManaged();
aes.KeySize = 256;
aes.BlockSize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = Convert.FromBase64String(AES_Key);
aes.IV = Convert.FromBase64String(AES_IV);
var decrypt = aes.CreateDecryptor();
byte[] xBuff = null;
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
{
byte[] xXml = Convert.FromBase64String(Input);
cs.Write(xXml, 0, xXml.Length);
}
xBuff = ms.ToArray();
}
String Output = Encoding.UTF8.GetString(xBuff);
return Output;
}
I am thinking the issues here is the AES_encrypt, because if I passed an encrypted string from PHP to my project, AES_decrypt will successfully decrypt it. However, whenever I use AES_encrypt, AES_decrypt is throwing error.
Any help would be greatly appreciated.