In this article I will explain with an example, how to use encryption and decryption (Cryptography) in ASP.Net using C# and VB.Net.
This article makes use of Symmetric (Same) key AES Algorithm for encryption and decryption process.
 
 
HTML Markup
The following HTML Markup consists of two TextBox controls to accept inputs for encryption and decryption, two Button controls and two Labels for displaying encrypted and decrypted text.
The Button controls have been assigned OnClick event handler.
<u>Encrypt</u><br /><br />
Original Text:
<asp:TextBox ID="txtOriginalText" runat="server" />
<br /><br />
Encrypted Text:
<asp:Label ID="lblEncryptedText" runat="server" />
<br /><br />
<asp:Button ID="btnEncrypt" OnClick="Encrypt" Text="Encrypt" runat="server" />
<hr />
<u>Decrypt</u><br /><br />
Encrypted Text:
<asp:TextBox ID="txtEncryptedText" runat="server" />
<br /><br />
Decrypted Text:
<asp:Label ID="lblDecryptedText" runat="server" />
<br /><br />
<asp:Button ID="btnDecrypt" OnClick="Decrypt" Text="Decrypt" runat="server" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Text;
using System.Security.Cryptography;
 
VB.Net
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
 
 
Encryption
Note: For Encryption and Decryption AES encryption algorithm will be used, where a Symmetric (Same) key will be used for encryption and decryption process
 
When the Encrypt button is clicked, the Encrypt method is called by passing the TextBox value i.e. original text to it.
Inside the Encrypt method, first the original text i.e. clear text is converted into bytes using the GetBytes method of Encoding class and then for the AES algorithm to perform encryption Key and IV is generated using the derived bytes and the symmetric key.
Then, using MemoryStream and CryptoStream the clear text is encrypted and written to byte array and the byte array is converted to Base64String and returned the encrypted text.
Finally, the encrypted text is displayed in the Label.
C#
protected void Encrypt(object sender, EventArgs e)
{
    lblEncryptedText.Text = this.Encrypt(txtOriginalText.Text.Trim());
}
 
private string Encrypt(string clearText)
{
    string encryptionKey = "MAKV2SPBNI99212";
    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);
        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(clearBytes, 0, clearBytes.Length);
                cs.Close();
            }
            clearText = Convert.ToBase64String(ms.ToArray());
        }
    }
    return clearText;
}
 
VB.Net
Protected Sub Encrypt(sender As Object, e As EventArgs)
    lblEncryptedText.Text = Me.Encrypt(txtOriginalText.Text.Trim())
End Sub
 
Private Function Encrypt(clearText As String) As String
    Dim encryptionKey As String = "MAKV2SPBNI99212"
    Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText)
    Using encryptor As Aes = Aes.Create()
        Dim pdb As New Rfc2898DeriveBytes(encryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _
         &H65, &H64, &H76, &H65, &H64, &H65, &H76})
        encryptor.Key = pdb.GetBytes(32)
        encryptor.IV = pdb.GetBytes(16)
        Using ms As New MemoryStream()
            Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
                cs.Write(clearBytes, 0, clearBytes.Length)
                cs.Close()
            End Using
            clearText = Convert.ToBase64String(ms.ToArray())
        End Using
    End Using
    Return clearText
End Function
 
 
Decryption
When the Decrypt button is clicked, the Decrypt method is called by passing the TextBox value i.e. encrypted text to it.
Inside the Decrypt method, first the encrypted text i.e. cipher text is converted into bytes and then similar to the encryption process a Key and IV is generated using the derived bytes and the symmetric key.
Then, using MemoryStream and CryptoStream the cipher text is decrypted and written to byte array and the byte array is converted to string and returned the decrypted i.e. original text.
Finally, the decrypted text is displayed in the Label.
C#
protected void Decrypt(object sender, EventArgs e)
{
    lblDecryptedText.Text = this.Decrypt(txtEncryptedText.Text.Trim());
}
 
private string Decrypt(string cipherText)
{
    string encryptionKey = "MAKV2SPBNI99212";
    byte[] cipherBytes = Convert.FromBase64String(cipherText);
    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);
        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(cipherBytes, 0, cipherBytes.Length);
                cs.Close();
            }
            cipherText = Encoding.Unicode.GetString(ms.ToArray());
        }
    }
    return cipherText;
}
 
VB.Net
Protected Sub Decrypt(sender As Object, e As EventArgs)
    lblDecryptedText.Text = Me.Decrypt(txtEncryptedText.Text.Trim())
End Sub
 
Private Function Decrypt(cipherText As String) As String
    Dim encryptionKey As String = "MAKV2SPBNI99212"
    Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText)
    Using encryptor As Aes = Aes.Create()
        Dim pdb As New Rfc2898DeriveBytes(encryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _
         &H65, &H64, &H76, &H65, &H64, &H65, &H76})
        encryptor.Key = pdb.GetBytes(32)
        encryptor.IV = pdb.GetBytes(16)
        Using ms As New MemoryStream()
            Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)
                cs.Write(cipherBytes, 0, cipherBytes.Length)
                cs.Close()
            End Using
            cipherText = Encoding.Unicode.GetString(ms.ToArray())
        End Using
    End Using
    Return cipherText
End Function
 
 
Screenshot
AES Encryption Decryption (Cryptography) Tutorial with example in ASP.Net using C# and VB.Net
 
 
Demo
 
 
Downloads