BASE64 Algorithm and Application, and BASE64 Algorithm Application

Source: Internet
Author: User
Tags 0xc0

BASE64 Algorithm and Application, and BASE64 Algorithm Application

Base64 is one of the most common encoding methods used to transmit 8-bit code on the network. Base64 has the following three advantages: First, information hiding and encryption, which avoids the security problem caused by plaintext transmission, and second, transfers binary bytes to visible characters for transmission, this makes it suitable for URL transmission. Third, it avoids the trouble of adjusting the size end and splicing data between different platforms and different processors, and has certain cross-platform and cross-programming language capabilities. For some weak languages or scripting languages that cannot process binary byte streams, an application window is also provided. Of course, his weakness is also quite obvious, making the original data more than 1/3.


The principle of Base64 is to convert the Three-byte 24bit into four-byte 32bit. The three bytes are 24 BITs, which are regarded as a whole. Each time a six bits are retrieved and saved to the lower six bits of the new four bytes, exactly four bytes are saved four times, in this way, if the original data is 0xff and three bytes, it will become four bytes of 0x3f. If the original data is positive, it is 0 ~ 255, base64 encoding, after the second-year high is filled with zero, it will become four 0 ~ As long as 64 visible characters are selected for replacement, the base64 encoding is formed. If you use a common base64 character replacement, online base64 encoding can help you decode it. If you want to be safer, You need to disrupt those characters or set the seed characters you choose. Common Character seeds:

Static final byte [] cb64 = {(byte) 'A', (byte) 'B ',
(Byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9 ',
(Byte) 'h', (byte) 'I', (byte) 'J', (byte) 'k', (byte) 'l ',
(Byte) 'V', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'Z ',
(Byte) 'R', (byte)'s ', (byte) 'T', (byte) 'U', (byte) 'V ',
(Byte) 'w', (byte) 'x', (byte) 'y', (byte) 'Z', (byte) 'A ',
(Byte) 'Q', (byte) 'R', (byte)'s ', (byte) 'T', (byte) 'U ',
(Byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G ',
(Byte) 'G', (byte) 'h', (byte) 'I', (byte) 'J', (byte) 'k ',
(Byte) 'M', (byte) 'n', (byte) 'O', (byte) 'P', (byte) 'Q ',
(Byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F ',
(Byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4 ',
(Byte) 'l', (byte) 'M', (byte) 'n', (byte) 'O', (byte) 'P ',
(Byte) '+', (byte) '/', (byte) '= '};


A simple java language base64 encoding:

public class XiaoMiBase64 {/* ----------------base64 from:http://base64.sourceforge.net/ ----------------- *//*** Translation Table as described in RFC1113*//*static final byte[] cb64={ (byte) 'A', (byte) 'B',          (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9',          (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',          (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',          (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V',          (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a',          (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u',          (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G',          (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k',          (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q',          (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',          (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4',          (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p',          (byte) '+', (byte) '/' , (byte) '='};        */static final String scb64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";static final byte[] cb64 = scb64.getBytes();/*** Translation Table to decode (created by author)*///static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";static byte base64_map_rev(byte c){   byte j;for(j = 0; j < 65; j++){if(c == cb64[j]) { return j;} }return 0;}/*** decodeblock**** decode 4 '6-bit' characters into 3 8-bit binary bytes*/static byte[] decodeblock(byte[] in){   byte[] out = new byte[3];out[0] = (byte) (in[0] << 2 | in[1] >> 4);out[1] = (byte) (in[1] << 4 | in[2] >> 2);out[2] = (byte) (((in[2] << 6) & 0xc0) | in[3]);return out;}static byte[] Base64Decode(byte[] b64_inbuf, long b64_len) { int size = (int)((b64_len*3)/4);byte[] b64_outbuf = new byte[size];byte[] in= new byte[4];byte[] out;// = new byte[3];    for (int i = 0; i < b64_len; i++)    {    b64_inbuf[i] = base64_map_rev(b64_inbuf[i]);    if (b64_inbuf[i] == 64) {size--;}if (b64_inbuf[i] == 64) {size--;}    }    for (int i = 0; i < (b64_len/4); i++){System.arraycopy(b64_inbuf, i*4, in, 0, in.length);out=decodeblock(in);System.arraycopy(out, 0, b64_outbuf, i*3, out.length);}byte[] b64_outbuf2 = new byte[size];System.arraycopy(b64_outbuf, 0, b64_outbuf2, 0, size);return b64_outbuf2;} static byte[] encodeblock(byte[] in, int len ){System.out.println("AAAAA  len="+len);byte[] out = new byte[4];System.out.println("AAAAA  in[0]="+in[0]);System.out.println("AAAAA  (in[0] >> 2)="+(in[0] >> 2));out[0] = (byte) cb64[((in[0] >> 2))&0x3f];out[1] = (byte) cb64[((((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)))&0x3f];out[2] = (byte) (len > 1 ? cb64[((((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)))&0x3f] : '=');out[3] = (byte) (len > 2 ? cb64[((in[2] & 0x3f))&0x3f] : '=');    return out;}static byte[] Base64Encode(byte[] inbuf, long len){int size = (int)((len*4)/3) + ((((len*4)%3)==0)?0:4);byte[] b64_outbuf = new byte[size];byte[] in = new byte[3];byte[] out;// = new byte[4];int i;for (i = 0; i < (len/3); i ++){System.arraycopy(inbuf, i*3, in, 0, in.length);out=encodeblock(in, in.length);System.arraycopy(out, 0, b64_outbuf, i*4, out.length);}if ((len%3) != 0){System.arraycopy(inbuf, i*3, in, 0, (int)(len-i*3));out=encodeblock(in, (int)(len-i*3));System.arraycopy(out, 0, b64_outbuf, i*4, out.length);}return b64_outbuf;}public static void main(String[] args) {String msg = "Z6V3ARgCAABPAgAAeQYAAKUIAABfEQAAKiMAAOiZAABMmgAA1H" +     "UBADh2AQCQygQAJCcFAORGBwAAAAAAAAAAAAAAAABFAQEBAQEB" +     "IQEhISEhISEBISEBAQEBAQEBASEhISEhISFhNYE1kTWRNZE1kT" +     "WRNZE1kTWxRQEBAQEBASEBISEhISEhASEhAQEBAQEBAQEhISEh" +     "ISEhcTWRNZE1kTWhRQEBAQEBASEBISEhISEhASEhAQEBAQEBAQ" +     "EhISEhISEhYTWBNYE1kTWBNcFFAQEBAQEBIQEhISEhISEBISEB" +     "AQEBAQEBASEhISEhISFxNZE1kTWRNZE1kTWBNYE1AQ==";//String data = "1";//byte[] result = XiaoMiBase64.encode(data.getBytes());//byte[] out = new byte[msg.length()*3/4];byte[] out = XiaoMiBase64.Base64Decode(msg.getBytes(), msg.length());System.out.println("AAAAA="+out.toString());for (int i = 0; i < out.length; i++){System.out.println("AAAAA out["+i+"]="+out[i]);}String str = "abcdefghijklmnopqrstuvwxyz";byte[] base64str = Base64Encode(str.getBytes(), str.length());System.out.println("AAAAA base64str ="+ new String(base64str));byte[] str1 = Base64Decode(base64str, base64str.length);System.out.println("AAAAA str1 ="+new String(str1));}}




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.