Encryption and Decryption With Querystring using ASP.NET 26/12/2010
Posted by anhcaxomlieu in .Net.trackback
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>
/// Summary description for Encryption
/// </summary>
public class Encryption
{
private static byte[] key = { };
private static byte[] IV = { 0×12, 0×34, 0×56, 0×78, 0×90, 0xAB, 0xCD, 0xEF };
private static string EncryptionKey = “!5623a#de”;
public Encryption()
{
}
/// <summary>
/// Decrypt Querstring Value
/// </summary>
/// <param name=”Input”></param>
/// <returns></returns>
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 “”;
}
}
/// <summary>
/// Encrypt QueryString Value
/// </summary>
/// <param name=”Input”></param>
/// <returns></returns>
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());
Phản hồi»
No comments yet — be the first.