Encrypting password and store in a Database using sqlserver
Encrypting password
HashFunction:To Encrypt a Password use HashFunction.HashFunction sends your password to a function which spits out your password all garbled. To hash a password, you can use the built in method called HashPasswordForStoringInConfigFile.
You can call it like this:
FormsAuthentication.HashPasswordForStoringInConfigFile("password","md5"). The first parameter is the password to be hashed. The second parameter is either "md5" or "sha1" depending on which hashing function you use.
Eg:
string str = FormsAuthentication.HashPasswordForStoringInConfigFile("dotnetfunda ", "MD5");
string str1 = FormsAuthentication.HashPasswordForStoringInConfigFile("dotnetfunda.com", "sha1");
Response.Write("dotnetfunda " + str);
Response.Write("<br>dotnetfunda.com " + str1);
Response.Write("<br>dotnetfunda " + str);
OutPut:
dotnetfunda =B9B93AF7A7DA8881888EDD632D7A23E7
dotnetfunda.com =0E34AD60C96D6A480C0FC9D2B82796F3F43EC833
dotnetfunda =B9B93AF7A7DA8881888EDD632D7A23E7
storing Hashvalue in a database:
create loginuser Table with two columns as username varchar(50),password varchar(50).
Drag and drop two Textboxes as txtusername and txtpassword on a WebPage.
String user_name=txtusername.Text;
String hasspassword= FormsAuthentication.HashPasswordForStoringInConfigFile(txtpassword.Text,"MD5”);
//Converting a password in to HashPassword
SqlConnetion conn=New SqlConnection(“your connection”);
Conn.Open();
SqlCommand cmd=new SqlCommand(“insert into loginuser(username,password) values(‘”+user_name +”’,’”+ hasspassword +”’)”,conn);
cmd.ExecuteNonquery();