asp.net encrypting querystring in the URL
I didnt find a c# version to encrypt query strings in the url, please find the same below..
use System.Security.Cryptography;
System.IO;
System.Text; name spaces
public class QueryStringEncryption
{
byte[] key = { };
byte[] IV = { 0×12, 0×34, 0×56, 0×78, 0×90, 0xAB,0xCD, 0xEF};
public QueryStringEncryption()
{
//
// TODO: Add constructor logic here
//
}
public string Encrypt(string stringToEncrypt, string SEncryptionKey)
{
try
{
key = System.Text.Encoding.UTF8.GetBytes(SEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message.ToString();
}
finally
{
}
}
public string Decrypt(string stringToDecrypt, string SEncryptionKey)
{
byte[] inputByteArray = new byte[stringToDecrypt.Length];
// inputByteArray.Length = stringToDecrypt.Length;
// inputByteArray.Length = stringToDecrypt.Length;
try
{
key = System.Text.Encoding.UTF8.GetBytes(SEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,des.CreateDecryptor(key,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 ex)
{
return ex.Message.ToString();
}
finally
{
}
}
}
Posted: July 7th, 2008 under BizTalk.
Tags: asp.net, c#, encryption, query string
Comment from Angshujit
Time August 12, 2008 at 12:22 pm
Hi
I have tried the above code, and found a problem -
The encrypted value of 4 is S91QD+K0/u8=. When I pass this value to another page and read it by Request.QueryString it read as S91QD K0/u8= (without + sign). For that in the time of decryption it is showing an error.
How to solve the problem ?