Tuesday, 26 November 2013

Simple Encryption Decryption in Asp.Net

Simple Encryption Decryption in Asp.Net

Note:-

Encrypted String Length will depend on the length of the string you have entered.

Aspx Page:-

<div>
        <table cellspacing="15px" width="21%" style="margin-left: 10%">
            <tr>
                <td colspan="2" align="center">
                    <asp:Label ID="Label13" runat="server" Style="text-align: center;" Text="Encrypt/Decrypt"
                        Font-Bold="true" Font-Size="28px"></asp:Label><br />
                    <br />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" AssociatedControlID="txt_str" Text="String To Encrypt/Decrypt"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txt_str" CssClass="TextBox" runat="server" AutoComplete="off" PlaceHolder="e.g :Sam"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="encrypt" runat="server" Text="Encrypt" OnClick="Encrypt_Click" />
                </td>
                <td>
                    <asp:Button ID="decrypt" runat="server" Text="Decrypt" OnClick="Decrypt_Click" />
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Label ID="lbl_dyc" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
        <asp:Label ID="lbl_enc" runat="server"></asp:Label>
    </div>


CS Page Code:-

Name Space Used:-


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


Code:-

Encrypt Button:-

       protected void Encrypt_Click(object sender, EventArgs e)
        {
             string strpass = txt_str.Text;
            string dyc = Encrypt(strpass);
            lbl_dyc.Text = dyc;
        }

Decrypt Button:-

       protected void Decrypt_Click(object sender, EventArgs e)
        {
            string encryptedstr = txt_str.Text;
            string dyc = Decrypt(encryptedstr);
            lbl_dyc.Text = dyc;
        }

Method for Encrypt String :-

        public string Encrypt(string txtstring)
        {
            byte[] passBytes = System.Text.Encoding.Unicode.GetBytes(txtstring);
            string encrypt = Convert.ToBase64String(passBytes);
            return encrypt;
        }


Method for Decrypt String :-

        public string Decrypt(string encryptedstr)
        {
            byte[] passByteData = Convert.FromBase64String(encryptedstr);
            string originalstr = System.Text.Encoding.Unicode.GetString(passByteData);
            return originalstr;
        }

Encrytpion:-

Decrytpion:-




Don't forget to leave your feedback and comments below..!

No comments:

Post a Comment