. NET MD5 encryption and decryption code parsing, md5 encryption and decryption
MD5 introduction:
It is used to compress large-capacity information into a confidential format (that is, to convert a byte string of any length into a large integer) before signing a private key using the digital signature software ). Whether MD2, MD4, or MD5, they all need to obtain a random length information and generate a 128-bit information digest. Although the structures of these algorithms are more or less similar, the design of MD2 is completely different from that of MD4 and MD5, because MD2 is designed and optimized for eight-bit machines, MD4 and MD5 are designed for 32-bit computers. The description of the three algorithms and the C language source code are described in detail in Internet RFCs 1321. This is the most authoritative document, which was submitted to IETF by Ronald L. Rivest in August 1992.
Code:
String JiaMi = Md5Encrypt (LoginPwd); string JieMi = Md5Decrypt (JiaMi ); # region MD5 encryption // <summary> // MD5 encryption // </summary> // <param name = "strSource"> string to be encrypted </param >/// <returns> MD5 encrypted string </returns> public static string Md5Encrypt (string strSource) {// put the string in the byte array byte [] bytIn = System. text. encoding. default. getBytes (strSource); // create the key and offset for the encryption object byte [] iv = {102, 16, 93,156, 78, 4,218, 32 }; // define the offset byte [] key = {55,103,246, 79, 36, 99,167, 3}; // define the key // instance DES encryption class DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider (); mobjCryptoService. key = iv; mobjCryptoService. IV = key; ICryptoTransform encrypto = mobjCryptoService. createEncryptor (); // instance MemoryStream encrypted file System. IO. memoryStream MS = new System. IO. memoryStream (); CryptoStream cs = new CryptoStream (MS, encrypto, CryptoStreamMode. write); cs. write (bytIn, 0, bytIn. length); cs. flushFinalBlock (); return System. convert. toBase64String (ms. toArray ());} # endregion # region MD5 decryption // <summary> // MD5 decryption // </summary> // <param name = "Source"> string to be decrypted </param> // <returns> MD5 decrypted string </returns> public static string Md5Decrypt (string Source) {// convert the decryption string to a byte array byte [] bytIn = System. convert. fromBase64String (Source); // returns the decrypted key and offset. The key and offset must be the same as the Encrypted Key and offset. byte [] iv = {102, 16, 93,156, 78, 4,218, 32}; // defines the offset byte [] key = {55,103,246, 79, 36, 99,167, 3}; // defines the key DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider (); mobjCryptoService. key = iv; mobjCryptoService. IV = key; // The instance stream to decrypt the System. IO. memoryStream MS = new System. IO. memoryStream (bytIn, 0, bytIn. length); ICryptoTransform encrypto = mobjCryptoService. createDecryptor (); CryptoStream cs = new CryptoStream (MS, encrypto, CryptoStreamMode. read); StreamReader strd = new StreamReader (cs, Encoding. default); return strd. readToEnd () ;}# endregion
AnotherMD5 encryption method:
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:
7ac66c0f148de9519b8bd264312c4d64 this section of ciphertext can only be obtained from the abcdefg section of the plain text, in addition to other plaintext encryption, the value is definitely not equal to the ciphertext, that is, without the Two plaintext encryption will get the same ciphertext.
Irreversible means:
The plaintext is encrypted to obtain the ciphertext, but 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.
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.
Using System. Web. Security;
FormsAuthentication. HashPasswordForStoringInConfigFile (str, "MD5"). ToLower ();
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.