Home>

md5 is the abbreviation of message digest 5, which is an encryption algorithm.Ability to encrypt byte arrays,Has the following characteristics:

① Cannot find the information before encryption based on the encrypted information;

② The encrypted result is 128 bits;

③ For a given byte array,Whenever this encryption algorithm is used,The result is the same;

④ For different byte arrays,The results of encryption are different.

In web applications, the password set by the user is usually encrypted and then stored.Otherwise the database administrator can see the clear text password,And the password is in danger of being obtained by hackers.

You can use md5 to encrypt the user's password.But there are two situations,You may need to use other methods:

① It is necessary to ensure that the password is also secure during transmission.At this time, https is usually used, and almost all bank websites are like this.The cost is relatively high.

② If the website provides the function to retrieve the password.Because there is no way to get the original password after using md5 encryption.

The application of md5 includes the following processes:

③ convert the information to be encrypted into a byte array;

④ Get the messagedigest object, which is encrypted;

⑤ initialize the messagedigest object with the converted byte array;

⑥ call the digest method to encrypt,Return byte array;

Byte Convert the byte array into a string,Then you can use the encrypted string.

Assume that the original string is oldstr, the content is "lixucheng", and the encrypted string is newstr. The specific processes are described below.

1.Convert string to byte array

Can be converted using the string's getbytes method,E.g:

byte [] oldbytes=oldstr.getbytes ();

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

Get the messagegaedigest object

Use messagedigest's getinstance (string str) method to get the messageigest object, using md5 as the parameter. E.g:

messagedigest md=messagedigest.getinstance ("md5");

3, initialize the messagedigest object with the converted byte array

Use the update method to initialize,The argument is a converted byte array.E.g:

md.update (oldbytes);

4, call the digest method for encryption

The method returns a byte array.E.g:

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.Convert to hexadecimal string

The following code completes the conversion:

//construct a string with a length of 2
char newstr []=new char [32];
//loop for 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];
}

The converted string (32-bit):ea0123798841724b7fde1feb33db9f8a

Tip:If you need to save the converted password to the database,The required type 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 ("Original string was:" + oldstr);
  try {
   //parameter oldstr represents the string to be encrypted
   //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 ();
   //construct a string with a length of 2
   char newstr []=new char [32];
   //loop for 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"));
 }
}
jsp
  • Previous Talking about touch events in javascript
  • Next Workaround for NET Runtime Optimization Service 1101 error