Code
1
2 public static string Md5 (string str)
3 {
4 string pwd = "";
5 System. Security. Cryptography. MD5 md5 = System. Security. Cryptography. MD5.Create ();
6 // After encryption, it is an array of the byte type. Pay attention to the selection of UTF-8/Unicode encoding.
7 byte [] s = md5.ComputeHash (System. Text. Encoding. UTF8.GetBytes (str ));
8 // convert an array of the byte type into a string by using a loop. This string is formatted as a regular character.
9 for (int I = 0; I <s. Length; I ++)
10 {
11 // Use lowercase letters in hexadecimal format and uppercase letters (X) for the obtained string)
12 pwd + = s [I]. ToString ("X ");
13}
14 return pwd. Length. ToString () + "|" + pwd;
15}
I used this function. When I tested a number of characters, such as "s", "00", and "3", I fell victim to it.
It is neither 32-bit nor 16-bit.
In the past, there were generally four or more characters in the password, but this was not the case. I never cared about it. I accidentally discovered it today.
A surprise.
After careful proof, we found that. Originally in Encoding. UTF8,
That is, the character encoding here, there are many ASCII, UTF7, UTF8, UTF32, and Unicode
The results are strange.
Now, I think you understand that the problem is solved after Unicode is changed.
----------------------------------------------- This function is widely spread on the Internet, but there are many problems
This is much better.
Public string md5 (string str)
{
System. Security. Cryptography. MD5 m = new System. Security. Cryptography. MD5CryptoServiceProvider ();
Byte [] s = m. ComputeHash (UnicodeEncoding. UTF8.GetBytes (str ));
Return BitConverter. ToString (s). Replace ("-","");
}