C # the MD5 encryption result is a 29-character password secret

Source: Internet
Author: User
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 ("-","");
}

 

 

 

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.