This article is mainly for you to analyze in detail the. NET MD5 encryption and decryption code, with a certain reference value, interested in small partners can refer to
MD5 Introduction:
is to allow bulk information to be "compressed" into a confidential format before signing a private key with a digital signature software (that is, converting an arbitrary-length byte string into a long, large integer). Whether it's MD2, MD4, or MD5, they all need to get a random length of information and generate a 128-bit message digest. Although these algorithms are more or less similar in structure, the design of MD2 is completely different from MD4 and MD5 because MD2 is designed for 8-bit machines, while MD4 and MD5 are for 32-bit computers. The description of these three algorithms and the C language source code are described in detail in Internet RFCs 1321, which is the most authoritative document submitted to the 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" > Required Encrypted strings </param>///<RETURNS>MD5 encrypted strings </returns> public static string Md5encrypt (String STRs Ource) {//Put the string in a byte array byte[] Bytin = System.Text.Encoding.Default.GetBytes (strsource); Establish the key and offset of the cryptographic object byte[] IV = {102, 16, 93, 156, 78, 4, 218, 32};//define offsets byte[] key = {55, 103, 246, 79, 36, 99, 167, 3};//define key//instance DES encryption class DESCryptoServiceProvider mobjcryptoservice = new Descryptoservicepr Ovider (); Mobjcryptoservice.key = IV; MOBJCRYPTOSERVICE.IV = key; ICryptoTransform encrypto = Mobjcryptoservice.createencryptor (); Instance MemoryStream stream Encryption 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//<summary>//MD5 decryption///</summary>/<param Nam E= "Source" > String to Decrypt </param>//<RETURNS>MD5 decrypted string </returns> public static string Md5dec Rypt (String Source) {//Converts the decrypted string to a byte array byte[] Bytin = System.Convert.FromBase64String (Source); Given the decrypted key and offset, the key and offset must be the same as the key and offset at the time of encryption byte[] IV = {102, 16, 93, 156, 78, 4, 218, 32};//define offset byte[] key = {55, 103, 246, 79, 36, 99, 167, 3};//define the key DESCryptoServiceProvider Mobjcryptoservice = new Descryptoserviceprovi Der (); Mobjcryptoservice.key = IV; MOBJCRYPTOSERVICE.IV = key; The instance stream is decrypted 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
Another MD5 encryption method:
MD5 encryption simply means that a piece of clear text through some kind of operation to find the cipher. For example: Clear text is: ABCDEFG through some column operation to get ciphertext 7AC66C0F148DE9519B8BD264312C4D64
It has two properties: 1. No collisions, 2. Irreversible.
No collision means:
7AC66C0F148DE9519B8BD264312C4D64 This cipher text can only be obtained by the ABCDEFG, in addition to other plaintext encryption, its value will never equal to 7AC66C0F148DE9519B8BD264312C4D64, that is, Without those two plaintext encryption will get the same cipher.
Irreversible means:
PlainText is encrypted by encryption, and plaintext cannot be obtained by ciphertext. That is, when we know that plaintext ADCDEFG can be 7ac66c0f148de9519b8bd264312c4d64 by encryption, but if we know that a piece of text is encrypted and get 7ac66c0f148de9519b8bd264312c4d64, But can not figure out the 7ac66c0f148de9519b8bd264312c4d64 this text is by who encrypted.
For example, the user set the password is ABCDEFG, and when stored we store is ABCDEFG encrypted after the value 7ac66c0f148de9519b8bd264312c4d64, then the user will enter the password ABCDEFG again, How do we compare the two to be equal?
We cannot convert the value before encryption by the encrypted value, so it is common practice to encrypt the password that was entered when the user logs in again and match the value stored in the database, and if equal, the correct password is entered.
Need to reference using System.Web.Security;
FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5"). ToLower ();
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.