Monday, December 9, 2013

Simple Cryptography encryption and decryption in ASP.Net using C#

Hi Friends,

Here i will explane you encryption and decryption cryptography in asp.net using C#.

Firstly you have to know about cryptography.

The transformation of data in order to hide its information content, prevent its undetected modification, or prevent its unauthorized use.

The conversion of data into a secret code for protection of privacy using a specific algorithm and a secret key. The original text, or "plaintext", is converted into a coded equivalent called "ciphertext" via an encryption algorithm. The ciphertext can only be decoded (decrypted) using a predefined secret key.

ON ASPX PAGE :- paste the code

<table style=" padding:20px; line-height:40px;">
        <tr>
            <td colspan="2" align="center">
                <asp:Label ID="Label1" runat="server" Text="Encryption" Font-Size="Larger" ForeColor="Red"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="Original Text:"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtOriginalText" runat="server" Text="" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server" Text="Encrypted Text:"></asp:Label>
            </td>
            <td>
                <asp:Label ID="lblEncryptedText" runat="server" Text="" Font-Bold="true" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnEncryption" Text="Encryption" runat="server" OnClick="btnEncryption_Click" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
            </td>
        </tr>
         <tr>
            <td>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <asp:Label ID="Label4" runat="server" Text="Decryption" Font-Size="Larger" ForeColor="Red"></asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label5" runat="server" Text="Encrypted Text:"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtEncryptedText" runat="server" Text="" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label6" runat="server" Text="Decrypted Text:"></asp:Label>
            </td>
            <td>
                <asp:Label ID="lblDecryptedText" runat="server" Text="" Font-Bold="true" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnDecryption" Text="Decryption" runat="server" OnClick="btnDecryption_Click" />
            </td>
        </tr>
    </table>


ON CODE BEHIND 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.IO;
using System.Text;
using System.Security.Cryptography;


protected void btnEncryption_Click(object sender, EventArgs e)
        {
            lblEncryptedText.Text = this.btnEncryption_Click(txtOriginalText.Text.Trim());
        }

        private string btnEncryption_Click(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;
        }



        protected void btnDecryption_Click(object sender, EventArgs e)
        {
            lblDecryptedText.Text = this.btnDecryption_Click(txtEncryptedText.Text.Trim());
        }

        private string btnDecryption_Click(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;
        }