The EnCrypt Method works. The Decrypt method thows an error of
Length of the data to decrypt is invalid.
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: Length of the data to decrypt is invalid.
Guys I really need to get this working. Please Help.
Code:/// <summary>
/// Decrypt a string encrypted by the EnCrypt method.
/// </summary>
/// <param name="value">String to DeCrypt.</param>
/// <returns>The Decrypted string.</returns>
public static string DeCrypt(string value)
{
string ret = "";
// char c = new char();
// for(int i = 0; i < value.Length; i++)
// {
// c = System.Convert.ToChar(value.Substring(i, 1));
// ret += System.Convert.ToChar(System.Convert.ToInt32(c) - 5);
// }
byte[] key = new byte[]{
0x01,
0x02,
0x03,
0x03,
0x04,
0x05,
0x05,
0x06,
0x07,
0x08,
0x09,
0x10,
0x11,
0x12,
0x13,
0x14,
0x15,
0x16};
byte[] iv = new byte[]{
0x01,
0x02,
0x03,
0x03,
0x04,
0x05,
0x05,
0x06,
0x07,
0x08,
0x09,
0x10,
0x11,
0x12,
0x13,
0x14,
0x15,
0x16};
byte[] b = ToByteArray(value);
System.IO.MemoryStream s = new System.IO.MemoryStream(b);
System.Security.Cryptography.RijndaelManaged rj = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.ICryptoTransform trans = rj.CreateDecryptor();//key, iv);
System.Security.Cryptography.CryptoStreamMode mode = System.Security.Cryptography.CryptoStreamMode.Read;
System.Security.Cryptography.CryptoStream enc = new System.Security.Cryptography.CryptoStream(s, trans, mode);
System.IO.StreamReader r = new System.IO.StreamReader(enc);
ret = r.ReadToEnd();
return ret;
}
/// <summary>
/// Encrypt a string.
/// </summary>
/// <param name="value">The String to encrypt.</param>
/// <returns>The encrypted string.</returns>
public static string EnCrypt(string value)
{
string ret = "";
// char c = new char();
// for(int i = 0; i < value.Length; i++)
// {
// c = System.Convert.ToChar(value.Substring(i, 1));
// ret += System.Convert.ToChar(System.Convert.ToInt32(c) + 5);
// }
byte[] key = new byte[]{
0x01,
0x02,
0x03,
0x03,
0x04,
0x05,
0x05,
0x06,
0x07,
0x08,
0x09,
0x10,
0x11,
0x12,
0x13,
0x14,
0x15,
0x16};
byte[] iv = new byte[]{
0x01,
0x02,
0x03,
0x03,
0x04,
0x05,
0x05,
0x06,
0x07,
0x08,
0x09,
0x10,
0x11,
0x12,
0x13,
0x14,
0x15,
0x16};
byte[] b = ToByteArray(value);
System.IO.MemoryStream s = new System.IO.MemoryStream(b);
System.Security.Cryptography.RijndaelManaged rj = new System.Security.Cryptography.RijndaelManaged();
System.Security.Cryptography.ICryptoTransform trans = rj.CreateEncryptor();//key, iv);
System.Security.Cryptography.CryptoStreamMode mode = System.Security.Cryptography.CryptoStreamMode.Read;
System.Security.Cryptography.CryptoStream enc = new System.Security.Cryptography.CryptoStream(s, trans, mode);
System.IO.StreamReader r = new System.IO.StreamReader(enc);
ret = r.ReadToEnd();
return ret;
}
