Base64 JAVA Implementation Method

Source: Internet
Author: User

Base64 encoding. The decoding algorithm is as follows:

Public class base64 <br/> {<br/>/** <br/> * encode the original data into base64 encoding <br/> */<br/> static public char [] encode (byte [] data) <br/> {<br/> char [] out = new char [(data. length + 2)/3) * 4]; </P> <p> for (INT I = 0, Index = 0; I <data. length; I + = 3, index + = 4) <br/>{< br/> Boolean quad = false; <br/> Boolean trip = false; <br/> int val = (0xff & (INT) data [I]); <br/> Val <= 8; <br/> If (I + 1) <data. Length) <br/>{< br/> Val | = (0xff & (INT) data [I + 1]); <br/> trip = true; <br/>}< br/> Val <= 8; <br/> If (I + 2) <data. length) <br/>{< br/> Val | = (0xff & (INT) data [I + 2]); <br/> quad = true; <br/>}< br/> out [index + 3] = alphabet [(quad? (Val & 0x3f): 64)]; <br/> Val >>=6; <br/> out [index + 2] = alphabet [(trip? (Val & 0x3f): 64)]; <br/> Val >>=6; <br/> out [index + 1] = alphabet [Val & 0x3f]; <br/> Val >>= 6; <br/> out [index + 0] = alphabet [Val & 0x3f]; <br/>}< br/> return out; <br/>}< br/>/** <br/> * decodes base64 encoded data into raw data. <br/> */<br/> static public byte [] Decode (char [] data) <br/>{< br/> int Len = (data. length + 3)/4) * 3; <br/> If (data. length> 0 & Data [data. length-1] = ') <br/> -- Len; <br /> If (data. length> 1 & Data [data. length-2] = ') <br/> -- Len; <br/> byte [] out = new byte [Len]; <br/> int shift = 0; <br/> int accum = 0; <br/> int Index = 0; <br/> for (int ix = 0; IX <data. length; ix ++) <br/>{< br/> int value = codes [DATA [ix] & 0xff]; <br/> If (value> = 0) <br/>{< br/> accum <= 6; <br/> SHIFT + = 6; <br/> accum | = value; <br/> If (shift> = 8) <br/>{< br/> shift-= 8; <br/> Out [index ++] = (byte) (accum> shift) & 0xff ); <br/>}< br/> If (index! = Out. Length) <br/> throw new error ("miscalculated data length! "); <Br/> return out; <br/>}</P> <p> static private char [] alphabet =" character ++/= ". tochararray (); </P> <p> static private byte [] codes = new byte [256]; <br/> static {<br/> for (INT I = 0; I <256; I ++) <br/> codes [I] =-1; <br/> for (INT I = 'a'; I <= 'Z'; I ++) <br/> codes [I] = (byte) (I-'A'); <br/> for (INT I = 'a'; I <= 'Z'; I ++) <br/> codes [I] = (byte) (26 + I-'A'); <br/> for (INT I = '0 '; I <= '9'; I ++) <br/> codes [I] = (byte) (52 + I-'0 '); <br/> codes ['+'] = 62; <br/> codes ['/'] = 63; <br/>}</P> <p> Public static void main (string [] ARGs) throws exception <br/>{< br/> // encrypted to base64 <br/> string strsrc = "Lin "; <br/> string Strout = new string (base64.encode (strsrc. getbytes (); <br/> system. out. println (Strout); </P> <p> string strout2 = new string (base64.decode (Strout. tochararray (); <br/> system. out. println (strout2); <br/>}< br/>}

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.