J
Jon Poploskie
Hello,
I am trying to encrypt/decrypt some text that is being stored in an XML
file. The encryption/decryption works fine when I don't save it to the
file, but I get the following error when I try to decrypt the text that I
stored in the XML file:
PKCS7 padding is invalid and cannot be removed.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.Security.Cryptography.CryptographicException:
PKCS7 padding is invalid and cannot be removed.
Source Error:
Line 64: CryptoStream crypto = new CryptoStream(ms,
sa.CreateDecryptor(key, iv), CryptoStreamMode.Write);
Line 65: crypto.Write(bytes, 0, bytes.Length);
Line 66: crypto.FlushFinalBlock();
Line 67: crypto.Close();
Line 68: return Encoding.Unicode.GetString(ms.ToArray());
I have tried pretty much every available encoding to write the XML file
(using an XmlTextWriter), but nothing seems to be working. I've included
the cryptography functions I'm using, I'd appreciate any thoughts anyone
has. By the way, key is a hardcoded 32 byte key and iv is a hardcoded 16
byte IV.
Thank you!
Jon
public static string EncryptText(string textToEncrypt)
{
if(textToEncrypt == null)
return null;
byte[] bytes = Encoding.Unicode.GetBytes(textToEncrypt);
MemoryStream ms = new MemoryStream();
SymmetricAlgorithm sa = new RijndaelManaged();
CryptoStream crypto = new CryptoStream(ms, sa.CreateEncryptor(key, iv),
CryptoStreamMode.Write);
crypto.Write(bytes, 0, bytes.Length);
crypto.FlushFinalBlock();
crypto.Close();
return Convert.ToBase64String(ms.ToArray());
}
public static string DecodeText(string txtToDecode)
{
if(txtToDecode == null)
return null;
byte[] bytes = Convert.FromBase64String(txtToDecode);
MemoryStream ms = new MemoryStream();
SymmetricAlgorithm sa = new RijndaelManaged();
CryptoStream crypto = new CryptoStream(ms, sa.CreateDecryptor(key, iv),
CryptoStreamMode.Write);
crypto.Write(bytes, 0, bytes.Length);
crypto.FlushFinalBlock();
crypto.Close();
return Encoding.Unicode.GetString(ms.ToArray());
}
I am trying to encrypt/decrypt some text that is being stored in an XML
file. The encryption/decryption works fine when I don't save it to the
file, but I get the following error when I try to decrypt the text that I
stored in the XML file:
PKCS7 padding is invalid and cannot be removed.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.Security.Cryptography.CryptographicException:
PKCS7 padding is invalid and cannot be removed.
Source Error:
Line 64: CryptoStream crypto = new CryptoStream(ms,
sa.CreateDecryptor(key, iv), CryptoStreamMode.Write);
Line 65: crypto.Write(bytes, 0, bytes.Length);
Line 66: crypto.FlushFinalBlock();
Line 67: crypto.Close();
Line 68: return Encoding.Unicode.GetString(ms.ToArray());
I have tried pretty much every available encoding to write the XML file
(using an XmlTextWriter), but nothing seems to be working. I've included
the cryptography functions I'm using, I'd appreciate any thoughts anyone
has. By the way, key is a hardcoded 32 byte key and iv is a hardcoded 16
byte IV.
Thank you!
Jon
public static string EncryptText(string textToEncrypt)
{
if(textToEncrypt == null)
return null;
byte[] bytes = Encoding.Unicode.GetBytes(textToEncrypt);
MemoryStream ms = new MemoryStream();
SymmetricAlgorithm sa = new RijndaelManaged();
CryptoStream crypto = new CryptoStream(ms, sa.CreateEncryptor(key, iv),
CryptoStreamMode.Write);
crypto.Write(bytes, 0, bytes.Length);
crypto.FlushFinalBlock();
crypto.Close();
return Convert.ToBase64String(ms.ToArray());
}
public static string DecodeText(string txtToDecode)
{
if(txtToDecode == null)
return null;
byte[] bytes = Convert.FromBase64String(txtToDecode);
MemoryStream ms = new MemoryStream();
SymmetricAlgorithm sa = new RijndaelManaged();
CryptoStream crypto = new CryptoStream(ms, sa.CreateDecryptor(key, iv),
CryptoStreamMode.Write);
crypto.Write(bytes, 0, bytes.Length);
crypto.FlushFinalBlock();
crypto.Close();
return Encoding.Unicode.GetString(ms.ToArray());
}