SoFunction
Updated on 2025-03-07

Implement md5 encryption

MD5 encryption simply means to find the ciphertext through some calculation method. For example: the plain text is: abcdefg through some column operations to obtain the ciphertext 7ac66c0f148de9519b8bd264312c4d64

It has two characteristics: 1. No collision and 2. Irreversible.

No collision means: 7ac66c0f148de9519b8bd264312c4d64 This ciphertext can only be obtained from the plaintext abcdefg. In addition, other plaintexts have the value of the encrypted, and the value will definitely not be equal to 7ac66c0f148de9519b8bd264312c4d64, which means that if there are no two plaintexts, the same ciphertext will be obtained.

Irreversible means: the plain text obtains the cipher text after encryption, but the plain text cannot be obtained through the cipher text. In other words, when we know that the plain text adcdefg can obtain 7ac66c0f148de9519b8bd264312c4d64 through encryption, but if we know that a certain text is encrypted, we get 7ac66c0f148de9519b8bd264312c4d64, we cannot calculate who encrypted the text 7ac66c0f148de9519b8bd264312c4d64.

Then some students will definitely ask, where should they be used?

Generally speaking, when we log in to the system on the website, the password is saved in ciphertext, and we generally use MD5 encryption.

After the user fills in the user name and password, we verify and pass the verification. When we want to store the user's information into the database, we need to first save the encrypted password into the password field through MD5 encryption.

Then some students will have carefully discovered that they just mentioned that MD5 encryption is irreversible. So how do you determine whether the password entered by the user is correct when logging in?

For example, the password set by the user is abcdefg, and when storing, we store the value obtained after abcdefg is encrypted. 7ac66c0f148de9519b8bd264312c4d64. Then when the user logs in again, the password abcdefg will be entered. How do we compare whether the two are equal?

We cannot convert the encrypted value to the value before encryption, so our usual approach is to compare the password entered when the user logs in again with the value stored in the database. If it is equal, it means that the entered password is correct.

OK, the basic principles and application scenarios are basically the same. Finally, let’s talk about how to perform MD5 encryption in China.

The encryption method of MD5 is very simple, the code is as follows:

Copy the codeThe code is as follows:

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

It should be noted that if the conversion of MD5 in lower case is performed during encryption, it should also be converted to lower case during verification to maintain uniformity. In addition, the above method is a 32-bit MD5 encryption method. If it is 16-bit, the value of the intermediate 16-bit of the 32-bit encryption result is just to take the value of the 16-bit value of the 32-bit encryption result.

There are also examples here, please refer to them
 

Copy the codeThe code is as follows:

/// <summary>
/// MD5 encryption
 /// </summary>
/// <param name="strSource">Plaintext that requires encryption</param>
/// <returns>Return 32-bit encryption result</returns>
 public static string Get_MD5(string strSource, string sEncode)
 {
     //new
     .MD5 md5 = new .MD5CryptoServiceProvider();

//Get ciphertext byte array
     byte[] bytResult = ((sEncode).GetBytes(strSource));

//Convert to string and take 9 to 25 bits
     //string strResult = (bytResult, 4, 8); 
//Convert to string, 32 bits

     string strResult = (bytResult);

//The string converted from BitConverter will generate a separator in the middle of each character, which needs to be removed
     strResult = ("-", "");

     return ();
 }