Monday, April 07, 2008

Encrypt and Decrypt querystring

Please Create a class Encryption as given below
and then

Use the following to Encrypt
============================
Encryption encrypt = new Class.Encryption();
string Url = "http://page.aspx?param1=" + encrypt.EncryptQueryString(parameter_querystring);

Use the following for Decrypt
============================
Encryption Decrypt = new Class.Encryption();
string encryptParam1 = Request.QueryString["param1"];
string param1 = Decrypt .DecryptQueryString(encryptParam1 .Replace(" ", "+"));

Encryption.Cs File
==================
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Security.Cryptography;
namespace Lucas.Class
{
public class Encryption
{
private byte[] key = {};
private byte[] IV = {18, 52, 86, 120, 144, 171, 205, 239};

private string Decrypt(string stringToDecrypt, string sEncryptionKey)
{

byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
try
{
key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 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 e)
{
return e.Message;
}
}

private string Encrypt(string stringToEncrypt, string SEncryptionKey)
{
try
{
key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 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 e)
{
return e.Message;
}
}

private string Left(string MyString,int length)
{
string tmpstr = MyString.Substring(0, length);
return tmpstr;
}

public string EncryptQueryString(string strQueryString)
{
return Encrypt(strQueryString,"!#$a54?3");
}

public string DecryptQueryString(string strQueryString)
{
return Decrypt(strQueryString,"!#$a54?3");
}
}
}

No comments: