1. Knowledge points involved
1.MD5
MD5 (Message Digest Algorithm 5) is a widely used "message-digest algorithm". The "Message-Digest Algorithm" is actually a single-term hash function. The data block obtains a fixed-length hash value through a one-way hash function. The signature of the data block is to calculate the hash value of the data block. The hash value of the MD5 algorithm is 128 bits.
2. Use the Create method of the base class to create MD5 objects
The Create method of its base class should be used to create an MD5 object. If you still use the MD5CryptoServiceProvider class, you will get the warning SYSLIB0021.
Use the () method to create MD5 objects to be more concise and easy to read. This method is also suitable for other outdated encryption classes, such as SHA1CryptoServiceProvider, SHA256CryptoServiceProvider, etc. By using the Create() method of the base class, the code will be more concise, readable and best practice-compliant.
using ; // ... MD5 md5 = ();
3. Encrypt each byte and convert it into a hexadecimal string
str += md5data[i].ToString("x").PadLeft(2, '0');//Encrypt the traversed bytes
Specifically, this code performs the following:
- Use for loop to loop through each byte in the byte array md5data.
- Converts the current byte to a hexadecimal string, represented by the "x" format specifier. For example, the byte value 0x1A will be converted to the string "1a".
- Use the PadLeft method to align the converted hexadecimal string left to ensure its length is 2. If the string length is less than 2, the character '0' will be filled on the left. For example, the string "1a" will be converted to "01a".
- Append the processed hexadecimal string to the string variable str.
- Through these operations, the raw byte data will be converted into a string containing hexadecimal characters. This is part of the MD5 encryption process, which will eventually generate a 32-character MD5 hash.
(Byte[])
Calculates the hash value of the specified byte array.
public byte[] ComputeHash (byte[] buffer); parameter buffer Byte[] To calculate the input of its hash code。 return Byte[] The calculated hash code。 exception ArgumentNullException buffer for null。 ObjectDisposedException The object has been released。
2. Encrypt password using MD5 algorithm
// Encrypt password using MD5 algorithmusing ; namespace _155 { class Program { /// <summary> /// Encrypt the traversed bytes /// </summary> /// <param name="strPwd">Input string to be encrypted</param> /// <returns name="str">Returns encrypted value</returns> public static string Encrypt(string strPwd) { MD5 md5 = (); byte[] data = (strPwd);//Encode characters into a byte sequence byte[] md5data = (data); // Calculate the hash value of the data byte array (); //Clear MD5 objects string str = ""; //Define a variable to record the encrypted password for (int i = 0; i < - 1; i++) { str += md5data[i].ToString("x").PadLeft(2, '0'); } return str; } static void Main(string[] args) { (args); while (true) { ("Please enter your password:"); string P_str_Code = ()!; ("The result after encryption using MD5 is:" + Encrypt(P_str_Code)); } } } } //Run result:/* Please enter your password: 123456789Qq The result after encryption using MD5 is: 6d28b4834bd6a9e384be01c131a8f2 Please enter your password: 123456789QQ The result after encryption using MD5 is: 46e7640c5b2ddeab142d71d872163b Please enter your password: 123456789qq The result after encryption using MD5 is: 9c32d6523556378b8b1632720f7ab1 Please enter your password: */
This is the article about the example code of C# using the MD5 algorithm to encrypt passwords. For more related C# MD5 algorithm password encryption content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!