Encrypt and Decrypt Querystring Paramiter Values in Asp.net using C# and VB.Net

In this tutorial i am going to share with you how to encrypt and decrypt querystring paramiter values in Asp.net using both C# and VB.Net. In Asp.Net By using “Cryptography” properties you can encrypt and decrpy querystring values easily. 

So, Let's Start with one example to encrypt and decrypt query string parameter.

To encrypt and decrypt query string paramiter in asp.net we need to add two diffrent .aspx file with name Encrypt.aspx and another is Decrypt.aspx in your project directory.

Now, we will write following code in Encrypt.aspx file for encrypt query string parameter.

Encrypt.aspx
<div>
    Id :
    <asp:TextBox ID="txt_Id" runat="server" />
    Name :
    <asp:TextBox ID="txt_Name" runat="server" />
    <br />
    <br />
    <asp:Button ID="btn_Enc" Text="Redirect Page" runat="server" OnClick="btn_Enc_Click" />
</div>
Now, After completion of aspx page we need to add following namespaces in code behind file (Encrypt.aspx.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Security.Cryptography;
After adding namespaces we will write the code like as shown below

C# Code for Encrypt Querystring Paramiter Values in Asp.net

public partial class Encrypt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btn_Enc_Click(object sender, EventArgs e)
    {
        string userid = HttpUtility.UrlEncode(EncryptQueryString(txt_Id.Text.Trim()));
        string username = HttpUtility.UrlEncode(EncryptQueryString(txt_Name.Text.Trim()));
        Response.Redirect("~/Decrypt.aspx?userid=" + userid + "&uname=" + username + "");
    }
    public static string EncryptQueryString(string inputText)
    {
        string encryptionkey = "NIKUNJSATASIYA-ASPPOINT95476";
        byte[] keybytes = Encoding.ASCII.GetBytes(encryptionkey.Length.ToString());
        RijndaelManaged rijndaelCipher = new RijndaelManaged();
        byte[] plainText = Encoding.Unicode.GetBytes(inputText);
        PasswordDeriveBytes pwdbytes = new PasswordDeriveBytes(encryptionkey, keybytes);
        using (ICryptoTransform encryptrans = rijndaelCipher.CreateEncryptor(pwdbytes.GetBytes(32), pwdbytes.GetBytes(16)))
        {
            using (MemoryStream mstrm = new MemoryStream())
            {
                using (CryptoStream cryptstm = new CryptoStream(mstrm, encryptrans, CryptoStreamMode.Write))
                {
                    cryptstm.Write(plainText, 0, plainText.Length);
                    cryptstm.Close();
                    return Convert.ToBase64String(mstrm.ToArray());
                }
            }
        }
    }
}

VB.NET Code for Encrypt Querystring Paramiter Values in Asp.net

