MD5 encryption in. net

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.