Implement MD5 encryption in Asp.net Programming

Source: Internet
Author: User

Encryption is required for the current web system.
Select MD5 as usual (in fact, I have used MD5 ^_^)

Two implementations are found.

 

/**////
/// Use MD5 Encryption
///
/// Input data
///
Public static string MD5 (string sourcein)
...{
Md5cryptoserviceprovider md5csp = new md5cryptoserviceprovider ();
Byte [] md5source = system. Text. encoding. utf8.getbytes (sourcein );
Byte [] md5out = md5csp. computehash (md5source );
Return convert. tobase64string (md5out );
}

Public static string MD5 (string STR, int code)
...{
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 ";
}

There are two methods. The first method is to encrypt the byte data through the md5cryptoserviceprovider class.

The second type of direct encryption uses the string type as the parameter, which is more intuitive.

What is the result of two encryption statements ??

Textbox1.text = md51 ("12345 ");
Textbox2.text = md52 ("12345", 32 );

Md51: gnzlduqkcgxmnkfokfhoew =

Md52: 827ccb0eea8a706c4c34a16891f84e7b

Why? Different. I use my intuition to judge that the second is the MD5 encryption I often see.

The first method is hard to understand.

Tip

The second method is to use. tolower () to be compatible with MD5 encryption in ASP;

Compatibility with ASP and encryption of Chinese characters may cause Encoding Problems.
 
 

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.