BASE64 encoding and decoding

Source: Internet
Author: User

reliance on Jar:import org.apache.commons.codec.binary.Base64;

BASE64 and other similar coding algorithms are commonly used to convert binary data into textual data, which is intended to simplify storage or transmission. More specifically, the BASE64 algorithm is primarily used to convert binary data into ASCII string formats. The Java language provides a very good implementation of the BASE64 algorithm, the Apache Commons codec toolkit. This article will briefly describe how to use BASE64 and how it works.

Here we encode the string using BASE64:

Import java.io.UnsupportedEncodingException; Import org.apache.commons.codec.binary.Base64;    public class Base64test {public     static void Main (string[] args) {         String str = ' Hello world ';         try{             System.out.println ("RESULT:" + encodestr (str));         } catch (Unsupportedencodingexception e) {             e.printstacktrace ();}}     }

    /** *      Decryption      *       * @param pwd      * @return      * @see [Class, Class # method, Class # member]      *      /public static String Decodestr ( String pwd)      {          Base64 base64 = new Base64 ();          byte[] Debytes = base64.decodebase64 (new String (PWD). GetBytes ());          return new String (debytes);      }      /** *      Encryption      *       * @param pwd      * @return      * @see [Class, Class # method, Class # member]      */public      static String Encodestr (String pwd)      {          Base64 base64 = new Base64 ();          byte[] enbytes = base64.encodebase64chunked (Pwd.getbytes ());          return new String (enbytes);  

The output is:

    1. RESULT: sgvsbg8gv29ybgq=

The above output string is a 8-bit binary value of the "Hello World" string that is concatenated and then grouped in 6 bits. Each group is then converted to a separate number and mapped to the index of the Base64.

Binary  Dec Base64 010010  s 000110  6   G 010101  V 101100  27 s 011011  b 000110  6   g 111100  8 100000  g 010101  d 110110  2 111101  9 110010  y 011011  b 000110  6   G 010000  Q

Note: The string is finally prefixed with "=", which means the end of the string encoding.

BASE64 encoding and decoding

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.