///
private static byte[] key = { };
private static byte[] IV = { 38, 55, 206, 48, 28, 64, 20, 16 };
private static string stringKey = "!5663a#AK";
#endregion
#region "--Public Methods--" public static string Encrypt(string text) {
try {
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Encoding.UTF8.GetBytes(text); MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,des.CreateEncryptor(key, IV), CryptoStreamMode.Write); cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
string retval= Convert.ToBase64String(memoryStream.ToArray()); return retval; }
catch (Exception ex) {
// Handle Exception Here
}
return string.Empty;
}
public static string GetEncryptedQueryString(string QueryStringToBeEncypted) {
string key = Encrypt(QueryStringToBeEncypted);
StringWriter writer = new StringWriter(); HttpContext.Current.Server.UrlEncode(key, writer); return writer.ToString();
}
public static string Decrypt(string text) {
try {
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Convert.FromBase64String(text);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
catch (Exception ex) {
// Handle Exception Here
}
return string.Empty;
}
#endregion
public ClassQueryEncrypt() { // // TODO: Add constructor logic here // }}
--On Redirecting Page-----
Response.Write(ClassQueryEncrypt.GetEncryptedQueryString("122") + "
");
Response.Redirect("EncreptedQuery.aspx?Id=" + ClassQueryEncrypt.GetEncryptedQueryString("122"));
--on Decrypting Page::--
string sId = Request.QueryString["Id"].ToString();
Response.Write(sId + "
Decrepted To:: ");
Response.Write(ClassQueryEncrypt.Decrypt(sId));