---
title: "Encryption and Decryption in ASP.NET"  
description: "In this article, I’m explaining about Encryption and Decryption in .NET"  
author: "priyanka kushwaha"  
published: 2015-02-27  
updated: 2020-11-20  
canonical: https://www.mindstick.com/articles/1620/encryption-and-decryption-in-asp-dot-net  
category: ".net"  
tags: ["c#", "asp.net", "security in .net", "encryption"]  
reading_time: 2 minutes  

---

# Encryption and Decryption in ASP.NET

\

In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370), I’m explaining about [Encryption and Decryption](https://www.mindstick.com/Articles/12911/encrypt-and-decrypt-data-with-c-sharp) in .NET

**Encryption:** is the [activity](https://www.mindstick.com/forum/23119/how-to-save-activity-state-in-android) of [converting data](https://www.mindstick.com/forum/33616/converting-data-of-nsdata-into-bytes-array-in-ios) or information into [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) or a [secret](https://www.mindstick.com/articles/23215/the-secret-to-a-compelling-logo-design) key.

**Decryption:** is the activity of making clear to converting from code into plain [text](https://www.mindstick.com/blog/301635/did-people-reinvent-texting-to-express-the-full-range-of-emotions).

## Example:

## Create a [Class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp).cs

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Security.Cryptography;using System.IO;using System.Text; /// <summary>/// Summary description for Class1/// </summary>public class Class1{    string Encryptionkey = "123";    public string Encrypt(string originalText)    {               byte[] originalbytes = Encoding.Unicode.GetBytes(originalText);        using (Aes encryptor = Aes.Create())        {            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(Encryptionkey, new Byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64,  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(originalbytes, 0, originalbytes.Length);                    cs.Close();                }                originalText = Convert.ToBase64String(ms.ToArray());            }        }        return originalText;     }  public string Decrypt(string cipherText)    {                   Byte[] cipherBytes = Convert.FromBase64String(cipherText);            using (Aes encryptor = Aes.Create())            {              Rfc2898DeriveBytes pbd = new Rfc2898DeriveBytes(Encryptionkey, new Byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x76 });                encryptor.Key = pbd.GetBytes(32);                encryptor.IV = pbd.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;}            public Class1()            {                        //                        // TODO: Add constructor logic here                        //            }} 
```

**Create Default.aspx [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp)**

```
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html>  <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">    <title></title> </head><body>     <form id="form1" runat="server">        <table>            <tr>                <td>Password</td>                <td colspan="2">                    <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>            </tr>            <tr>                <td>Encryption text=</td>                <td>                    <asp:Label ID="lblEnc" runat="server" Text=""></asp:Label></td>                <td>                    <asp:Button ID="btnEncrypt" runat="server" Text="Encrypt" OnClick="btnEncrypt_Click" /></td>            </tr>            <tr>                <td>Decryption text=</td>                <td>                    <asp:Label ID="lblDec" runat="server" Text=""></asp:Label></td>                <td>                    <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" Enabled="False" OnClick="btnDecrypt_Click" /></td>            </tr>            <tr>                <td></td>            </tr>        </table>     </form> </body></html>
```

**Write in Default.aspx.cs**

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{     Class1 method = new Class1();    protected void Page_Load(object sender, EventArgs e)    {     }    protected void btnEncrypt_Click(object sender, EventArgs e)    {        lblEnc.Text =method.Encrypt(txtPassword.Text.Trim());        btnDecrypt.Enabled = true;    }    protected void btnDecrypt_Click(object sender, EventArgs e)    {        lblDec.Text = method.Decrypt(lblEnc.Text.Trim());    }}
```

**[Output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular):**

![Encryption and Decryption in ASP.NET](https://www.mindstick.com/mindstickarticle/669b6ac7-d445-444b-ac26-9bfc8db001cc/images/2c254a1b-e6e3-43a5-bd3a-467d1273d757.png)

![Encryption and Decryption in ASP.NET](https://www.mindstick.com/mindstickarticle/669b6ac7-d445-444b-ac26-9bfc8db001cc/images/8f17028b-3b22-4a43-8155-2437c7daff5f.png)

---

Original Source: https://www.mindstick.com/articles/1620/encryption-and-decryption-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
