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