In this article I will explain with an example, how to encrypt using JavaScript and decrypt using C# and VB.Net.
This article makes use of CryptoJS AES library to perform encryption using JavaScript.
 
 

CryptoJS AES JavaScript library

CryptoJS AES JavaScript library is a JavaScript based encryption library used to perform encryption on Client Side. For complete documentation, please refer the following link.
 
 

HTML Markup

The HTML Markup consists of following controls:
TextBox – For capturing plain text to be decrypted.
Label – For displaying encrypted and decrypted text.
Button – For performing encrypting and decrypting function.
The Buttons have been assigned with the JavaScript onclick and OnClick event handlers.
<asp:TextBox ID="txtPlain" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="Encrypt()" OnClick="Submit" />
<hr />
<asp:Label ID="lblDecrypted" runat="server" />
 
 

Encrypting Plain Text using JavaScript

Inside the HTML Markup, the following JS file is inherited.
1.crypto-js.min.js
Note: For Encryption and Decryption, AES encryption algorithm will be used, where a Symmetric (Same) key will be used for encryption and decryption process.
 
Firstly, the secret key is defined for the encryption and decryption and converted into a BYTE array using parse method of the CryptoJS JavaScript library.
When the Submit button is clicked the Encrypt JavaScript is executed.

Encrypt

Inside the Encrypt function, the plain text is captured from the TextBox and passed as parameter to encrypt method of CryptoJS JavaScript library where necessary properties are set.
Mode – It defines the mode of the operation. Here it is CryptoJS.mode.ECB (Electronic Codebook).
Padding – It specifies padding mode.
Finally, the encrypted text is set in the TextBox and it value is sent to the Server-Side event handler.
Note:For more details on Encryption and Decryption is JavaScript, please refer my article Encrypt and Decrypt using JavaScript.
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.2.0/crypto-js.min.js" integrity="sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script type="text/javascript">
    //Secret Key.
    var secretKey = "$ASPcAwSNIgcPPEoTSa0ODw#";
 
    //Secret Bytes.
    var secretBytes = CryptoJS.enc.Utf8.parse(secretKey);
 
    function Encrypt() {
        //Read the Plain text.
        var txtPlain = document.getElementById("<%=txtPlain.ClientID%>");
 
        //Encrypt with AES Alogorithm using Secret Key.
        var encrypted = CryptoJS.AES.encrypt(txtPlain.value, secretBytes, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
 
        //Set the encrypted Text in TextBox.
        txtPlain.value = encrypted;
    }
</script>
 
 

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
 
 

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

When Submit button is clicked, the encrypted text is captured from the TextBox is decrypted using Decrypt method.
Note: For details on Decryption method, please refer my article AES Encryption Decryption (Cryptography) Tutorial with example in ASP.Net using C# and VB.Net.
 
protected void Submit(object sender, EventArgs e)
{
    lblDecrypted.Text = this.Decrypt(Request.Form[txtPlain.UniqueID]);
}
 
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 AES Alogorithm 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 Submit(ByVal sender As Object, ByVal e As EventArgs)
    lblDecrypted.Text = Me.Decrypt(Request.Form(txtPlain.UniqueID))
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)
 
    'Encrypted Bytes.
    Dim encryptedBytes As Byte()  =  Convert.FromBase64String(encryptedText)
 
    'Decrypt with AES Alogorithm 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

ASP.Net: Encrypt using JavaScript and Decrypt using C# and VB.Net
 
 

Demo

 
 

Downloads