Saturday 18 August 2012

Encryption And Decryption

First We know what is encryption and decryption.
Encryption: It is the process of  translating plain text data in some random form and meaningless.
Decryption: It is the process of translating  encrypted data in  its original form.i.e in meaning full form that means in plain text.

what is the use of encryption and decryption.

By using this process we can hide the original data and display in junk formate so that no one can understand that what was the original.
In this I will explain how to encrypt data and save into the database and how to decrypt encrypted data and show in the gridview.

Design your aspx page like this:

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

<!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>
        Use Name:<asp:TextBox ID="TextBox1" runat="server"/><br />
        Password:<asp:TextBox ID="TextBox2"
            runat="server"></asp:TextBox>

            Encrypted Password Details:
        <asp:GridView ID="IncryptGridView" runat="server">
        </asp:GridView>
            Decrypted Password Details:<br />
            <asp:GridView ID="DecrptGridView" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code behind page i.e .cs page :

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.Data;
using System.Data.SqlClient;

public partial class EncryptionAndDecryprion : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=SUSHIL-70F1F267;Initial Catalog=sushil;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindencryptGridView();
            binddecryptGridView();
        }
    }
    public void bindencryptGridView()
    {
        con.Open();
        SqlCommand com = new SqlCommand("select * from loginDetails",con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        EncryptGridView.DataSource = ds;
        EncryptGridView.DataBind();
        con.Close();
    }
    public void binddecryptGridView()
    {
        con.Open();
        SqlCommand com = new SqlCommand("select * from loginDetails", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DecrptGridView.DataSource = ds;
        DecrptGridView.DataBind();
        con.Close();
    }
    private string Encryptdata(string password)
    {
        string strpass = string.Empty;
        byte[] encode = new byte[password.Length];
        encode = Encoding.UTF8.GetBytes(password);
        strpass = Convert.ToBase64String(encode);
        return strpass;

    }
    private string DecryptData(string encryptpwd)
    {
        string strdecrypt = string.Empty;
        UTF8Encoding encodepwd = new UTF8Encoding();
        Decoder decode = encodepwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace('0','+'));
        int charCount = decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decodeChar=new char[charCount];
        decode.GetChars(todecode_byte, 0, todecode_byte.Length, decodeChar, 0);
        strdecrypt = new string(decodeChar);
        return strdecrypt;
    }

    protected void DecrptGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string decryptpassword = e.Row.Cells[1].Text;
            e.Row.Cells[1].Text = DecryptData(decryptpassword);
        }
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        string strpassword = Encryptdata(TextBox2.Text);
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into loginDetails(user_name,password) values('" + TextBox1.Text + "','" + strpassword + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
        bindencryptGridView();
        binddecryptGridView();
    }
}

Run the code.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.