Whenever we use a database development website, we must protect user data, which is necessary.
Hackers can steal passwords, causing serious damage to personal privacy. The best way is not to store the original password, but to encrypt it and store it in the database.
To verify the user, we only need to encrypt the password entered by the user again and compare it with the records in the database.
In ASP, we need additional object encryption.
However, in Asp.net, the SDK can solve the problem through the hashpasswordforstoringinconfigfile method of the cookieauthentication class in system. Web. Security namespace.
The purpose of this operation is to encrypt the password of the configuration file, and even the cookie. hashpasswordforstoringinconfigfile method is very easy to use. It also supports the hash algorithms of "sha1" and "MD5.
To understand the "hashpasswordforstoringinconfigfile" method, let's create a small ASP. NET page and translate the input string into the password in sha1 and MD5 formats.
<% @ Import namespace = "system. web. security "%> <HTML> Sha1.text = cookieauthentication. hashpasswordforstoringinconfigfile (txtpassword. Text, "sha1 ")
Md5.text = cookieauthentication. hashpasswordforstoringinconfigfile (txtpassword. Text, "MD5 ")
End sub </SCRIPT> <Form runat = Server>
<P> <B> original clear text password: </B> <br> <asp: textbox id = "txtpassword" runat = server/> <asp: button runat = "server" text = "encrypt string" onclick = "encryptstring"/> </P>
<P> <B> encrypted password in sha1: </B> <asp: Label id = "sha1" runat = server/> </P>
<P> <B> encrypted password in MD5: </B> <asp: Label id = "MD5" runat = server/> </P>
</Form>
</Body>
</Html>
It is easy to encrypt a string. In order to make it easier to use it, I made a function. The source code of the function is provided below.
Function encryptpassword (passwordstring as string, passwordformat as string) as string
If passwordformat = "sha1" then
Encryptpassword = cookieauthentication. hashpasswordforstoringinconfigfile (passwordstring, "sha1 ")
Elseif
Passwordformat = "MD5" then
Encryptpassword = cookieauthentication. hashpasswordforstoringinconfigfile (passwordstring, "MD5 ")
Else
Encryptpassword = ""
End if
End Function
The following is the C # version.
Public String encryptpassword (string password, string passwordformate)
...{
String encryptpassword;
If (passwordformate = "sha1 ")
Encryptpassword = formsauthentication. hashpasswordforstoringinconfigfile (password, "sha1 ");
Else if (passwordformate = "MD5 ")
Encryptpassword = formsauthentication. hashpasswordforstoringinconfigfile (password, "MD5 ");
Else
Encryptpassword = "";
Return encryptpassword;
}