<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table align="center"> <tr> <td colspan="2"> <b>Encryption and Decryption of Password</b> </td> </tr> <tr> <td> UserName </td> <td> <asp:TextBox ID="txtname" 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> FirstName </td> <td> <asp:TextBox ID="txtfname" runat="server"></asp:TextBox> </td> </tr> <tr> <td> LastName </td> <td> <asp:TextBox ID="txtlname" runat="server"></asp:TextBox> </td> </tr> <tr> <td> </td> <td> <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /> </td> </tr> </table> </div> <div> <table align="center"> <tr> <td> <b>Encryption of Password Details</b> </td> </tr> <tr> <td> <asp:GridView ID="gvUsers" runat="server" CellPadding="4" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"> <RowStyle BackColor="White" ForeColor="#330099" /> <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" HorizontalAlign="Left"/> </asp:GridView> </td> </tr> </table> </div> <div> <table align="center"> <tr> <td> <b>Decryption of Password Details</b> </td> </tr> <tr> <td> <asp:GridView ID="gvdecryption" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" onrowdatabound="gvdecryption_RowDataBound"> <RowStyle BackColor="White" ForeColor="#330099" /> <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> </asp:GridView> </td> </tr> </table> </div> </form> </body> </html> |
private const string strconneciton = "Data Source=MYCBJ017550027;Initial Catalog=MySamplesDB;Integrated Security=True"; SqlConnection con = new SqlConnection(strconneciton); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindencryptedData(); BindDecryptedData(); } } /// <summary> /// btnSubmit event is used to insert user details with password encryption /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSubmit_Click(object sender, EventArgs e) { string strpassword = Encryptdata(txtPassword.Text); con.Open(); SqlCommand cmd = new SqlCommand("insert into SampleUserdetails(UserName,Password,FirstName,LastName) values('" + txtname.Text + "','" + strpassword + "','" + txtfname.Text + "','" + txtlname.Text + "')", con); cmd.ExecuteNonQuery(); con.Close(); BindencryptedData(); BindDecryptedData(); } /// <summary> /// Bind user Details to gridview /// </summary> protected void BindencryptedData() { con.Open(); SqlCommand cmd = new SqlCommand("select * from SampleUserdetails", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); gvUsers.DataSource = ds; gvUsers.DataBind(); con.Close(); } /// <summary> /// Bind user Details to gridview /// </summary> protected void BindDecryptedData() { con.Open(); SqlCommand cmd = new SqlCommand("select * from SampleUserdetails", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); gvdecryption.DataSource = ds; gvdecryption.DataBind(); con.Close(); } /// <summary> /// Function is used to encrypt the password /// </summary> /// <param name="password"></param> /// <returns></returns> private string Encryptdata(string password) { string strmsg = string.Empty; byte[] encode = new byte[password.Length]; encode = Encoding.UTF8.GetBytes(password); strmsg = Convert.ToBase64String(encode); return strmsg; } /// <summary> /// Function is used to Decrypt the password /// </summary> /// <param name="password"></param> /// <returns></returns> private string Decryptdata(string encryptpwd) { string decryptpwd = string.Empty; UTF8Encoding encodepwd = new UTF8Encoding(); Decoder Decode = encodepwd.GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(encryptpwd); int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); char[] decoded_char = new char[charCount]; Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); decryptpwd = new String(decoded_char); return decryptpwd; } /// <summary> /// rowdatabound condition is used to change the encrypted password format to decryption format /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gvdecryption_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string decryptpassword = e.Row.Cells[2].Text; e.Row.Cells[2].Text = Decryptdata(decryptpassword); } } |
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
25 comments :
wat about 1 way hashing
gud one...............
hi.
Sir I Run Your program but i have One error occured
Invalid length for a Base-64 char array.
please help me What Shhould i do
hi,
check this here i explained clearly how to solve this Invalid length for a Base-64 char array
http://www.aspdotnet-suresh.com/2011/05/invalid-length-for-base-64-char-array.html
This is encoding not encryption. Using only base64 to encode a password is worthless; you may as well use cleartext as this is going to take an intruder about 30 seconds to break.
hi i am getting error in visual studio 2008 i am using c# and sql i got this error
A potentially dangerous Request.Form value was detected from the client (TextBox1="< html >").
cos its not accepting tag type characters. pls help
hi you have to set ValidateRequest="false" on the @Page line your problem will solve
hi
when i m using byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("", "+")); this then it show (String cannot be of zero length.
Parameter name: oldValue) but also update data in database .... and program terminate....
hi
when i m using byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("", "+")); this then it show (String cannot be of zero length.
Parameter name: oldValue) but also update data in database .... and program terminate....
hi
when i m using byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("", "+")); this then it show (String cannot be of zero length.
Parameter name: oldValue) but also update data in database .... and program terminate....
it good to given a video explanation about encrypt and decryption in c#
Thanks for sharing
http://csharpektroncmssql.blogspot.com
THIS IS HORRIBLE ADVICE.
THIS IS NOT ENCRYPTION - IT IS BASE64 ENCODING.
CALLING BASE64 DECODING WILL BREAK YOUR "ENCRYPTION."
You're not doing any sort of encryption here, all you're doing is storing text in base64 format. This is terrible advice, and anyone that takes this article seriously, and uses it to try to secure actual sensitive data will be at real risk because of what you have written here.
REALLY irresponsible to be writing articles on encryption when you obviously have no idea what you're doing. Just to start, you don't even have any of the cryptography namespaces imported into your "solution."
This isn't even amateur - it's a complete misunderstanding of what you're doing. Again - Base64 is just a format - not at all encryption. It is plain text!!!!!!!
Solve This Error Invalid length for a Base-64 char array.
No Need to Cahnge this encryptpwd.Replace('', '+'));
Try this Only Change index of Cell
if (e.Row.RowType == DataControlRowType.DataRow)
{
string decryptpassword = e.Row.Cells[1].Text;
e.Row.Cells[1].Text = Decryptdata(decryptpassword);
}
Thank you very much
Yaah its working superb.Thank u mans
Thanks My Dear
A great post but a person knowing this decryption can decrypt it. so any another way to make a run time random decrypt.
Thanks Once again
some-think wrong with this code:-
byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
Don't be so rough on this guy. True, his example is not demonstrating encrytion, but it does display the foundation. The encrypt and dycrypt function just need to be altered. I would recommend reading through http://msdn.microsoft.com/en-us/library/system.security.cryptography(v=vs.71).aspx. I would also do some more research before starting to store passwords in a database. Your users expect this of a good programmer and web developer.
hi Mr.Dasari
how to set a "UTF8Encode". Which Kind of header files need to add???
help me Mr.dasari
BY
suresh
use this namespace
using System.Text;
sorryy but your this password encryption algorithm is not working in any condition.....please suggest something other method
Thank you very much Disertation writing, We appreciate your interest and suggestions.
Thank You,....Absolutely good coding...
this is very good
which visual studio version is good for web designing 2008 or 2010 pls reply me my mail id pmkarthi87@gmail.com