In short, MD5 encryption is used to obtain the ciphertext of a piece of plain text by some calculation method. For example, the plaintext is abcdefg, And the ciphertext 7ac66c0f148de9519b8bd264312c4d64 is obtained through some column operations.
It has two features: 1. No collision, 2. irreversible.
No collision means that the ciphertext of 7ac66c0f148de9519b8bd264312c4d64 can only be obtained from the abcdefg section. Other plaintext values will not be equal to 7ac66c0f148de9519b8bd264312c4d64 after encryption, that is to say, without the Two plaintext encryption, the same ciphertext will be obtained.
Irreversible means that the plaintext is encrypted to obtain the ciphertext, And the plaintext cannot be obtained through the ciphertext. That is to say, when we know that the plaintext adcdefg can be encrypted to get the token, but if we know that a text segment is encrypted to get 7ac66c0f148de9519b8bd264312c4d64, we cannot figure out who encrypted the token.
Some may ask, where should I use it?
Generally, when logging on to a website, the passwords are stored in ciphertext, and MD5 encryption is generally used.
After the user enters the username and password, click "register". We have passed the verification. To save the user information to the database, we need to first encrypt the password entered by the user by using MD5, store the encrypted ciphertext to the password field.
We must have learned carefully that MD5 encryption is irreversible. How can we determine whether the entered password is correct During User Login?
For example, if the password set by the user is abcdefg and the value 7ac66c0f148de9519b8bd264312c4d64 after abcdefg is encrypted, the user will enter the password abcdefg when logging on again, how can we compare whether the two are equal?
We cannot use the encrypted value to calculate the pre-encrypted value. Therefore, we usually compare the password entered when the user logs on again with the value stored in the database, if they are equal, the entered password is correct.
OK. The basic principles and application scenarios are almost the same. Finally, let's talk about how to implement MD5 encryption in ASP. NET.
In ASP. NET, the MD5 encryption method is simple. The Code is as follows:
Copy codeThe Code is as follows:
FormsAuthentication. HashPasswordForStoringInConfigFile (str, "MD5"). ToLower ();
Note that if MD5 encryption is converted to lowercase, it must be converted to lowercase during verification to ensure consistency. In addition, the above method is a 32-bit MD5 encryption method. If it is a 16-bit, take the 16-bit value in the middle of the 32-bit encryption result.
Here are examples. For more information, see
Copy codeThe Code is as follows:
/// <Summary>
/// MD5 Encryption
/// </Summary>
/// <Param name = "strSource"> plaintext to be encrypted </param>
/// <Returns> returns the 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 ();
// Obtain the ciphertext byte array
Byte [] bytResult = md5.ComputeHash (System. Text. Encoding. GetEncoding (sEncode). GetBytes (strSource ));
// Convert it to a string with 9 to 25 digits
// String strResult = BitConverter. ToString (bytResult, 4, 8 );
// Convert the string to 32-bit
String strResult = BitConverter. ToString (bytResult );
// The character string converted by BitConverter will generate a separator between each character, which needs to be removed
StrResult = strResult. Replace ("-","");
Return strResult. ToLower ();
}