Password Encryption MD5 in Asp.net

Source: Internet
Author: User
Tags md5 encryption

MD5 common encryption method (this method is generally not used as the encryption of user passwords)

<% @ Page Language = "C #" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

<SCRIPT runat = "server">
Protected void page_load (Object sender, eventargs E)
{
// Obtain the field to be encrypted and convert it to a byte [] Array
Byte [] DATA = system. Text. encoding. Unicode. getbytes (source. Text. tochararray ());
// Create an encryption service
System. Security. cryptography. MD5 MD5 = new system. Security. cryptography. md5cryptoserviceprovider ();
// Encrypt byte [] Arrays
Byte [] result = md5.computehash (data );
// Convert the encrypted array into a field
String sresult = system. Text. encoding. Unicode. getstring (result );
// Display
Pass_1.text = sresult. tostring ();
}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> MD5 encryption www.itstudy.cn </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
MD5 common encryption: <asp: Label id = "pass_1" runat = "server"> </ASP: Label> <br/>
<Asp: textbox id = "Source" runat = "server" text = "test" autopostback = "true"/>
(Press ENTER)
</Form>
</Body>
</Html>

MD5 password encryption (this is a common method)
Page:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: textbox id = "textbox1" runat = "server" textmode = "password"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "MD5 encryption" onclick = "button#click"/>
<Asp: textbox id = "textbox2" runat = "server"> </ASP: textbox>
</Div>
</Form>
</Body>
</Html>

 
BackgroundCode:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Computer c = new computer ();
}
Protected void button#click (Object sender, eventargs E)
{
// Encrypted as a password
String enpswdstr = MD5 (textbox1.text, 16 );
// Display
Textbox2.text = enpswdstr;
}

Public String MD5 (string STR, int code) // code 16 or 32
{
If (code = 16) // 16-bit MD5 encryption (take 32-bit encryption 9 ~ 25 characters)
{
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (STR, "MD5"). tolower (). substring (8, 16 );
}

If (code = 32) // 32-bit encryption
{
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (STR, "MD5"). tolower ();
}

Return "00000000000000000000000000000000 ";
}
}

Note: In addition to a single encryption method, you can also use multiple encryption methods to obtain a certain number of encrypted digits to form a new encryption result.

Related Article

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.