Protected Sub btn_Enc_Click(ByVal sender As ObjectByVal e As EventArgs)
       Dim userid As String = HttpUtility.UrlEncode(EncryptQueryString(txt_Id.Text.Trim()))
       Dim username As String = HttpUtility.UrlEncode(EncryptQueryString(txt_Name.Text.Trim()))
       Response.Redirect((Convert.ToString((Convert.ToString("~/Decrypt.aspx?userid=") & userid) + "&uname=") & username) + "")
   End Sub
   Public Shared Function EncryptQueryString(ByVal inputText As StringAs String
       Dim encryptionkey As String = "NIKUNJSATASIYA-ASPPOINT95476"
       Dim keybytes As Byte() = Encoding.ASCII.GetBytes(encryptionkey.Length.ToString())
       Dim rijndaelCipher As New RijndaelManaged()
       Dim plainText As Byte() = Encoding.Unicode.GetBytes(inputText)
       Dim pwdbytes As New PasswordDeriveBytes(encryptionkey, keybytes)
       Using encryptrans As ICryptoTransform = rijndaelCipher.CreateEncryptor(pwdbytes.GetBytes(32), pwdbytes.GetBytes(16))
           Using mstrm As New MemoryStream()
               Using cryptstm As New CryptoStream(mstrm, encryptrans, CryptoStreamMode.Write)
                   cryptstm.Write(plainText, 0, plainText.Length)
                   cryptstm.Close()
                   Return Convert.ToBase64String(mstrm.ToArray())
               End Using
           End Using
       End Using
   End Function
Now, open Decrypt.aspx file and write the following code as shown below
Decrypt.aspx
<div>
    <b>Decrypted Values Are:</b><br />
    <br />
    Id is:<asp:Label ID="lbl_Id" runat="server" />
    <br />
    <br />
    Name is:
    <asp:Label ID="lbl_Name" runat="server" />
</div>
After completion of aspx page we need to add following namespaces in code behind file (Decrypt.aspx.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Security.Cryptography;
After adding namespaces we will write the code like as shown below

C# Code For for Decrypt Querystring Paramiter Values in Asp.net

public partial class Decrypt : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string userid = HttpUtility.UrlDecode(Request.QueryString["userid"]);
        string username = HttpUtility.UrlDecode(Request.QueryString["uname"]);
        lbl_Name.Text = DecryptQueryString(username);
        lbl_Id.Text = DecryptQueryString(userid);
    }
 
    public static string DecryptQueryString(string encryptText)
    {
        string encryptionkey = "NIKUNJSATASIYA-ASPPOINT95476";
        byte[] keybytes = Encoding.ASCII.GetBytes(encryptionkey.Length.ToString());
        RijndaelManaged rijndaelCipher = new RijndaelManaged();
        byte[] encryptedData = Convert.FromBase64String(encryptText.Replace(" ""+"));
        PasswordDeriveBytes pwdbytes = new PasswordDeriveBytes(encryptionkey, keybytes);
        using (ICryptoTransform decryptrans = rijndaelCipher.CreateDecryptor(pwdbytes.GetBytes(32), pwdbytes.GetBytes(16)))
        {
            using (MemoryStream mstrm = new MemoryStream(encryptedData))
            {
                using (CryptoStream cryptstm = new CryptoStream(mstrm, decryptrans, CryptoStreamMode.Read))
                {
                    byte[] plainText = new byte[encryptedData.Length];
                    int decryptedCount = cryptstm.Read(plainText, 0, plainText.Length);
                    return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
                }
            }
        }
    }
}

VB.NET Code for Decrypt Querystring Paramiter Values in Asp.net

Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgsHandles Me.Load
       Dim userid As String = HttpUtility.UrlDecode(Request.QueryString("userid"))
       Dim username As String = HttpUtility.UrlDecode(Request.QueryString("uname"))
       lbl_Id.Text = DecryptQueryString(userid)
       lbl_Name.Text = DecryptQueryString(username)
   End Sub
   Public Shared Function DecryptQueryString(ByVal encryptText As StringAs String
       Dim encryptionkey As String = "NIKUNJSATASIYA-ASPPOINT95476"
       Dim keybytes As Byte() = Encoding.ASCII.GetBytes(encryptionkey.Length.ToString())
       Dim rijndaelCipher As New RijndaelManaged()
       Dim encryptedData As Byte() = Convert.FromBase64String(encryptText.Replace(" ""+"))
       Dim pwdbytes As New PasswordDeriveBytes(encryptionkey, keybytes)
       Using decryptrans As ICryptoTransform = rijndaelCipher.CreateDecryptor(pwdbytes.GetBytes(32), pwdbytes.GetBytes(16))
           Using mstrm As New MemoryStream(encryptedData)
               Using cryptstm As New CryptoStream(mstrm, decryptrans, CryptoStreamMode.Read)
                   Dim plainText As Byte() = New Byte(encryptedData.Length - 1) {}
                   Dim decryptedCount As Integer = cryptstm.Read(plainText, 0, plainText.Length)
                   Return Encoding.Unicode.GetString(plainText, 0, decryptedCount)
               End Using
           End Using
       End Using
   End Function
Demo:

Encrypt and Decrypt Querystring Paramiter Values in Asp.net

Download Sample Code:

Download Sample Code


Previous
Next Post »

If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon