.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

Another method for QueryString Encryption and Encryption in ASP.NET

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

Below is the sample example to encrypt and decrypt Querystring in asp.net using c#

using System;

using System.Data;

using System.Configuration;

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.IO;

using System.Xml;

using System.Text;

using System.Security.Cryptography;

namespace VetCareWeb

{

public class VetCareEncription

{

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

}

}

public static string encryptQueryString(string strQueryString)

{

VetCareEncription objES = new VetCareEncription();

return objES.Encrypt(strQueryString, "!#$a54?3");

}

public static string decryptQueryString(string strQueryString)

{

VetCareEncription objDS = new VetCareEncription();

return objDS.Decrypt(strQueryString, "!#$a54?3");

}

}

}



     

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