PKCS7 padding is invalid and cannot be removed
Hi All,
I get this Cryptography exception while decrypting a querystring.
Any Help in this will be appreciated.
Thanks
Here is my code for encryption/decryption.
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace lweb
{
///
/// Summary description for Encryption.
///
public class Encryption
{
byte [] key;
byte [] IV;
byte [] inputByteArray;
RijndaelManaged myR = new RijndaelManaged();
public Encryption()
{
myR.GenerateKey();
myR.GenerateIV();
key = myR.Key;
IV = myR.IV;
}
public string Decrypt(string stringToDecrypt)
{
inputByteArray = new byte[stringToDecrypt.Length];
ICryptoTransform decryptor = myR.CreateDecryptor(key, IV);
try
{
inputByteArray = Convert.FromBase64String(stringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
ASCIIEncoding encoding = new ASCIIEncoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception ex)
{
throw ex;
}
}
public string Encrypt(string stringToEncrypt)
{
inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
ICryptoTransform encryptor = myR.CreateEncryptor(key, IV);
try
{
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(System.Exception ex)
{
throw ex;
}
}
}
}
I am using this for Encryption, which works fine
string userModURL = "updateProfile.aspx?FN=";
string test = oES.Encrypt(fn.Text.ToString());
userModURL = userModURL + test;
Decryption which doesnt works
string ff = Request.QueryString["FN"];
string dd = ff.Replace(" ", "+");
string ee = oES.Decrypt(dd);
The decrypt page works fine within the same page, i.e., if i encrypt and decrypt the strings on the same page.