Share Blog

Thursday, November 26, 2015

Encryption and Decryption in ASP.NET

Encryption: is the activity of converting data or information into code or a secret key.


Decryption: is the activity of making clear to converting from code into plain text.

Create a Class.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;


public class Class1
{
    string Encryptionkey = "987";
    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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Encryption_decryption.aspx.cs" Inherits="Encryption_decryption" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <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="lblEncry" runat="server" Text=""></asp:Label></td>
                <td>
                    <asp:Button ID="BtnEncryption" runat="server" Text="Encrypt"
                        onclick="BtnEncryption_Click"  /></td>
            </tr>
            <tr>
                <td>Decryption text=</td>
                <td>
                    <asp:Label ID="lblDecry" runat="server" Text=""></asp:Label></td>
                <td>
                    <asp:Button ID="BtnDecryption" runat="server" Text="Decrypt" Enabled="False"
                        onclick="BtnDecryption_Click"  /></td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>

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


public partial class Encryption_decryption : System.Web.UI.Page
{
    Class1 obj = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnEncryption_Click(object sender, EventArgs e)
    {
        lblEncry.Text = obj.Encrypt(TxtPassword.Text.Trim());
        BtnDecryption.Enabled = true;

    }
    protected void BtnDecryption_Click(object sender, EventArgs e)
    {
        lblDecry.Text = obj.Decrypt(lblEncry.Text.Trim());
    }
}


OutPut



No comments:

Post a Comment