---
title: "Encryption and Decryption in .NET"  
description: "In this blog, I’m explaining about Encryption and Decryption in .NetEncryption is the process of translating plain text into something that appears to"  
author: "priyanka kushwaha"  
published: 2015-01-27  
updated: 2015-01-27  
canonical: https://www.mindstick.com/blog/732/encryption-and-decryption-in-dot-net  
category: ".net"  
tags: [".net"]  
reading_time: 2 minutes  

---

# Encryption and Decryption in .NET

In this [blog](https://www.mindstick.com/articles/12705/myths-and-misconception-about-blog), I’m explaining about [Encryption and Decryption](https://www.mindstick.com/forum/158184/how-do-you-implement-session-encryption-and-decryption-in-a-web-application) in .Net

**Encryption** is the [process](https://yourviews.mindstick.com/story/1525/7-important-factors-that-may-affect-the-learning-process) of translating plain text into something that appears to be [random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c) and meaningless.

Decryption is the process of translating random and meaningless [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) to plain text.

[Design](https://www.mindstick.com/articles/12279/interior-design-and-furniture-store-wordpress-theme) your aspx like this

```
<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>
```

##### Step 2:

Create [Class file](https://www.mindstick.com/forum/60/how-we-add-dll-with-other-class-file)

```
using System.Security.Cryptography;using System.IO;using System.Text;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                        //            }}
```

##### Step 3:

\

After that add following code in [code behind](https://www.mindstick.com/forum/2199/call-javascript-s-function-from-code-behind)

```
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());    }} 
```

---

Original Source: https://www.mindstick.com/blog/732/encryption-and-decryption-in-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
