Encryption of java string MD5

Introduction

MD5 Message-Digest Algorithm (English: MD5 Message-Digest Algorithm), a widely used cryptographic hash function, can generate a 128-bit (16-byte) hash value to ensure information transmission Complete and consistent. MD5 was designed by Ronald Linn Rivest, an American cryptographer, and published in 1992 to replace the MD4 algorithm. The procedures of this algorithm are regulated in the RFC 1321 standard. After 1996, the algorithm was confirmed to have weaknesses and could be cracked. For data that requires high security, experts generally recommend switching to other algorithms, such as SHA-2. In 2004, it was confirmed that the MD5 algorithm could not prevent collisions, so It is not suitable for security certification, such as SSL public key certification or digital signature.


1 What is MD5 algorithm

MD5 Message-Digest Algorithm (English: MD5 Message-Digest Algorithm), a widely used cryptographic hash function, can generate a 128-bit (16-byte) hash value to ensure Information transmission is complete and consistent.

2 MD5 function

Input information of any length, after processing, the output is 128-bit information (digital fingerprint);
different results obtained by different inputs (uniqueness);

3 MD5 is not an encryption algorithm

I think people do not belong because they think you can not turn get text from the ciphertext (hash value), that is, without the decryption algorithm, which some people think that can only belong MD5 algorithm, can not be called an encryption algorithm;
that man is a part of Because they feel that the original text cannot be seen after MD5 processing, that is, the original text has been encrypted, so they think MD5 is an encryption algorithm; I personally support the former, just as BASE64 algorithm can only be regarded as encoding.

4 MD5 algorithm is irreversible

The reason why MD5 is irreversible is that it is a hash function, which uses a hash algorithm, and part of the original information is lost during the calculation process.

However, one thing worth pointing out is that in theory, one MD5 may indeed correspond to an infinite number of original texts, because MD5 is limited and there can be an infinite number of original texts. For example, the mainstream MD5 maps any length of "byte string to a 128bit large integer. That is, there are 2^128 possibilities, about 3.4*10^38. This number is limited, but the world There are countless possibilities for the original text that can be used for encryption.

However, one thing to note is that, as far as possible, this is a theoretical limit to infinity, but the problem is that this infinity is not completely established in real life, because on the one hand, the length of the original text is often limited in reality (using commonly used passwords as For example, most people are within 20). On the other hand, it is very difficult to find that two original texts correspond to the same MD5 (professional call this is called hash collision) value, so in a sense, I want to build within a certain range The one-to-one correspondence between the MD5 value and the original text is entirely possible. Therefore, the most effective attack method for MD5 is the rainbow table. For details, you can find out through Google.

MD5 is equivalent to over-loss compression.

5 MD5 uses

1. To prevent tampering:
1) For example, to send an electronic document, before sending, I first get the MD5 output result a. Then after the other party receives the electronic document, the other party also gets an MD5 output result b. If a is the same as b, it means it has not been tampered with.
2) For example, if I provide file downloads, in order to prevent criminals from adding Trojan horses to the installation program, I can publish the MD5 output result obtained from the installation file on the website.
3) SVN detects whether the file has been modified after CheckOut, and MD5 is also used.

2. Prevent the plaintext from being seen directly:
Many websites now store the MD5 value of the user's password when storing the user's password in the database. In this way, even if criminals get the MD5 value of the user's password in the database, they cannot know the user's password. (For example, in the UNIX system, the user's password is encrypted with MD5 (or other similar algorithms) and stored in the file system. When the user logs in, the system calculates the password entered by the user into an MD5 value, and then goes and saves it The MD5 value in the file system is compared to determine whether the entered password is correct. Through this step, the system can determine the legitimacy of the user's login system without knowing the clear code of the user password. This can not only prevent the user from logging in to the system. The password of is known by users with system administrator rights, and it also increases the difficulty of password cracking to a certain extent.)

3. Prevent denial (digital signature):
This requires a third-party certification body. For example, A writes a file, and the certification body uses MD5 algorithm to generate summary information for this file and make a record. If A later says that this document was not written by him, the authority only needs to regenerate the summary information for this document and compare it with the recorded summary information. If it is the same, it proves that it was written by A. This is the so-called "digital signature".

6 MD5 security

It is generally believed that MD5 is very safe, because the time for brute force cracking is unacceptable for ordinary people. In fact, if the user's password is MD5 processed and then stored in the database, it is actually very insecure. Because the user's password is relatively short, and many users' passwords use birthday, mobile phone number, ID number, phone number, etc. Or use some commonly used auspicious numbers, or an English word. If I process the commonly used passwords in MD5 first, store the data, and then match with your MD5 results, then I may get the plaintext. For example, a certain MD5 cracking website http://www.cmd5.com/default.aspx, so the current password policy of most websites is to force users to use a combination of uppercase and lowercase letters to improve the security of user passwords.

7 code implementation

package xx.study.design.test;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {

    public static void main(String[] args) {

        System.out.println(MD5("hello md5"));
        //输出:741fc6b1878e208346359af502dd11c5
    }

    /**
     * Description 获取字符串MD5值
     *
     * @param sourceStr
     */
    private static String MD5(String sourceStr) {
        String result = "";
        try {
            MessageDigest md = null;

                md = MessageDigest.getInstance("MD5");

            md.update(sourceStr.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();

        } catch (NoSuchAlgorithmException e) {
            // log.error(e.getMessage());
        }
        return result;
    }
}

 

 

Guess you like

Origin blog.csdn.net/h4241778/article/details/108569022