MD5 Encryption and BASE64 encoding

Source: Internet
Author: User

MD5 is message-digest algorithm 5 (Information-Digest algorithm 5): Method One: Add Jar package: ${spring_home}/lib/jakarta-commons/commons-codec.jarimport Org.apache.commons.codec.digest.digestutils;public class Md5util {//returns MD5 encrypted string public static strings MD5 (string Message) {String md5digest = digestutils.md5hex (message); return md5digest;}} Method Two://JDK from the import java.security.messagedigest;//is also the JDK comes with, you can view its source code import sun.misc.BASE64Encoder in OpenJDK; public class Md5util {//returns MD5 after the encrypted string public-static string MD5 (String message) {try {messagedigest MD = Messagedigest.getin Stance ("MD5"); byte[] md5bytes = Md.digest ();//Use BASE64 encoding to convert a sequence of bytes into plaintext base64encoder Base64encoder = new Base64encoder (); Return Base64encoder.encode (md5bytes);} catch (Exception e) {throw new RuntimeException (e);}}} Add: Base64 Encoding principle: Convert 3 bytes to 4 bytes ((3X8) =24= (4X6)) read in 3 bytes, each read a byte, left 8 bits, and then right four times, 6 bits each time, so there is 4 bytes. Decoding principle: Convert 4 bytes to 3 bytes read into 4 6 bits (with or operations), each shift left 6 bits, and then right 3 times, 8 bits each time, so it is restored. Description: 1,BASE64 encoding can be used to pass long identification information in an HTTP environment 2,BASE64 encoded identity information length of 243, 3 bytes to 4 bytes, each byte represents the minimum and maximum number is: 00000000 ~ 001111110~ 63 A total of 64 integers,To call it Base64, it is actually a code table, each number corresponds to a visible character 


MD5 Encryption and BASE64 encoding

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.