In this article I will explain with an example, how to encrypt
QueryString parameter values and send it to another page and then
decrypt the
encrypted QueryString parameter values in
ASP.Net using C# and VB.Net.
This article makes use of
System.Security.Cryptography class and
AES algorithm for
encryption and
decryption in
ASP.Net.
QueryString Encryption
The following page will perform encryption of QueryString parameter values and then send it to another Page.
HTML Markup
The
HTML Markup consist of following controls:
TextBox – For capturing Name value.
DropDownList – For capturing Technology value.
Button – For submitting the Form and redirecting to another page with QueryString parameter values.
The Button has been assigned with an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Name:</td>
<td>
<asp:TextBox ID="txtName" runat="server" Text="Mudassar Khan" />
</td>
</tr>
<tr>
<td>Technology:</td>
<td>
<asp:DropDownList ID="ddlTechnology" runat="server">
<asp:ListItem Text="ASP.Net" Value="ASP.Net" />
<asp:ListItem Text="PHP" Value="PHP" />
<asp:ListItem Text="JSP" Value="JSP" />
</asp:DropDownList>
</td>
</tr>
</table>
<hr />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit" />
Namespaces
You will need to import the following namespaces.
C#
using System.Text;
using System.Security.Cryptography;
VB.Net
Imports System.Text
Imports System.Security.Cryptography
Encrypting QueryString parameter values
When the
Submit button is clicked, the fetched values of
TextBox and
DropDownList is passed as parameter to
Encrypt method.
C#
protected void Submit(object sender, EventArgs e)
{
string name = HttpUtility.UrlEncode(this.Encrypt(txtName.Text.Trim()));
string technology = HttpUtility.UrlEncode(this.Encrypt(ddlTechnology.SelectedItem.Value));
Response.Redirect(string.Format("~/CS2.aspx?name={0}&technology={1}", name, technology));
}
private string Encrypt(string plainText)
{
//Secret Key.
string secretKey = "$ASPcAwSNIgcPPEoTSa0ODw#";
//Secret Bytes.
byte[]secretBytes = Encoding.UTF8.GetBytes(secretKey);
//Plain Text Bytes.
byte[]plainTextBytes = Encoding.UTF8.GetBytes(plainText);
//Encrypt with AESAlogorithm using Secret Key.
using (Aes aes = Aes.Create())
{
aes.Key = secretBytes;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
byte[]encryptedBytes = null;
using (ICryptoTransform encryptor = aes.CreateEncryptor())
{
encryptedBytes = encryptor.TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length);
}
return Convert.ToBase64String(encryptedBytes);
}
}
VB.Net
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String = HttpUtility.UrlEncode(Me.Encrypt(txtName.Text.Trim()))
Dim technology As String = HttpUtility.UrlEncode(Me.Encrypt(ddlTechnology.SelectedItem.Value))
Response.Redirect(String.Format("~/VB2.aspx?name={0}&technology={1}", name, technology))
End Sub
Private Function Encrypt(ByVal plainText As String) As String
'Secret Key.
Dim secretKey As String = "$ASPcAwSNIgcPPEoTSa0ODw#"
'Secret Bytes.
Dim secretBytes As Byte() = Encoding.UTF8.GetBytes(secretKey)
'Plain Text Bytes.
Dim plainTextBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
'Encrypt with AESAlogorithm using Secret Key.
Using aes As Aes = Aes.Create()
aes.Key = secretBytes
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.PKCS7
Dim encryptedBytes As Byte() = Nothing
Using encryptor As ICryptoTransform = aes.CreateEncryptor()
encryptedBytes = encryptor.TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length)
End Using
Return Convert.ToBase64String(encryptedBytes)
End Using
End Function
QueryString Decryption
HTML Markup
The
HTML markup consists following controls:
Label – For displaying QueryString values.
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Name:</td>
<td>
<asp:Label ID="lblName" runat="server" Text="" />
</td>
</tr>
<tr>
<td>Technology:</td>
<td>
<asp:Label ID="lblTechnology" runat="server" Text="" />
</td>
</tr>
</table>
Namespaces
You will need to import the following namespaces.
C#
using System.Web;
using System.Text;
using System.Security.Cryptography;
VB.Net
Imports System.Web
Imports System.Text
Imports System.Security.Cryptography
Decrypting QueryString Parameter values
Inside the Page_Load event handler, the Decrypt method called which accepts the QueryString fetched from the URL of the page from where the page is being redirected.
C#
protected void Page_Load(object sender EventArgs e)
{
if (!this.IsPostBack)
{
lblName.Text = this.Decrypt(HttpUtility.UrlDecode(Request.QueryString["name"]));
lblTechnology.Text = this.Decrypt(HttpUtility.UrlDecode(Request.QueryString["technology"]));
}
}
private string Decrypt(string encryptedText)
{
//Secret Key.
string secretKey = "$ASPcAwSNIgcPPEoTSa0ODw#";
//Secret Bytes.
byte[]secretBytes = Encoding.UTF8.GetBytes(secretKey);
//Encrypted Bytes.
byte[]encryptedBytes = Convert.FromBase64String(encryptedText);
//Decrypt with AESAlogorithm using Secret Key.
using (Aes aes = Aes.Create())
{
aes.Key = secretBytes;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
byte[]decryptedBytes = null;
using (ICryptoTransform decryptor = aes.CreateDecryptor())
{
decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
}
return Encoding.UTF8.GetString(decryptedBytes);
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
lblName.Text = Me.Decrypt(HttpUtility.UrlDecode(Request.QueryString("name")))
lblTechnology.Text = Me.Decrypt(HttpUtility.UrlDecode(Request.QueryString("technology")))
End If
End Sub
Private Function Decrypt(ByVal encryptedText As String) As String
'Secret Key.
Dim secretKey As String = "$ASPcAwSNIgcPPEoTSa0ODw#"
'Secret Bytes.
Dim secretBytes As Byte() = Encoding.UTF8.GetBytes(secretKey)
'Plain Text Bytes.
Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedText)
'Encrypt with AESAlogorithm using Secret Key.
Using aes As Aes = Aes.Create()
aes.Key = secretBytes
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.PKCS7
Dim decryptedBytes As Byte() = Nothing
Using decryptor As ICryptoTransform = aes.CreateDecryptor()
decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length)
End Using
Return Encoding.UTF8.GetString(decryptedBytes)
End Using
End Function
Screenshot
Demo
Downloads