MD5 Encryption usage Analysis _JSP programming for Java Web development

Source: Internet
Author: User
Tags md5 md5 encryption java web

This example describes the MD5 encryption usage of Java Web development. Share to everyone for your reference. Specifically as follows:

MD5 is the acronym for Message Digest 5, an encryption algorithm that encrypts byte arrays with the following characteristics:

① cannot find the information before encryption based on the encrypted information;
The result of ② encryption is 128 bits;
③ for a given byte array, the result is the same whenever the encryption algorithm is used;
④ for different byte arrays, the result of the encryption is not the same.

In a Web application, the password that is set by the user is usually encrypted and then stored, or the database administrator can see the password in plaintext, and the password is also in danger of being hacked.

You can use MD5 to encrypt a user's password. But there are two things that you might want to do with other methods:

① need to ensure that passwords are also secure during transmission, which is usually done using HTTPS, almost all bank sites are, and relatively expensive.
② If the site provides the ability to retrieve passwords. Because the original password is not available after using MD5 encryption.
The application of MD5 includes the following processes:
③ converts the information to be encrypted into a byte array;
④ gets the MessageDigest object, which completes the encryption;
⑤ initializes the Messgedigest object using the converted byte array;
⑥ calls the digest method for encryption, returning a byte array;
⑦ converts the byte array to a string, and then it 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 below separately.

1. Converting a string into a byte array

You can use the GetBytes method of a string to convert, for example:

Copy Code code as follows:
byte[] oldbytes = Oldstr.getbytes ();

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

2. Get Messgaedigest Object

The Messgedigest object is obtained using the MessageDigest getinstance (String str) method, and the parameter uses MD5. For example:

Copy Code code as follows:
MessageDigest MD = messagedigest.getinstance ("MD5");

3. Initialize the Messgedigest object with the converted byte array

Initializes using the Update method, which is the converted byte array. For example:

Copy Code code as follows:
Md.update (oldbytes);

4, call the digest method for encryption

method returns a byte array. For example:

Copy Code code as follows:
byte[] newbytes = Md.digest ();

The data in the array (16 bits):-22 1 35 121-120 65 114 75 127-34 31-21 51-37-97-118
5, converted into a 16-in-system representation of the string

The following code completes the transformation:

Twice-fold string
char newstr[] = new CHAR[32];
The loop is processed for
(int i = 0; i < i++) {
 byte tmp = newbytes[i];
 Newstr[2*i] = hexdigits[tmp >>> 4 & 0xf];
 Newstr[2*i+1] = hexdigits[tmp & 0xf];
}

Converted string (32-bit): ea0123798841724b7fde1feb33db9f8a
Tip: If you need to save the converted password to the database, you need to use the following type: 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 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 Object MessageDigest MD = messagedigest.getinstance ("MD5");
   Initialization of Md.update (oldbytes);
  Run encryption algorithm byte[] newbytes = Md.digest ();
  for (byte b:newbytes) {System.out.print (b + "");
   } System.out.println ();
   Twice-fold string char newstr[] = new CHAR[32];
    The loop is processed for (int i = 0; i < 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 will help you with your JSP programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.