Let's talk about the MD5 encryption algorithm and the MD5 encryption algorithm.

Source: Internet
Author: User
Tags rounds

Let's talk about the MD5 encryption algorithm and the MD5 encryption algorithm.

MD5 is a secure hashing algorithm. Two different input plain texts do not obtain the same output value. Based on the output value, the original plain text cannot be obtained, that is, the process is irreversible; therefore, there is no ready-made Algorithm for decryption of MD5. We can only use the exhaustive method to scatter the plaintext that may appear with the MD5 algorithm, form a one-to-one ing table between the obtained hash value and the original data, and hash the value by comparing it with the MD5 algorithm used to crack the password in the table, find the original plaintext corresponding to the password to be cracked by matching the ing table.

For information systems or website systems, the MD5 algorithm is mainly used for encryption of user registration passwords. For encryption of common-strength passwords, the following three methods can be used for cracking:

(1) query the password online. Some online MD5 value query websites provide MD5 password value queries. After the MD5 password value is entered, if it exists in the database, the password value can be obtained quickly.

(2) Use an MD5 cracking tool. There are many specialized software for MD5 cracking on the network, which can be cracked by setting the dictionary.

(3) obtain or reset the user's password through social engineering.

Therefore, simple MD5 encryption cannot achieve absolute security, because common MD5 encryption has multiple brute-force cracking methods. Therefore, to ensure the security of information systems or websites, MD5 needs to be modified to enhance its security. This article aims to improve the MD5 encryption algorithm!

2. Md5 Algorithm Application

For example, string encryption and file encryption

String Encryption

方式一:
public string GetMD5String(string pwd)
{
MD5CryptoServiceProvider md5=new MD5CryptoServiceProvide();
byte[] data=System.Text.Encoding.ASCII.GetBytes(pwd);
byte[] md5data=md5.ComputeHash(data);
StringBuilder builder=new StringBUilder();
for(int i=0;i<md5data.Length-1;i++)
{
builder.Append(md5data[i].ToString("X2"));    
}
return builder;
}
方式二:
public string GetMd5(string msg)
{
string cryptStr = "";
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] bytes = Encoding.UTF8.GetBytes(msg);
byte[] cryptBytes = md5.ComputeHash(bytes);
for (int i = 0; i < cryptBytes.Length; i++)
{
cryptStr += cryptBytes[i].ToString("X2");
}
return cryptStr;
}

File encryption

MD5 encrypted file

string GetFileMd5 (string path)
{
string ctyptStr = "";
byte [] cryptBytes;
using (FileStream fs = new FileStream (path, FileMode.Open))
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider ();
cryptBytes = md5.ComputeHash (fs);
}
for (int i = 0; i <cryptBytes.Length; i ++)
{
ctyptStr + = cryptBytes [i] .ToString ("X2");
}
return ctyptStr;
} 

2.1Md5 encryption principle

MD5 processes input information in 512-bit groups, and each group is divided into 16 32-bit groups. After a series of processing, the algorithm output consists of four 32-bit groups. After these four 32-bit groups are cascaded, a 128-bit hash value is generated.

In the MD5 algorithm, you first need to fill in the information so that the result of the length of its byte for the remainder of 512 is equal to 448. Therefore, the byte Length (Bits Length) of the information is extended to N * 512 + 448, that is, N * 64 + 56 Bytes (Bytes), and N is a positive integer. The filling method is as follows. Fill in one and countless zeros after the information until the above conditions are met. Then, the length of the information before filling in 64-bit binary is appended to the result. After these two steps, the length of the current information byte = N * 512 + 448 + 64 = (N + 1) * 512, that is, the length is exactly an integer multiple of 512. The reason for this is to meet the requirements for the length of information in subsequent processing. MD5 contains four 32-bit integer parameters called Chaining Variable, which are A = 0x01234567, B = 0x89abcdef, C = 0xfedcba98, D = 0x76543210. After these four linked variables are set, the four-round cyclic Operation of the algorithm starts. The number of cycles is the number of 512-bit information groups in the information.

Copy the above four linked variables to the other four variables: A to a, B to B, C to c, D to d. The main cycle has four rounds (MD4 only has three rounds), and each cycle is very similar. Perform 16 operations in the first round. Each operation performs a non-linear function operation on three of them in a, B, c, and d, and then adds the fourth variable (a subgrouping and a constant in the text) to the result ).

Then shift the result to the right by an indefinite number, and add one of a, B, c, or d. Finally, replace one of a, B, c, or d with this result. Here are the four non-linear functions used in each operation (one in each round ).


Where ,? It is an exclusive or, between is and, between is or, is an anti-symbol.

If the corresponding bits of X, Y, and Z are independent and even, then each bit of the result should also be independent and even. F is a bitwise operation function. That is, if X, then Y, otherwise Z. Function H is a bitwise parity operator. After all these operations are completed, add A, B, C, and D to a, B, c, and d respectively. Then, use the next group of data to continue running the algorithm. The final output is Cascade A, B, C, and D. The final result of A, B, C, and D is the output result. A is the low position, D is the high position, and DCBA makes up A 128-bit output result.

2.2 MD5 Security

From the security point of view, the MD5 output is 128 bits. If a pure strong attack is used to find a message, the calculation difficulty of having a given Hash value is 2128, it takes 1000000000x1.07 to test 1022 messages per second on a computer. If we use the birthday attack method, we need to test 264 messages for two messages with the same Hash value. A computer that can test 1000000000 messages per second takes 585.

2.3 Application of Md5 Encryption Algorithm

The MD5 encryption algorithm is widely used because of its high security and commercial availability, the md5 algorithm is mainly used in digital signatures, file integrity verification, and password encryption.

3. Improved Encryption Method

3.1 current defects in md5 Encryption

In the current information system, the use of md5 encryption methods mainly references files containing md5 encryption function code on the script page. Take asp script as an example, on the page to be called, add md5.asp to encrypt the function code file for md5, and then directly call the function MD5 (sMessage). The value after md5 encryption is divided into 16-bit and 32-bit, if md5 = LCase (WordToHex (a) & WordToHex (B) & WordToHex (c) & WordToHex (d) is used in the MD5 encryption function, the value is 32 bits, if MD5 = LCase (WordToHex (B) & WordToHex (c) is used, it indicates 16 bits. For example, to encrypt the plaintext value "123456", there are two md5 values, as shown below:

A = 123456 password = md5 (A) = 49ba59abbe56e057 password = md5 (A) = e10adc3949ba59abbe56e057f20f883e if the encrypted md5 value is saved in the database, when the website has injection or other vulnerabilities, intruders are very likely to obtain the user's password value.

If the encrypted md5 value is directly stored in the database, when the website has injection or other vulnerabilities, intruders are very likely to obtain the user's password value and obtain the password through md5 online query or brute-force cracking.

3.2 Improved encryption method based on md5 Algorithm

The method mentioned in this Article is to change the ciphertext based on the md5 Encryption Algorithm for plaintext (password) encryption, and intercept a piece of data in the password and discard it,

Then, the random function is used to fill in the discarded data, and the number of digits after md5 encryption is not changed throughout the process. The encryption process is described as follows using algorithms:

(1) perform md5 encryption on the plaintext password to obtain the ciphertext md5 (password ).

(2) Use the truncation function to intercept the encrypted ciphertext, from the beginnumber position start to intercept the number value, get the password A, where A = left (md5 (password), beginnumber-1 ).

(3) Use the truncation function to intercept the value B after the number digits of the encrypted plaintext, where B = right (md5 (password), md5-digit-(beginnumber + number-1 )).

(4) use the random function gen_key (number) to fill in the intercepted number value.

(5) The converted password value is encrypt_password = A & get_key (number) & B

Variable description:


The decryption process is similar to the encryption process. First, encrypt the input plaintext, and then extract the First Half of the beginnumber to obtain A', and then the second half to obtain B ′,

Then, read the and B parts of the password from the database. If A = A' and B = B ', the password entered by the user matches the password in the database.

3. key code and implementation

The key implementation code of the improvement method mentioned in this article is as follows:


4. Discussion and conclusion

Some people have also proposed to modify functions or variables in the md5 encryption algorithm to enhance the security of the original md5 algorithm,

However, after this method modifies the md5 original function or variable, it cannot verify whether the strength of the modified md5 algorithm is consistent with that of the original algorithm.

The method proposed in this article is based on the original md5 encryption, by intercepting a string of a certain number of digits in the ciphertext, and filling with a random number,

The final ciphertext is encrypted by md5, but its value is already quite different. Therefore, the conventional md5 cracking method will never crack its original password value,

This ensures data security. Although there are many attack methods, such as SQL injection and cross-site attacks, you can easily obtain values in the database,

This method is used to encrypt the data. Before the website or system code is leaked, the data is relatively safe, so it has a certain reference value.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.