Plain text change in unreadable format known as encoding. And reverse of it known as decoding. Change a plain text into cipher text using any key known as hashing. There are lots of techniques available in hashing.
Here we take a simple example of among. First we start from Encoding.
first of all, take a string variable, after that take a byte array, which size is equal to string Length. Look like
Get Bytes of string value using getBytes( ) method of UTF8 encoding. Now, your code look like
// here encodepwd is the string variable.
First of all, encoded string convert into specified string format, which is equivalent 8-bit unsigned integer array using FromBase64String ( ) method. count number of characters using GetCharCount( ) of byte array like.
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td class="style1">
UserName</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
Password</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" />
</td>
<td>
</td>
</tr>
</table>
</div>
<asp:GridView ID="GridView1" runat="server" Caption="Encrypted Data">
</asp:GridView>
<br />
<asp:GridView ID="GridView2" runat="server" Caption="Decrypted Data"
onrowdatabound="GridView2_RowDataBound">
</asp:GridView>
<br />
</form>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
public _Default()
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
cmd = new SqlCommand();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindgridview1();
bindgridview2();
}
}
private void bindgridview2()
{
con.Open();
cmd.CommandText = "select * from [user]";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView2.DataSource = ds;
GridView2.DataBind();
}
private void bindgridview1()
{
con.Open();
cmd.CommandText = "select * from [user]";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string pwdtxt = encodepassword(TextBox2.Text);
con.Open();
cmd.CommandText = "insert into [user](userName,Password)values(@us,@pw)";
cmd.Parameters.AddWithValue("@us", TextBox1.Text);
cmd.Parameters.AddWithValue("@pw", pwdtxt);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
bindgridview1();
bindgridview2();
}
private string encodepassword(string p)
{
string encodepwd = string.Empty;
byte[] encode = new byte[p.Length];
encode = Encoding.UTF8.GetBytes(p);
encodepwd = Convert.ToBase64String(encode);
return encodepwd;
}
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row .RowType ==DataControlRowType .DataRow)
{
string decryptpwd = e.Row.Cells[2].Text;
e.Row.Cells[2].Text = decryptpassword(decryptpwd);
}
}
private string decryptpassword(string decryptpwd)
{
string decryptpass = string.Empty;
UTF8Encoding encode = new UTF8Encoding();
Decoder decode = encode.GetDecoder();
byte[] todecode = Convert.FromBase64String(decryptpwd);
int charcountvariable = decode.GetCharCount(todecode, 0, todecode.Length);
char[] decode_array = new char[charcountvariable];
decode.GetChars(todecode, 0, todecode.Length,decode_array ,0);
decryptpass = new String(decode_array);
return decryptpass;
}
}