asp. net implements md5 encryption

  • 2020-12-13 18:57:02
  • OfStack

MD5 encryption simply means that the 1 segment plaintext through some way of operation to find the ciphertext. For example, expressly for: abcdefg by 1 some 7 ac66c0f148de9519b8bd264312c4d64 operation get ciphertext

It has two properties: 1. No collision, 2. Irreversible.

No collision means: 7 ac66c0f148de9519b8bd264312c4d64 the ciphertext only by abcdefg this Duan Mingwen, besides other plaintext encrypted after its value not equal to 7 ac66c0f148de9519b8bd264312c4d64, That is to say without those two plaintext encryption will result in the same ciphertext.

Irreversible refers to: plaintext encryption through the ciphertext to get ciphertext, but can not be obtained through ciphertext plaintext. That is to say, when we know definitely adcdefg can get 7 ac66c0f148de9519b8bd264312c4d64 by encrypting, but if we know one paragraph 7 ac66c0f148de9519b8bd264312c4d64 after encryption, But can't work out 7 ac66c0f148de9519b8bd264312c4d64 this text is encrypted by who.

Student 1 is going to ask, where exactly should it be used?

Generally speaking, when we do the website login system, the password is saved in ciphertext. Generally speaking, MD5 encryption is used.

After the user fills in the username and password and clicks the registration, we verify that when the user information is stored in the database, the password entered by the user is required to be stored in the password field through MD5 encryption.

Then 1 must have students carefully found that just mentioned MD5 encryption is irreversible, so how to determine the user login password is correct?

Such as the password for abcdefg user Settings, and the storage time is stored encrypted abcdefg after we get the value of the 7 ac66c0f148de9519b8bd264312c4d64, the user can enter the password when login again abcdefg, how do we compare the two are equal?

We cannot convert the encrypted value to the encrypted value, so our usual practice is to re-encrypt the password entered when the user logs in and compare it with the value stored in the database. If the password is equal, it means that the password entered is correct.

OK, basic principles and application scenarios are pretty much done, and finally, how to encrypt MD5 in ES88en.ES89en.

In ES93en. NET, the encryption method of MD5 is very simple, and the code is as follows:


FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();

It is important to note that if MD5 is converted to lowercase when encrypting, it should also be converted to lowercase when validating, maintaining unity 1. In addition, the above method is the 32-bit MD5 encryption method. If it is 16-bit, the middle 16-bit value of the 32-bit encryption result can be taken.

Here's another example, so let's look at 1


/// <summary>
 /// MD5 encryption 
 /// </summary>
 /// <param name="strSource"> Clear text that needs to be encrypted </param>
 /// <returns> return 32 Bit encryption result </returns>
 public static string Get_MD5(string strSource, string sEncode)
 {
     //new 
     System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
     // Gets an array of ciphertext bytes 
     byte[] bytResult = md5.ComputeHash(System.Text.Encoding.GetEncoding(sEncode).GetBytes(strSource));
     // Converts to a string, and takes 9 to 25 position  
     //string strResult = BitConverter.ToString(bytResult, 4, 8);  
     // To a string, 32 position  
     string strResult = BitConverter.ToString(bytResult);
     //BitConverter The converted string is generated between each character 1 A delimiter, which needs to be removed  
     strResult = strResult.Replace("-", "");
     return strResult.ToLower();
 }


Related articles: