Search This Blog

Monday, December 27, 2010

EncryptionHelper

public class EncryptionHelper
{

# region Function to encrypt QueryString
public static string EncryptText(string pass)
{
return Encrypt(pass, "&%#@?,:*");
}
#endregion

#region EncryptQuery string
///
/// Encrypts a particular string with a specific Key
///

///
///
///

public static string Encrypt(string stringToEncrypt, string encryptionKey)
{
byte[] key = { };
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray; //Convert.ToByte(stringToEncrypt.Length)

try
{
key = Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
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 (System.Exception ex)
{
return (string.Empty);
}
}
#endregion

#region Function decrypt Querystring
public static string DecryptText(String pass)
{
return Decrypt(pass, "&%#@?,:*");
}
#endregion

#region Decrypt QueryString
///
/// Decrypts a particular string with a specific Key
///

public static string Decrypt(string stringToDecrypt, string sEncryptionKey)
{
byte[] key = { };
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray = new byte[stringToDecrypt.Length];
try
{
key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(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();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (System.Exception ex)
{
return (string.Empty);
}
}
#endregion


}

----------------------grid inline-----------
protected string Encode(object objGrid)
{
return Server.UrlEncode(EncryptionHelper.EncryptText(Convert.ToString(objGrid.ToString())));
}
-----------------------------
if (Request.QueryString["Status"] != null && Request.QueryString["EditID"] != null)
{
hdnDecodeWOID.Value = Convert.ToString(Server.UrlDecode(EncryptionHelper.DecryptText(Request.QueryString["EditID"].ToString())));
hdneDecodeStatus.Value = Convert.ToString(Server.UrlDecode(EncryptionHelper.DecryptText(Request.QueryString["Status"].ToString())));
}
-------example--------------------------------------------------
< a href="/Pages/AccountWorkOrderDisplay.aspx?EditID=< %#(Encode(Eval("ID")))%>&Status=<%#(Encode(Eval("Status")))%>&Mailstatus=<%#(Encode(Eval("MailImageStatus")))%>">

No comments:

Post a Comment