MD5 encryption algorithm detailed introduction

Hello everyone, we will now explain the knowledge about encryption. When it comes to encryption, I think I have to mention MD5, because this is a special encryption method. What is it special? Now we will start to learn it

Full name: message-digest algorithm 5

The translation is: Information Digest Algorithm 5

Encryption and digest, are not the same

The encrypted message is complete; it has a decryption algorithm to get the original data;

The message obtained from the digest is incomplete; through the digested data, the original data cannot be obtained;

So, when you see a lot of people saying, md5, encryption, decryption, just laugh.


MD5 length

Some people say that md5, 128-bit, 32-bit, 16-bit, how long is md5?

The length of md5, the default is 128bit, that is, 128 binary strings of 0 and 1.

This expression is very unfriendly.

So the binary is converted to hexadecimal, and every 4 bits represents a hexadecimal,

So 128/4 = 32 is 32 bits after changing to hexadecimal representation.


Why is there md5 on the Internet that is 16-bit?

There are many posts on the Internet, md5 32-bit 16-bit encryption difference.

Take a closer look at the 32-bit and 16-bit md5 values ​​generated by the admin...

    search result:

    md5(admin,32) = 21232f297a57a5a743894a0e4a801fc3

    md5(admin,16) = 7a57a5a743894a0e

See it!

In fact, the 16-bit length is derived from the 32-bit md5 value. It is obtained by removing the first eight bits and the last eight bits of the 32-bit md5.

The role of MD5

①Consistency test, the top example

②Digital signature, or the top example. I just saw a fingerprint on md5, and pressed a fingerprint to show that it is unique.

③Security access authentication, this is the usual system design problem.

When a user registers, the password will be encrypted by md5 and stored in the database. This prevents malicious manipulation by those who can see the database data.

 

Is md5 unique?

Is md5 feasible as a primary key in a database? This involves a question, is the md5 value unique? The answer is, not only.

That is, a raw data, corresponding to only one md5 value;

But one md5 value may correspond to multiple raw data.


Can md5 be cracked?

md5 is irreversible, that is, there is no corresponding algorithm, and the original data is obtained in reverse from the produced md5 value.

But if you use brute force, that's another story.

Generate MD5 value in Java language


Features
public class MD5Test {
 
    //main test class
    public static void main(String[] args) {
        String result = getMD5("aaa");
        System.err.println(result);
    }
 
    /**
     * Generate md5
     * @param message
     * @return
     */
    public static String getMD5(String message) {
        String md5str = "";
        try {
            //1 Create an object that provides the information digest algorithm, initialized to the md5 algorithm object
            MessageDigest md = MessageDigest.getInstance("MD5");
 
            //2 Turn the message into a byte array
            byte[] input = message.getBytes();
 
            //3 After the calculation, the byte array is obtained, which is the 128 bits
            byte[] buff = md.digest(input);
 
            //4 Change each byte of the array (one byte occupies eight bits) into hexadecimal and connect them into an md5 string
            md5str = bytesToHex(buff);
 
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return md5str;
    }
 
    /**
     * binary to hexadecimal
     * @param bytes
     * @return
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuffer md5str = new StringBuffer();
        //Replace each byte of the array into hexadecimal and connect it to an md5 string
        int digital;
        for (int i = 0; i < bytes.length; i++) {
             digital = bytes[i];
 
            if(digital < 0) {
                digital += 256;
            }
            if(digital < 16){
                md5str.append("0");
            }
            md5str.append(Integer.toHexString(digital));
        }
        return md5str.toString().toUpperCase();
    }
}
tagged with JAVA , md5 , MD5 length , encryption , unique , digest , generation , cracking
   
1. Fixed length :

  • No matter how long the string is, the length after encryption is the same.
    Function : It is convenient
    for statistics and management of usual information

  • 2. Easy to calculate :

    The process of string and file encryption is easy.
    Function: It is easy for developers to understand and make encryption tools

  • 3. Subtlety

    A file, no matter how big it is, as small as a few k or as large as a few G, as long as you change a character in it, the MD5 value will change .
    Function:
    Many software and applications provide download resources on the website, which contain the MD5 of the file. After downloading , the user only needs to use the tool to test the downloaded file , and through comparison, we can know whether the file has been changed or not .

  • 4. Irreversibility

    You clearly know the ciphertext and encryption method , but you cannot calculate the original password in reverse .
    Function: Based on this feature, many secure encryption methods are used. This greatly improves data security.

encrypted string

logical thinking:

  • 1. Get the information summary object: md5

    Obtained through the constructor of the information summary singleton :

    MessageDigest md5 = MessageDigest.getInstance("MD5");
    
    • 1
    • 2
  • 2. The information digest object is to digest the byte array , so first get the byte array of the string .

    byte[] bytes = str.getBytes();
    
    • 1
    • 2
  • 3. The message digest object digests the byte array to get the digest byte array :

    byte[] digest = md5.digest(bytes);
    
    • 1
    • 2
  • 4. Convert each byte in the summary array to hexadecimal, and put them together to get the MD5 value.
    (PS, some conversions get 6 f in front, such as: ffffff82 , this is Because there are 6 groups of 4 1s in front , it is good to change these 6 groups of 1111 to 0 in advance , and then convert to hexadecimal, there will be no f)
    (in fact, you can also remove f later)

Article from: http://www.weixuehao.com/archives/474

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325808912&siteId=291194637