Java Web practice topic-MD5 Encryption

Source: Internet
Author: User
MD5 is short for message digest 5. It is an encryption algorithm that can encrypt byte arrays. It has the following features:

L cannot find the encrypted information based on the encrypted information; l the encrypted result is 128 bits; l no matter when this encryption algorithm is used for the given byte array, the results are the same. l the encryption results for different byte arrays 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, there are two situations where you may need to use other methods for processing: l you need 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. L if the website provides the function of retrieving the password. Because the original password cannot be obtained after MD5 encryption. The Application of MD5 includes the following processes: l converts the information to be encrypted into a byte array; l obtains the messagedigest object, which is encrypted; l initializes the messgedigest object using the converted byte array; l call the Digest Method to encrypt and return the byte array; l converts the byte array into a string, and then the encrypted string can be used. 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. You can use the getbytes method to convert a string to a byte array. For example, byte [] oldbytes = oldstr. getbytes (); Data in the array: 108 105 120 117 104 99 101 110 1032. Get the messgedigest object and use the getinstance (string Str) method of messagedigest to get the messgedigest object. The parameter uses MD5. For example, messagedigest MD = messagedigest. getinstance ("MD5"); 3. Use the converted byte array to initialize the messgedigest object using the update method. The parameter is the converted byte array. For example, MD. Update (oldbytes); 4. Call the Digest Method to return a byte array. 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-1185, the code below converts to a hexadecimal string to complete 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): ea0123168821324b7fde1feb33db9f8a prompt: 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:" + 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 "));}}

 

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.