Wednesday 17 July 2013

what is encryption and decryption with example? how to encrypt and decryption password in c# ?


In this article I will explain what is encryption and decryption with example

Encryption is the process of translating plain text data into something that appears to be meaningless and random. Encryption is a great way to keep valuable data safe—whether you’re transmitting it over the Internet, backing it up on a server, or just carrying it through airport security on your laptop. Encrypting your data makes it completely unreadable to anyone but you or its intended recipient.


Decryption is the process of translating random and meaningless data to plain text. Why we need to use this Encryption and decryption processes

Design :

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

<!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>
                    UserName:</td>
                <td>
                    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    Password:</td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="btnCreate" runat="server" Text="Encrypt Account"
                        onclick="btnEncript_Click" />
                </td>
            </tr>
        </table>
   
        <br />
        <asp:Button ID="btnCheck" runat="server" Text="Decrypt Data" onclick="btnDecrypt_Click" />
        <asp:Label ID="Label1" runat="server"></asp:Label>

    </div>
    <div>
   
    </div>
    </form>
</body>
</html>


 Below code is useful for Encrypt and Decryption the password


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class PasswordProtect : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }   
    // Bellow event is used for Encrypt the Password
    protected void btnEncript_Click(object sender, EventArgs e)
    {
        string val = txtPassword.Text;
        string pass = PasswordProtect.base64Encode(val);
        Label1.Text = pass;

    }
    // Bellow event is used for Decrypt the Password
    protected void btnDecrypt_Click(object sender, EventArgs e)
    {
        string str = PasswordProtect.base64Decode(Label1.Text);
        Label1.Text = str;
    }

    // Below code is usefull for Encrypt the password

    public static string base64Encode(string sData)
    {
        try
        {
            byte[] encData_byte = new byte[sData.Length];

            encData_byte = System.Text.Encoding.UTF8.GetBytes(sData);

            string encodedData = Convert.ToBase64String(encData_byte);

            return encodedData;

        }
        catch (Exception ex)
        {
            throw new Exception("Error in base64Encode" + ex.Message);
        }
    }
    // Below code is usefull for Decrypt the password
    public static string base64Decode(string sData)
    {
        try
        {

            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();

            System.Text.Decoder utf8Decode = encoder.GetDecoder();

            byte[] todecode_byte = Convert.FromBase64String(sData);

            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

            char[] decoded_char = new char[charCount];

            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);

            string result = new String(decoded_char);

            return result;
        }
        catch (Exception ex)
        {
            throw new Exception("Error in base64Decode" + ex.Message);
        }

    }
}

Output Screens :

Here I entered Username is 'satya' and Password is 'satya' after i clicked the Encryption button.




After clicked the encryption button encrypted code is shown below. See the below screen shot for quick identification .




Finally clicked the Decryption button , Decryption code is showing like 'satya' 



1 comment:

  1. Using the example its more easy and quick to understand encryption and decryption process. Do also throw some light on the application and tools which are based on this technique. I am curious to learn about them.
    digital signature FAQ

    ReplyDelete