.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Encryption & Decryption >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Encryption and Decryption With Querystring using ASP.NET

Posted By:Athira Sreejith       Posted Date: April 03, 2009    Points: 10    Category: Encryption & Decryption    URL: http://www.dotnetspark.com  
 

Encryption and Decryption With Querystring using ASP.NET.

Sometime we need to encrypt the Querystring to hide the information available in Querystring below is the method to solve this problem.


Class file:

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Security.Cryptography;

using System.Xml;

using System.Text;

using System.IO;

///

/// Summary description for Encryption

///

public class Encryption

{

private static byte[] key = { };

private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

private static string EncryptionKey = "!5623a#de";

public Encryption()

{

}

///

/// Decrypt Querstring Value

///

///

///

public static string Decrypt(string Input)

{

Byte[] inputByteArray = new Byte[Input.Length];

try

{

key = System.Text.Encoding.UTF8.GetBytes(EncryptionKey.Substring(0, 8));

DESCryptoServiceProvider des = new DESCryptoServiceProvider();

inputByteArray = Convert.FromBase64String(Input);

MemoryStream ms = new MemoryStream();

CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);

cs.Write(inputByteArray, 0, inputByteArray.Length);

cs.FlushFinalBlock();

Encoding encoding = Encoding.UTF8;

return encoding.GetString(ms.ToArray());

}

catch (Exception ex)

{

return "";

}

}

///

/// Encrypt QueryString Value

///

///

///

public static string Encrypt(string Input)

{

try

{

key = System.Text.Encoding.UTF8.GetBytes(EncryptionKey.Substring(0, 8));

DESCryptoServiceProvider des = new DESCryptoServiceProvider();

Byte[] inputByteArray = Encoding.UTF8.GetBytes(Input);

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 "";

}

}

}

Use:

Response.Redirect("AddClub.aspx?ClubId=" + Encryption.Encrypt(RowId));

string Id = Encryption.Decrypt(Request.QueryString["ClubId"].Trim());



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend