Home  >  Article  >  Java  >  java MD5 encryption algorithm

java MD5 encryption algorithm

伊谢尔伦
伊谢尔伦Original
2016-12-10 09:19:171406browse

MD5 (Message Digest Algorithm 5), translated as the fifth edition of the message digest algorithm. According to convention, we reason that there may also be historical versions with names like MD2 and MD3...

Even if we don’t understand the principles of this algorithm at all, we can We can see some clues from the naming. The so-called abstract is a short summary. Like the graduation thesis I wrote, the first part is the abstract. It makes a short and powerful summary of the long article that follows. In fact, the role of MD5 It also smells like this. Let’s take a look at a text describing the function of the MD5 algorithm:

The function of MD5 is to allow large-capacity information to be “compressed” into a confidential format (that is, before signing the private key with digital signature software). A byte string of any length is converted into a large integer of a certain length), which is mainly used to ensure the integrity and consistency of data transmission.

Suppose A wants to send a text file to B in the distance, with 1 million words. When B When receiving a file, how do you know whether the file has been tampered with during the transmission? It would not be good if someone intercepted and tampered with the file content midway. This is when MD5 comes in handy, no matter how big it is. After the file is encrypted by MD5, it will get a fixed-length string, usually 32 bits. At this time, A first encrypts the file with MD5, and the obtained string of ciphertext is also passed to B. When B receives the file, Also use MD5 to encrypt the file and see if the ciphertext obtained is consistent with the one sent by A. If it is consistent, it means that the file is safe. This ensures the integrity of the data transmission.

In fact, we get the ciphertext from the network When downloading files, sometimes there is a piece of MD5 cipher text behind the downloaded file, such as MD5 (e8027a87676ea48b3a3c9b0a4d8d87a0). The function is similar to the example I gave above (I think it is...).

MD5 is a public The irreversible algorithm means that there is no way to directly crack the ciphertext to obtain the source data information. MD5 can encrypt a file of any size and obtain a unique 32-bit string.

A brief understanding of MD5 function, you can look at the code directly.

java code to implement MD5 encryption
Copy code
package com.wang.encryption;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.security.MessageDigest;
/**
* @author yogo.wang
* @date 2016/11/04-1:02 pm.
*/
public class MD5Test {
public static String md5Encode(String msg) throws Exception{

byte[] msgBytes = msg.getBytes("utf-8");
    /**
     * 声明使用Md5算法,获得MessaDigest对象
     */
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    /**
     * 使用指定的字节更新摘要
     */
    md5.update(msgBytes);
    /**
     * 完成哈希计算,获得密文
     */
    byte[] digest = md5.digest();
    /**
     * 以上两行代码等同于 byte[] digest = md5.digest(msgBytes);
     */
    return bytesToHexString(digest);
}
/**
 * 将byte数组转化为16进制字符串形式
 * @param bys
 * @return
 */
public static String byteArr2hexString(byte[] bys){
    StringBuffer hexVal=new StringBuffer();
    int val=0;
    for (int i = 0; i < bys.length; i++) {
        //将byte转化为int  如果byte是一个负数就必须要和16进制的0xff做一次与运算
        val=((int)bys[i]) & 0xff;
        if(val<16){
            hexVal.append("0");
        }
        hexVal.append(Integer.toHexString(val));
    }
    return hexVal.toString();
}
public static void main(String[] args) throws Exception {
    String msg="helloworld";
    String result=md5Encode(msg);
    String result1=md5Encode(msg);
    System.out.println(result);
    System.out.println(result1);
}

}
Copy the code
Run the code, the output results are as follows:

fc5e038d38a57032085441e7fe7010b0
fc5e038d38a57032085441e7fe7010b0

It can be seen that when the same field is encrypted, the ciphertext obtained is always the same. Next, let’s take a look at the principles and applications of MD5 implementation.

MD5 implementation principles and applications
I read some online My friend implemented the md5 encryption code himself, and I can only say that he has a partial understanding of it. I will post it here and introduce some implementation steps on the Internet (just take a look).

The principle of the MD5 algorithm is mainly divided into the following steps. ,

 1) Filling: First, fill the length (bit) of the input information so that the remainder of 512 is equal to 448. The filling method is to fill a 1 and n 0s.

 2) Record information length: Use 64 bits to store the information length before filling. These 64 bits are added after the result of the first step, so that the information length becomes N*512+448+64=(N+1)*512 bits.

 3) Load the standard magic number: The standard magic number is (A=(01234567)16, B=(89ABCDEF)16, C=(FEDCBA98)16, D=(76543210)16). If defined in the program, it should be (A=0X67452301L, B=0XEFCDAB89L, C=0X98BADCFEL, D=0X10325476L).

 4) Four rounds of loop operation: the number of loops is the number of groups (N+1).

Here we mainly introduce the MessageDigest class in java. Check the jdk development document and you can see that this class is located under the java.security package. The document describes MessageDigest as follows:

public abstract class MessageDigest

extends MessageDigestSpi
  此 MessageDigest 类为应用程序提供信息摘要算法的功能,如 MD5 或 SHA 算法。信息摘要是安全的单向哈希函数,它接收任意大小的数据,输出固定长度的哈希值。
  MessageDigest 对象开始被初始化。该对象通过使用 update 方法处理数据。任何时候都可以调用 reset 方法重置摘要。一旦所有需要更新的数据都已经被更新了,应该调用 digest 方法之一完成哈希计算。
  对于给定数量的更新数据,digest 方法只能被调用一次。digest 被调用后,MessageDigest 对象被重新设置成其初始状态。
  实现可随意选择是否实现 Cloneable 接口。客户端应用程可以通过尝试复制和捕获 CloneNotSupportedException 测试可复制性:

MessageDigest md = MessageDigest.getInstance("SHA");

try {

md.update(toChapter1);
 MessageDigest tc1 = md.clone();
 byte[] toChapter1Digest = tc1.digest();
 md.update(toChapter2);
 ...etc.

} catch (CloneNotSupportedException cnse) {

throw new DigestException("couldn&#39;t make digest of partial content");

}

Note that if the given implementation is not copyable and the number of digests is known in advance, then It is still possible to compute intermediate digests by instantiating several instances.

  The functions of the main methods have been given in the comments of the above code, so they will not be introduced here.

The application fields of MD5 can be mainly divided into the following categories:

1. Prevent tampering (file integrity verification) , for example, if I provide file downloads, in order to prevent criminals from adding Trojans to the installation program, I can publish the MD5 output results obtained from the installation files on the website.

2. Prevent direct viewing of plain text (password encryption). Nowadays, many websites store the MD5 value of the user's password in the database when storing the user's password. In this way, even if criminals obtain the MD5 value of the user password in the database, they will not be able to know the user's password.

3. Prevent repudiation (digital signature). For example, A writes a file, and the certification authority uses the MD5 algorithm to generate summary information for the file and keep a record. This can prevent trouble caused by A not admitting the matter in the future.

Although MD5 is an irreversible algorithm, it does not mean that it cannot be cracked. Most users will use fields with special meaning when setting passwords, such as birthday, abbreviated name, etc. If I put your relevant information , guess the passwords you may use, and then encrypt them with MD5 to get a lot of ciphertext, and then get the ciphertext of your password and compare it one by one with my ciphertext library, if the ciphertext can match , then your password itself will be self-defeating.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn