Chapter 3 message digest algorithm-MD5, digest-md5

Source: Internet
Author: User

Chapter 3 message digest algorithm-MD5, digest-md5

Note: In this section, refer to "verify data integrity-message digest algorithm" in Chapter 2nd of Java encryption and decryption art (version 6th"

3.1 message digest algorithm: prevents tampering with messages during transmission.

Principle: After a message passes through the message digest algorithm, a unique hash value (that is, "Data fingerprint") is generated. (no matter how many times a message digest algorithm is encrypted, the results are the same). Therefore, if the message is modified during transmission, the calculated data fingerprint is different from the original message. If the data fingerprint is not modified, the data fingerprint is the same.

Features: one-to-one (only encrypted, not decrypted)

3.2. MD5 (the algorithm has been cracked and applied to scenarios with low security requirements)

Implementation Method:

  • Commons Codec("CC ",Most recommendedBecause it encapsulates the underlying code of JDK and provides a way to convert binary byte arrays to hexadecimal notation)
  • JDK (there is no way to convert a binary byte array to a hexadecimal format. to convert a binary byte array, you need to use BC)
  • Bouncy Castle ("BC", not recommended, complicated implementation)

Only CC-based tool code is listed below. For JDK-based code, refer to "Java encryption and decryption art (version 2nd)". for BC-based code, refer to "MOOC"

3.2.1 CC-based MD5 Encryption Algorithm

1 package com. util. md5; 2 3 import java. io. unsupportedEncodingException; 4 import java. security. noSuchAlgorithmException; 5 import org. apache. commons. codec. digest. digestUtils; 6/** 7 * md5 Algorithm Based on Commons Codec 8 */9 public class Md5CC {10 private static final String ENCODING = "UTF-8 "; 11 12/** 13 * MD5 encryption, the encrypted result is a binary byte array 14 */15 public static byte [] encode (String data) throws NoSuchAlgorithmException, U NsupportedEncodingException {16 return DigestUtils. md5 (data. getBytes (ENCODING); 17} 18 19/** 20 * MD5 encryption. The encrypted result is a binary byte array, here, the binary byte array is converted to a 32-bit hexadecimal 21 */22 public static String encodeMd5Hex (String data) throws NoSuchAlgorithmException, UnsupportedEncodingException {23 return new String (DigestUtils. md5Hex (data. getBytes (ENCODING); // use new String (encodedByte, "UTF-8") here not 24} 25 26/** 27 * Test 28 * @ param ar Gs29 * @ throws UnsupportedEncodingException 30 * @ throws NoSuchAlgorithmException 31 */32 public static void main (String [] args) throws UnsupportedEncodingException, noSuchAlgorithmException {33 34 String data = "It is my dream to find a good girl as a wife! "; 35/************** test encode () ****************/36 System. out. println ("original -->" + data); 37 byte [] encodedByte = Md5CC. encode (data); 38 System. out. println ("encrypted -->" + encodedByte); 39 byte [] encodedByte2 = Md5CC. encode (data); 40 System. out. println ("encrypted -->" + encodedByte2); 41 for (int I = 0; I <encodedByte. length; I ++) {42 System. out. println (encodedByte [I] = encodedByte2 [I]); 43} 44/************* test encodeMd5Hex () * ************/45 System. out. println ("original -->" + data); 46 String encodedStr = Md5CC. encodeMd5Hex (data); 47 System. out. println ("encrypted -->" + encodedStr); 48 String encodedStr2 = Md5CC. encodeMd5Hex (data); 49 System. out. println ("encrypted -->" + encodedStr2); 50 System. out. println (encodedStr. equals (encodedStr2); 51} 52}View Code

The introduction of the jar package can be carried out by referring to the first example in chapter 2. During the test, try to see if "the same message has the same result after multiple MD5 encryption ";

In the test of the encode () method, determine whether two byte [] are equal: Compare the elements in the two byte arrays in order by index (if the main method is used for testing ); if Junit is used, use assertArrayEquals (array1, array2) directly.

 

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.