BASE64 algorithm and its application

Source: Internet
Author: User
Tags 0xc0 base64

Base64 is one of the most common encoding methods for transmitting 8Bit bytes of code on the network. The use of Base64 has the following three advantages, one is the information hiding encryption, to avoid the security problems caused by the plaintext transmission, the second is to transfer the binary byte to the visible character transmission, making it very suitable for transmission in the URL, and the third is to avoid the different platforms and different processors between the size of the end and splicing data trouble, With a certain cross-platform and cross-programming language capabilities. For some weak languages or scripting languages that cannot handle binary byte streams, an application window is also available. Of course, his weaknesses are quite obvious, making the original data One-third larger than the future.


Base64 's algorithm principle is to convert three bytes 24bit to 4 byte 32bit. Three bytes of 24bit, as a whole, each time a 6 bit is taken out into the new four bytes of the low six bit, four times just save four bytes, so the original data if the positive number is 0xFF and three bytes, it will become 0x3f four bytes. If the original data represents a positive number, that is, after the 0~255,BASE64 encoding, the high two-bit fill is zero, it becomes four 0~63 integer, so as long as 64 visible characters are selected for the corresponding substitution, the base64 encoding is formed. If you use a generic base64 character replacement, online base64 encoding on the Web can help you decode it. If you want to be safe, you'll have to disrupt those characters yourself, or make your own choice of seed characters. 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 code:

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 ', (bytes) ' V ', (byte) ' W ', (byte) ' X ', (byte) ' Y ', (byte) ' Z ', (Byte) ' A ', (byte) ' Q ', (byte) ' R ', (Byte) ' s ', (b yte) ' 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{$ $$$$$$>[email protected]$$$$$ $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] >&gt ; 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;//= newBYTE[3];    for (int i = 0; i < B64_len; i++) {B64_inbuf[i] = Base64_map_rev (B64_inbuf[i]); if (b64_inbuf[i] = =) {size--;}    if (b64_inbuf[i] = =) {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];o UT[1] = (byte) cb64[((((In[0] & 0x03) << 4) | ((In[1] & 0xf0) >> 4))) &AMP;0X3F];OUT[2] = (byte) (Len > 1? cb64[((((in[1) & 0x0f) << 2) | ((In[2] & 0xc0) >> 6))) &AMP;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.Array Copy (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" + "UBADH2AQC Qygqajccfaorgbwaaaaaaaaaaaaaaaabfaqebaqeb "+" iqehisehisebisebaqebaqebasehisehisfhnye1ktwrnze1kt "+" WRNZE1kTWxRQE Baqebasebisehisehasehaqebaqebaqehiseh "+" Isehctwrnze1ktwhrqebaqebasebisehisehasehaqebaqebaq "+" EhISEhISEhYTWBNYE 1kTWBNcFFAQEBAQEBIQEhISEhISEBISEB "+" 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));}}




BASE64 algorithm and its application

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.