JAVA Implementation of base64 encoding/Decoding

Source: Internet
Author: User
Tags 0xc0 base64 encode

/** <Br/> * base64util for Java <br/> * cheungmine <br/> * 2009-11-8 <br/> */<br/> public class base64util {<br/ >/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) <br/>{< br/> // Source byte array <br/> int cb = 0; <br/> system. out. print ("Source byte array:"); </P> <p> byte in [] = new byte [100]; <br/> in [CB ++] = '1'; <br/> in [CB ++] = '2 '; <br/> in [CB ++] = '3'; <br/> in [CB ++] = '4 '; <br/> in [CB ++] = '5'; </P> <p> syst Em. out. write (in, 0, CB); </P> <p> // size of the output bytes required for encoding <br/> int cbout = encodestring (in, CB, null, 0); </P> <p> // allocate an array to the output <br/> byte out [] = new byte [cbout]; </P> <p> // start encoding <br/> encodestring (in, CB, out, 0 ); </P> <p> // output the encoding content <br/> system. out. print ("/n encoded content:"); <br/> system. out. write (Out, 0, cbout); </P> <p> // calculate the required bytes for decoding <br/> int cbdec = decodestring (Out, cbout, null ); </P> <p> // assign a decoding array <br/> byte dec [] = new byte [Cbdec]; </P> <p> // start decoding <br/> decodestring (Out, cbout, DEC); </P> <p> // output the decoding result, this result should be consistent with the output <br/> system. out. print ("/n decoding Result:"); <br/> system. out. write (Dec, 0, cbdec ); <br/>}</P> <p>/** <br/> * translation table as described in rfc1113 <br/> */<br/> static string cb64 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /"; <br/>/** <br/> * translation table to decode (created by author) <br/> */<Br/> static string cd64 = "| $} rstuvwxyz {$ $>? @ Abcdefghijklmnopqrstuvw $ XYZ [//] ^ _ 'abcdefghijklmnopq "; <br/>/** <br/> * encodeblock <br/> * encode 3 8-bit binary bytes (in) as 4 '6-bit 'characters (out) <br/> */<br/> static void encodeblock (byte in [], byte out [], int Len) <br/>{< br/> out [0] = (byte) cb64.charat (in [0]> 2); <br/> out [1] = (byte) cb64.charat (in [0] & 0x03) <4) | (in [1] & 0xf0)> 4 )); <br/> out [2] = (byte) (LEN> 1? Cb64.charat (in [1] & 0x0f) <2) | (in [2] & 0xc0)> 6): '= '); <br/> out [3] = (byte) (LEN> 2? Cb64.charat (in [2] & 0x3f): '= '); <br/>}</P> <p>/** <br/> * decodeblock <br/> * decode 4 '6-bit 'characters 3 8-bit binary bytes <br/> */<br/> static void decodeblock (byte in [], byte out []) <br/>{< br/> out [0] = (byte) (in [0] <2 | in [1]> 4 ); <br/> out [1] = (byte) (in [1] <4 | in [2]> 2 ); <br/> out [2] = (byte) (in [2] <6) & 0xc0) | in [3]); <br/>}</P> <p>/** <br/> * ENC Odestring <br/> * base64 encode bytes <br/> * by cheungmine <br/> * outstr is allocated by caller <br/> * length of encoded string is returned (not including end closing '/0 ') <br/> */<br/> Public static int encodestring (byte instr [], int inlen, byte outstr [], int linesize) <br/>{< br/> byte in [] = new byte [3]; <br/> byte out [] = new byte [4]; </P> <p> int Inc = 0, outc = 0, I, Len, blocksout = 0; </P> <p>/* only size of buffer required */<br/> If (outstr = NULL) {<br/> If (linesize = 0) <br/> return (inlen % 3 = 0 )? (Inlen/3) * 4) :( (inlen/3 + 1) * 4); <br/> while (INC <inlen) {<br/> Len = 0; <br/> for (I = 0; I <3; I ++) {<br/> If (INC <inlen) {<br/> Len ++; <br/> Inc ++; <br/>}< br/> If (LEN> 0) {<br/> outc ++ = 4; <br/> blocksout ++; <br/>}< br/> If (blocksout >=( linesize/4) | Inc = inlen) {<br/> If (blocksout> 0) {<br/> outc ++ = 2; <br/>}< br/> blocksout = 0; <br/>}< br/> return outc; <br/>}< br/>/* Actual E Ncoding below */<br/> while (INC <inlen) {<br/> Len = 0; <br/> for (I = 0; I <3; I ++) {<br/> If (INC <inlen) {<br/> Len ++; <br/> in [I] = instr [INC ++]; <br/>}< br/> else {<br/> in [I] = 0; // padding with zero <br/>}< br/> If (LEN> 0) {<br/> encodeblock (In, out, Len ); <br/> for (I = 0; I <4; I ++) {<br/> outstr [outc ++] = out [I]; <br/>}< br/> blocksout ++; <br/>}< br/> If (linesize> 0) {<br /> If (blocksout >=( linesize/4) | Inc = inlen) {<br/> If (blocksout> 0) {<br/> outstr [outc ++] = '/R'; <br/> outstr [outc ++] ='/N '; <br/>}< br/> blocksout = 0; <br/>}< br/> return outc; <br/>}</P> <p>/** <br/> * Add By cheungmine <br/> * 2009-11-1 <br/> */<br/> Public static int decodestring (byte instr [], int inlen, byte outstr []) <br/>{< br/> byte in [] = new byte [4]; <br/> byte out [] = N EW byte [3]; <br/> byte V; <br/> int Inc = 0, outc = 0, I, Len; <br/>/* only size of buffer required */<br/> If (outstr = NULL) {<br/> while (INC <inlen) {<br/> for (LEN = 0, I = 0; I <4 & Inc <inlen; I ++) {<br/> V = 0; <br/> while (INC <= inlen) & (V = 0) {<br/> V = (INC <inlen )? Instr [INC]: 0; <br/> Inc ++; <br/> V = (byte) (v <43 | V> 122 )? 0: cd64.charat (v-43); <br/> If (V> 0) {<br/> V = (byte) (V = '$ ')? 0: V-61); <br/>}< br/> If (INC <= inlen) {<br/> Len ++; <br/>}< br/> If (LEN> 1) {<br/> outc + = (len-1 ); <br/>}< br/> return outc; <br/>}</P> <p> while (INC <inlen) {<br/> for (LEN = 0, I = 0; I <4 & Inc <inlen; I ++) {<br/> V = 0; <br/> while (INC <= inlen) & (V = 0) {<br/> V = (INC <inlen )? Instr [INC]: 0; <br/> Inc ++; <br/> V = (byte) (v <43 | V> 122 )? 0: cd64.charat (v-43); <br/> If (V> 0) {<br/> V = (byte) (V = '$ ')? 0: V-61); <br/>}< br/> If (INC <= inlen) {<br/> Len ++; <br/> If (V> 0) {<br/> in [I] = (byte) (V-1 ); <br/>}< br/> else {<br/> in [I] = 0; <br/>}< br/> If (LEN> 0) {<br/> decodeblock (In, out ); <br/> for (I = 0; I <len-1; I ++) {<br/> outstr [outc ++] = out [I]; <br/>}< br/> return outc; <br/>}< br/>

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.