Encrypted querystring in ASP.NET

9 Comments


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

Erase all textboxes contents after submitting data in contentplace holder using Masterpages

Leave a comment


Hello When we have some 30 textboxes in a page where it has a master Page. If we want to erase all th data  in textboxes usuallywe will write TextBox1.Text==” “; But it s time consuming so I have the easy way to do it.    wait…….

private void getAllCtl(ControlCollection ctls)
{
foreach (Control c in ctls)
{
if (c is System.Web.UI.WebControls.TextBox)
{
TextBox txt = c as TextBox;
txt.Text = “”;

}
if (c.HasControls())
{
getAllCtl(c.Controls);

}
}
}

In the button code event please write to recognize contentplaceholder in masterpage

ContentPlaceHolder ContentPlaceHolder1=this.Master.FindControl(“ContentPlaceHolder1”) as ContentPlaceHolder;
getAllCtl(ContentPlaceHolder1.Controls);

ASP.Net JavaScript 2-button (OK/Cancel)

Leave a comment


I found something that works for me on the web after much looking.  In Page_Load, just add the 2 lines:

ibtnDelete.Attributes.Add(“language”, “javascript”)

ibtnDelete.Attributes.Add(“OnClick”, “return confirm(‘Are you sure you want to delete this record?’);”)

This will bring up an OK/Cancel box when the button is clicked; if Cancel is then clicked the button’s click event code won’t run.


%d bloggers like this: