This article is copyrighted by chenyangASP. It can be reproduced, copied, pasted, and indicated in the source, but cannot be modified!
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 use
HashPassWordForStoringInConfigFile method to solve the problem.
The purpose is to encrypt the password of the configuration file and even cookies.
The HashPasswordForStoringInConfigFile method is very easy to use and supports "SHA1" and "MD5" hashing algorithms.
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>
<Head>
<Script language = "VB" runat = server>
Sub encryptString (Src As Object, E As EventArgs)
SHA1.Text = CookieAuthentication. HashPasswordForStoringInConfigFile (txtPassword. Text, "SHA1 ")
MD5.Text = CookieAuthentication. HashPasswordForStoringInConfigFile (txtPassword. Text, "MD5 ")
End Sub
</Script>
</Head>
<Body>
<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
Source: http://www.vipcn.com/InfoView/Article_10312.html