The MD5 Algorithm in hashpasswordforstoringinconfigfile is not a common MD5 algorithm.

Source: Internet
Author: User

When developing a software today, MD5 encryption is used in both B/S and C/S versions.
Which of the following statements is used in ASP. NET?

/** // <Summary>
/// Obtain the MD5 encrypted string
/// </Summary>
/// <Param name = "input"> source plaintext string </param>
/// <Returns> ciphertext string </returns>
Public static string getmd5hash (string input)
...{
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (stroriginal, "MD5 ");
}
;
In winform

/** // <Summary>
/// Obtain the MD5 encrypted string
/// </Summary>
/// <Param name = "input"> source plaintext string </param>
/// <Returns> ciphertext string </returns>
Public static string getmd5hash (string input)
...{
System. Security. cryptography. md5cryptoserviceprovider MD5 = new system. Security. cryptography. md5cryptoserviceprovider ();
Byte [] BS = system. Text. encoding. Default. getbytes (input );
BS = md5.computehash (BS );
System. Text. stringbuilder S = new system. Text. stringbuilder ();
Foreach (byte B in BS)
...{
S. append (B. tostring ("X2"). toupper ());
}
String Password = S. tostring ();
Return password;
}
But found that the same string, two ways to obtain the ciphertext is not the same, and finally found that the original is caused by encoding, ASP. NET in the default is UTF-8 encoding, The winform Program System. Text. encoding. Default. getbytes (input); replace it with system. Text. encoding. utf8.getbytes (input );
Final correct version:

/** // <Summary>
/// Obtain the MD5 encrypted string
/// </Summary>
/// <Param name = "input"> source plaintext string </param>
/// <Returns> ciphertext string </returns>
Public static string getmd5hash (string input)
...{
System. Security. cryptography. md5cryptoserviceprovider MD5 = new system. Security. cryptography. md5cryptoserviceprovider ();
Byte [] BS = system. Text. encoding. utf8.getbytes (input );
BS = md5.computehash (BS );
System. Text. stringbuilder S = new system. Text. stringbuilder ();
Foreach (byte B in BS)
...{
S. append (B. tostring ("X2"). toupper ());
}
String Password = S. tostring ();
Return password;
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ChaoYang0502/archive/2008/05/15/2448156.aspx

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.