Java Web development-MD5 encryption Usage Analysis, javamd5

Source: Internet
Author: User

Java Web development-MD5 encryption Usage Analysis, javamd5

This example describes how to use MD5 encryption in Java Web development. Share it with you for your reference. The details are as follows:

MD5 is short for Message Digest 5. It is an encryption algorithm that can encrypt byte arrays. It has the following features:

① You cannot find the encrypted information based on the encrypted information;
② The encrypted result is 128 bits;
③ For a given byte array, no matter when this encryption algorithm is used, the results are the same;
④ For different byte arrays, the encryption results are different.

In Web applications, the password set by the user must be encrypted before being stored. Otherwise, the database administrator can see the plaintext password and the password may also be obtained by hackers.

You can use MD5 to encrypt your password. However, in either case, you may need to use another method:

① It is necessary to ensure that the password is secure during transmission. https is usually used at this time. Almost all banking websites are like this and the cost is relatively high.
② If the website provides the password retrieval function. Because the original password cannot be obtained after MD5 encryption.
MD5 applications include the following processes:
③ Convert the information to be encrypted into a byte array;
④ Get the MessageDigest object, which is encrypted;
⑤ Use the converted byte array to initialize the MessgeDigest object;
6. Call the digest Method for encryption and return the byte array;
7. Convert the byte array into a string and then use the encrypted string.

Assume that 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:
Copy codeThe Code is as follows: byte [] oldBytes = oldStr. getBytes ();
Data in the array: 108 105 120 117 99 104 101 110 103

2. Get the MessgaeDigest object

Use the getInstance (String str) method of MessageDigest to obtain the MessgeDigest object. The parameter uses MD5. For example:
Copy codeThe Code is as follows: MessageDigest md = MessageDigest. getInstance ("MD5 ");
3. Use the converted byte array to initialize the MessgeDigest object.

Use the update method for initialization. The parameter is the converted byte array. For example:
Copy codeThe Code is as follows: md. update (oldBytes );
4. Call the digest Method for encryption

The method returns a byte array. For example:
Copy codeThe Code is as follows: 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 a hexadecimal string

The following code completes the conversion:

// Construct a string of 2 times the length of char newStr [] = new char [32]; // it is processed cyclically 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];}

Converted string (32-bit): ea0123168810924b7fde1feb33db9f8a
Tip: If you need to save the converted password to the database, you need to use the char (32) type ).

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:" + oldStr); try {// The oldStr parameter indicates the string to be encrypted. // It is converted to the byte [] oldBytes = oldStr. getBytes (); for (byte B: oldBytes) {System. out. print (B + "");} System. out. println (); // obtain the object MessageDigest md = MessageDigest. getInstance ("MD5"); // initialize 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 of 2 times the length of char newStr [] = new char [32]; // process the 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 will help you with JSP program design.

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.