Encrypted Querystring

Hello friends When we are passing data between two asp.net pages we will use querystring. But the original value will be displayed there which is not a secure way to do. So we must encrypt that information.

I faced the same situation and searched GOOGLE and come across a nice article by Mr.Tiberius OsBurn of DEVCITY. The original article you can get at: http://www.devcity.net/PrintArticle.aspx?ArticleID=47.

Basically I am C# guy. I made some changes to that program and its working fine now.

First we will write a class file where Encrypt and Decrypt functions will be there.By using them we will execute that.

1) Create a class file in APP_CODE folder and paste this code.

Code Begins

using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Security.Cryptography;

public class Encryption64
{
private byte[] key = { };
private byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };

public string Decrypt(string stringToDecrypt, string sEncryptionKey)
{
byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
try
{

//key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 8));
key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey.ToCharArray(), 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 e)
{
return e.Message;
}
}

public string Encrypt(string stringToEncrypt, string SEncryptionKey)
{

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

}

Code Ends

Take an aspx page and write the code and write the code below.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;

public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
public string encryptQueryString(string strQueryString)
{
//ExtractAndSerialize.Encryption64 oES =
//    new ExtractAndSerialize.Encryption64();
Encryption64 oES = new Encryption64();
return oES.Encrypt(strQueryString, “!#$a54?3”);
}

public string decryptQueryString(string strQueryString)
{

Encryption64 oES = new Encryption64();
return oES.Decrypt(strQueryString, “!#$a54?3”);
}

protected void lnk_btn_Click(object sender, EventArgs e)
{
string strValues = “search”;
string strURL = “http://yoursite.com?search=”
+ encryptQueryString(strValues);

Response.Redirect(strURL);
}
}

In aspx

Take a link button with Id=”lnk_btn”.

when you click it will encrypt an you can decrypt by using the class file.

Bye

Advertisement