Java generates MD5 encrypted string code example

 

What is MD5?

 

      MD5 is the abbreviation of message-digest algorithm 5 (message-digest algorithm), which is a hash function used in the field of computer security. It is widely used in encryption and decryption technology. It is like the "digital fingerprint" of a file. .

   Whether it is an executable program, an image file, a temporary file or other types of files, there is a unique corresponding MD5 information value. Just like human genes, different people have different genes.

 

The role of MD5:

 

     1. Through the MD5 value of the same file, you can check whether the file has been "tampered". We often encounter such a problem. When downloading things, sometimes the downloaded things are not what we want or have a virus. The virus can be checked and killed, but the file has been modified. Sometimes, you do not notice it yourself. arrived. This can be verified by the MD5 value (generally regular websites will provide the MD5 value of the file).

 

     2. MD5 is also used for login authentication of operating systems, such as Unix. When a user logs in, the system performs a set of arithmetic operations on the password entered by the user with MD5, and then compares it with the MD5 value stored in the file system. Make sure the password is correct.

 

      MD5 values ​​cannot be converted to raw strings. Because MD5 maps a "string" of arbitrary length to a large 128-bit integer, it is difficult to reverse the original string through a large 128-bit integer.

 

     Deciphering MD5 is to use something called the pigeonhole principle. To use it to crack MD5, you need to create N multiple files, and find the same MD5 files from them, but the MD5 content of the found files is not necessarily the same. . For files, MD5 is still quite secure, but for passwords, it is certain. If the MD5 of two people's passwords are the same, if one's password is a, then the other person can log in directly with the same MD5 password b. account, and directly steal the account.

<script type="text/javascript">// <![CDATA[ /*iteye博客内页Banner-468*60,创建于2016-5-31*/ var cpro_id = "u1405021"; // ]]></script><script type="text/javascript" src="http://cpro.baidustatic.com/cpro/ui/c.js"></script>

(1) Usernames and passwords are stored in the databases generally used, and the passwords are not stored in plain text.

      Sometimes MD5 passwords are used, and many languages ​​provide methods or functions for generating strings into MD5 passwords. The encryption algorithm of MD5 is public.

      Sometimes you can also use your own string encryption algorithm, which only you know.

(2) The process of cracking MD5 is to first calculate the MD5 value of a large number or all possible strings, and then query it to crack. Although some websites stipulate that the number of digits in the password is between 6 and 20, it is quite troublesome and slow to calculate so many strings in advance and effectively organize storage and query.

    Because the number of digits of MD5 is fixed, such as 16, 32, 64, and the combination and length of strings are infinite, there is a conflict. But if you know that the length of the string before encryption has a fixed range, such as 6~20, this can still be cracked.

    However, if the length of characters before encryption is not known then this is infinite. It seems that no one has been able to crack it yet.

       MD5 password cracking website: You can Baidu search " MD5 decryption "

(3) A java module program for generating the MD5 password of a given string is given below.

 

import java.security.MessageDigest;

public class Md5Test {
	public void toMD5(String plainText) {
	     try {
	        //Generate a MessageDigest object that implements the specified digest algorithm.
	        MessageDigest md = MessageDigest.getInstance("MD5");  
	        //Update the digest with the specified byte array.
	        md.update(plainText.getBytes());
	        // The hash calculation is done by performing final operations like padding.
	        byte b[] = md.digest();
	        //Generate the specific md5 password to the buf array
	        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));
	        }
	        System.out.println("32-bit: " + buf.toString());// 32-bit encryption
	        System.out.println("16-bit: " + buf.toString().substring(8, 24));// 16-bit encryption is actually the interception after 32-bit encryption
	     }
	     catch (Exception e) {
	       e.printStackTrace ();
	     }
	   }
	   
	   public static void main(String agrs[]) {   
	       new Md5Test().toMD5("LXD");//Encrypt LXD
	       System.out.println("Encrypted source code: LXD");
	   }
}

 

 

 

 

Guess you like

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