querystring encryption problem
Hi there.
I found some code which encrypts things you would like so you can place this encrypted string as a querystring to another page so it can request it and decrypt it and use it for whatever it needs to be used for.
for some reason, it doesnt work 100% of the time.
I am trying to figure out what the problem could be but i cannot seem to find it.
one minute it works, the next it doesnt. The error i get when it doesnt work is the error where it says that the length isnt the correct length for the algorithm. It gets thrown in the decrypt method
Code:
public static string EncryptText(string theTextToEncrypt)
{
return Encrypt(theTextToEncrypt, "&%#@?,:*");
}
public static string DecryptText(string theStringToDecrypt)
{
return Decrypt(theStringToDecrypt, "&%#@?,:*");
}
public static string Encrypt(string theStringToEncrypt, string theStringEncryptionKey)
{
byte[] byKey = {};
byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(theStringEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(theStringToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(Exception e)
{
return e.Message;
}
}
public static string Decrypt(string theStringToDecrypt, string theDecryptionKey)
{
byte[] byKey = {};
byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = {};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(theDecryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(theStringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch(Exception e)
{
return "-1"; //e.Message;
}
}