Using MD5 and BASE64 in Java

Source: Internet
Author: User
Tags base64



MD5:



In the previous session of the Servlet Learning sessions (5), in order to enable the same number of digits for all random numbers obtained, we used MD5 to get a message digest of random numbers (or data fingerprints, data digests, etc.).



MD5 can combine all the numbers into a new combination of numbers, and this new combination of numbers is 128 bits, also known as the MD5 code. MD5 code is irreversible, that is, the MD5 code can not be reversed to get the original data. MD5 applications are very many, such as saving the user password (to prevent the user password in the database is seen by the administrator, so the password in the database is not allowed to use the plaintext password), or file verification (to prevent the download of the file is modified by other people's files rather than the official original file, such as the XcodeGhost event of the Apple development tool), or the disc breakage check, etc.



In Java, the method is called by the MessageDigest object to obtain the MD5 code for some combination of numbers (by the way, messagedigest can also be used to obtain the SHA checksum code). Call the digest method with the MessageDigest object to convert the number combination into a character array:


 
 
1     String data = "1";
2         
3     MessageDigest md = MessageDigest.getInstance("md5");
4     byte[] md5 = md.digest(data.getBytes());
5         
6   System.out.println(Arrays.toString(md5));


The array of bytes obtained is 128 bits , or 16 bytes. In other words, there are 16 elements in the character array after MD5, because the range of byte in Java is -128~127, so each element is within this range:






Note that the character array after MD5 contains negative elements, so if you convert this character array to a string, the query GB2312 or UTF-8 are inappropriate, such as:


 
1     String sData = new String(md5,"utf-8");
2     System.out.println(sData);





So how do we convert this byte array into a random number with no garbled strings? We can use the "Base64" code.



Base64 Code :



The BASE64 encoding is to convert every 3 8 bytes (3*8=24) to 4 6-bit bytes (4*6=24) and 6 two in front of each 0 byte, re-form a 8-bit byte, so that the total length is the same, and that the highest bit is not 1 (that is, not a negative number).



If the last remaining character is less than 3 characters, the upper is filled with 0, and the output character uses "=". Therefore, one or two "=" may appear at the end of the encoded string.



According to the above principle, the highest two bits are 0, then the remaining 6 are all 12 decimal is 63, so the maximum number will not exceed 63, according to these 64 numbers for a code value corresponding to the character, it becomes the Base64 encoding, the corresponding encoding table as shown:






In Java we use the Base64encoder object to call the Encode method to encode a byte array.



We encode the MD5 code above base64:


 
1     BASE64Encoder be = new BASE64Encoder();
2     String base64 = be.encode(md5);
3     System.out.println(base64);


You can see that this will be garbled characters are corresponding to the character:






Note that Base64encoder cannot view its API documentation because Sun does not formally publish it, and there is no corresponding Javadoc document to review it. You can download the jar package and import it online only by downloading the appropriate jar package or by checking it online, if found unable to find Sun.misc.BASE64Encode in the development IDE.



Using MD5 and BASE64 in Java


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.