Analysis of MD5 Encryption Usage Developed by Java Web

  • 2021-07-24 11:35:37
  • OfStack

This article illustrates the use of MD5 encryption developed by Java Web. Share it for your reference. The details are as follows:

MD5 is the abbreviation of Message Digest 5. It is an encryption algorithm that can encrypt byte arrays. It has the following characteristics:

(1) The information before encryption cannot be found according to the encrypted information;
The encrypted result is 128 bits;
(3) For a given byte array, no matter when this encryption algorithm is adopted, the result is the same;
4 For different byte arrays, the encryption results are different.

In Web application, it is usually necessary to encrypt the password set by the user before storing it, otherwise the database administrator can see the plaintext password, and the password is also in danger of being obtained by hackers.

You can use MD5 to encrypt the user's password. But there are two situations that you may need to deal with in other ways:

① It is necessary to ensure that the password is safe during transmission. At this time, https is usually used, which is the case for almost all bank websites, so the cost is relatively high.
② If the website provides the function of retrieving the password. Because there is no way to obtain the original password after MD5 encryption.
The application of MD5 includes the following processes:
Converting the information to be encrypted into a byte array;
Obtaining an MessageDigest object, which is encrypted;
MessgeDigest object is initialized by using the converted byte array;
6. Calling the digest method for encryption and returning the byte array;
⑦ Convert the byte array into a string, and then you can use the encrypted string.

Suppose the original string is oldStr, the content is "lixucheng", and the encrypted string is newStr. The specific process is described as follows.

1. Convert a string to a byte array

You can use the getBytes method of the string for conversion, for example:

byte[] oldBytes = oldStr.getBytes();

Data in Array: 108 105 120 117 99 104 101 110 103

2. Get the MessgaeDigest object

The MessgeDigest object is obtained using the getInstance (String str) method of MessageDigest, and the parameters are MD5. For example:

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

3. Initialize the MessgeDigest object with the converted byte array

Initialized with the update method, and the parameters are converted byte arrays. For example:

md.update(oldBytes);

4. Call digest method for encryption

Method returns an array of bytes. For example:

byte[] newBytes = md.digest();

Data in the array (16 bits):-22 1 35 121-120 65 114 75 127-34 31-21 51-37-97-118
5. A string converted to a hexadecimal representation

The following code completes the conversion:


//  The length of the structure is 2 A string of times 
char newStr[] = new char[32];
//  Cyclic processing 
for (int i = 0; i < 16; i++) {
 byte tmp = newBytes[i];
 newStr[2*i] = hexDigits[tmp >>> 4 & 0xf];
 newStr[2*i+1] = hexDigits[tmp & 0xf];
}

Conversion String (32 bits): ea0123798841724b7fde1feb33db9f8a
Tip: If you need to save the converted password to the database, the type you need to use is char (32).

The complete reference code is as follows:


package test;
import java.security.*;
class MD5_Test {
 public final static String MD5(String oldStr) {
  char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    'a', 'b', 'c', 'd', 'e', 'f' };
  System.out.println(" The original string is: "+oldStr);
  try {
   //  Parameter oldStr Represents the string to encrypt 
   //  Convert to byte stream 
   byte[] oldBytes = oldStr.getBytes();
  for(byte b:oldBytes)
  {
   System.out.print(b+" ");
  }
  System.out.println();
   //  Get the object 
   MessageDigest md = MessageDigest.getInstance("MD5");
   //  Initialization 
   md.update(oldBytes);
   //  Run the encryption algorithm 
   byte[] newBytes = md.digest();
  for(byte b:newBytes)
  {
   System.out.print(b+" ");
  }
  System.out.println();
   //  The length of the structure is 2 A string of times 
   char newStr[] = new char[32];
   //  Cyclic processing 
   for (int i = 0; i < 16; i++) {
    byte tmp = newBytes[i];
    newStr[2*i] = hexDigits[tmp >>> 4 & 0xf];
    newStr[2*i+1] = hexDigits[tmp & 0xf];
   }
   System.out.println(newStr);
   return new String(newStr);
  } catch (Exception e) {
   return null;
  }
 }
 public static void main(String[] args) {
  System.out.println(MD5_Test.MD5("lixucheng"));
 }
}

I hope this article is helpful to everyone's JSP programming.


Related articles: