In this article I will explain with an example, how to use Cryptography in ASP.Net Web Forms using C# and VB.Net.
 
 
 

HTML Markup

The HTML Markup consists of following controls:
TextBox – For capturing plain text to be encrypted.
Label – For displaying encrypted and decrypted text.
Button – For performing encrypting and decrypting function.
The Buttons have been assigned with an OnClick event handler.
Plain Text:
<asp:TextBox ID="txtPlain" runat="server"></asp:TextBox>
<asp:Button ID="btnEncrypt" runat="server" Text="Encrypt" OnClick="OnEncrypt" />
<br />
EncryptedText: <asp:Label ID="lblEncrypted" runat="server"></asp:Label>
<hr />
<asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" OnClick="OnDecrypt" />
<br />
<br />
Decrypted Value: <asp:Label ID="lblDecrypted" runat="server"></asp:Label>
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.Security.Cryptography;
using System.Text;
 
VB.Net
Imports System.Security.Cryptography
Imports System.Text
 
 

Encrypting Plain Text using C# and VB.Net

When Encrypt button is clicked, the plain text is captured from the TextBox and passed as parameter to Encrypt method.
 

Encrypt

Inside the Encrypt method, first the secret key is defined for the encryption and converted into a BYTE array using GetBytes method of the Encoding class.
And the plain text captured from the TextBox is also converted into a BYTE array.
Then, an object of AES class (AES Symmetric key algorithm) is created for performing encryption and its necessary properties are set.
Note: For Encryption and Decryption, AES encryption algorithm will be used, where a Symmetric (Same) key will be used for encryption and decryption process.
 
Key – For defining the EncryptionKey.
Mode – It defines the mode of the operation. Here it is CipherMode.ECB (Electronic Codebook).
Padding – It specifies padding mode.
After that, an object of ICryptoTransform is created and CreateEncryptor method of AES class (AES Symmetric key algorithm) is called.
Then, using TransformFinalBlock method of ICryptoTransform class object the plain text is encrypted and stored as BYTE array.
Finally, the BYTE array is converted into a BASE64 string and string value is returned and displayed using Label control.
C#
protected void OnEncrypt(object sender, EventArgs e)
{
    lblEncrypted.Text = this.Encrypt(txtPlain.Text.Trim());
}
 
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 OnEncrypt(ByVal sender As ObjectByVal e As EventArgs)
    lblEncrypted.Text = Me.Encrypt(txtPlain.Text.Trim())
End Sub
 
Private Function Encrypt(ByVal plainText As String) As String
    Dim secretKey As String "$ASPcAwSNIgcPPEoTSa0ODw#"
    Dim secretBytes As Byte() = Encoding.UTF8.GetBytes(secretKey)
    Dim plainTextBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
    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
 
 

Decrypting Encrypted Text to Plain Text using C# and VB.Net

When Decrypt button is clicked, the encrypted text is captured from the Label and passed as parameter to Decrypt method.
 

Decrypt

Inside the Decrypt method, first the secret key is defined for the decryption and converted into a BYTE array using GetBytes method of the Encoding class.
And the encrypted text captured from the Label is also converted into a BYTE array.
Then, an object of AES class (AES Symmetric key algorithm) is created for performing encryption and its necessary properties are set.
Note: For Encryption and Decryption, AES encryption algorithm will be used, where a Symmetric (Same) key will be used for encryption and decryption process.
 
Key – For defining the EncryptionKey.
Mode – It defines the mode of the operation. Here it is CipherMode.ECB (Electronic Codebook).
Padding – It specifies padding mode.
After that, an object of ICryptoTransform is created and CreateDecryptor method of AES class (AES Symmetric key algorithm) is called.
Then, using TransformFinalBlock method of ICryptoTransform class object the encrypted text is decrypted and stored as BYTE array.
Finally, the BYTE array is converted into a BASE64 string and string value is returned and displayed using Label control.
C#
protected void OnDecrypt(object sender, EventArgs e)
{
    lblDecrypted.Text = this.Decrypt(lblEncrypted.Text.Trim());
}
 
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 OnDecrypt(ByVal sender As ObjectByVal e As EventArgs)
    lblDecrypted.Text = Me.Decrypt(lblEncrypted.Text.Trim())
End Sub
 
Private Function Decrypt(ByVal encryptedText As String) As String
    Dim secretKey As String "$ASPcAwSNIgcPPEoTSa0ODw#"
    Dim secretBytes As Byte() = Encoding.UTF8.GetBytes(secretKey)
    Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedText)
    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

Using Cryptography in ASP.Net Web Forms
 
 

Demo

 
 

Downloads