Simple introduction and implementation of BASE64 coding

Source: Internet
Author: User
Tags rfc

The full Base64 definition is visible in RFC 1421 and RFC 2045. The encoded data is slightly longer than the original data for the original 43. In the e-mail message, according to RFC 822, each 76 characters, plus a carriage return to wrap. You can estimate the length of the data after encoding is approximately 135.1% of the original.

When converting, the data of three bytes is placed in a 24bit buffer successively, and the first byte is the high. If the data is less than 3byte, the remaining bits in the buffer are filled with 0. Then, each time the 6 (because 26=64) bit is removed, the character in its value selection ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ is used as the encoded output. Continue until the full input data conversion is complete.

When the original data length is not an integer multiple of 3, if the last input data is left, add 2 "=" after the encoding result, and if there are two input data left, add 1 "=" after the encoding result, and if there is no data left, do not add anything, so as to guarantee the correctness of the data restoration.

Example

For example, a quote from Thomas Hobbes "Leviathan":

Man was distinguished, not only by he reason, but by this singular passion from other animals, which are a lust of the mind , that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short Vehemenc E of any carnal pleasure.

After BASE64 encoding, it becomes:

Twfuiglzigrpc3rpbmd1axnozwqsig5vdcbvbmx5igj5ighpcybyzwfzb24sigj1dcbiesb0aglzihnpbmd1bgfyihbhc3npb24gznjvbsbvdghlcibhbmlty Wxzlcb3agljacbpcybhigx1c3qgb2ygdghlig1pbmqsihroyxqgynkgysbwzxjzzxzlcmfuy2ugb2ygzgvsawdodcbpbib0agugy29udgludwvkigfuzcbpbm Rlzmf0awdhymxligdlbmvyyxrpb24gb2yga25vd2xlzgdllcblegnlzwrzihrozsbzag9ydcb2zwhlbwvuy2ugb2ygyw55ignhcm5hbcbwbgvhc3vyzs4 =
    • Code "man"
TD colspan= "1" rowspan= "1" >0
Text M A N
ASCII encoding 77 97 110
bits 0 1 0 1 1 0 1 0 1 1 0 0 0 0 1 0 1 1 0 1 1 1 0
Index 19 22 5 46
BASE64 encoding T W F U

In this example, the BASE64 algorithm encodes three characters into 4 characters

Base64 Index Table:

value character value character value character value character
0 A 16 Q 32 G 48 W
1 B 17 R 33 H 49 X
2 C 18 S 34 I 50 Y
3 D 19 T 35 J 51 Z
4 E 20 U 36 K 52 0
5 F 21st V 37 L 53 1
6 G 22 W 38 M 54 2
7 H 23 X 39 N 55 3
8 I 24 Y 40 O 56 4
9 J 25 Z 41 P 57 5
10 K 26 A 42 Q 58 6
11 L 27 B 43 R 59 7
12 M 28 C 44 S 60 8
13 N 29 D 45 T 61 9
14 O 30 E 46 U 62 +
15 P 31 F 47 V 63 /

If the number of bytes to encode cannot be divisible by 3 and the last 1 or 2 bytes, then the following method can be used to process: First use the 0-byte value at the end of the top, so that it is divisible by 3, and then the BASE64 encoding. After the encoded BASE64 text, add one or two ' = ' sign, which represents the number of bytes to be replenished. That is, when the last remaining eight-bit byte (a byte), the last 6-bit BASE64 byte block has four bits is 0 value, and the last two equals, if the last two bits of eight bytes (2 byte), the last 6 bits of the base byte block has two bits is the 0 value, and finally append an equal sign. Refer to the following table:

TD colspan= "1" rowspan= "1" >0
Text (1 bytes) A

bits 0 1 0 0 0 0 0 1















bits (complement 0) 0 1 0 0 0 0 0 1 0 0 0 0











BASE64 encoding Q Q

text (2 Byte) B C
bits 0 1 0 0 0 1 0 0 1 0 0 0 0 1 1

x x x x x x
bits (complement 0) 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 x x x x x x
BASE64 encoding Q K M


public class base64{/** * encode raw data as base64 encoding */static public char[] encode (byte[] data) {char[] out =        New Char[((Data.length + 2)/3) * 4];                for (int i = 0, index = 0; i < data.length; i + = 3, index + = 4) {Boolean quad = false;                Boolean trip = false;                int val = (0xFF & (int) data[i]);                Val <<= 8;                        if ((i + 1) < Data.length) {val |= (0xFF & (int) Data[i + 1]);                Trip = true;                } Val <<= 8;                        if ((i + 2) < Data.length) {val |= (0xFF & (int) Data[i + 2]);                Quad = true; } Out[index + 3] = alphabet[(quad?                (Val & 0x3F): 64)];                Val >>= 6; Out[index + 2] = alphabet[(trip?)                (Val & 0x3F): 64)]; Val >>= 6;                Out[index + 1] = alphabet[val & 0x3F];                Val >>= 6;        Out[index + 0] = alphabet[val & 0x3F];    } return out;  }/** * Decodes base64 encoded data into raw data */static public byte[] Decode (char[] data) {int len = ((Data.length + 3)      /4) * 3;      if (data.length > 0 && data[data.length-1] = = ' = ')--len;      if (Data.length > 1 && data[data.length-2] = = ' = ')--len;      Byte[] out = new Byte[len];      int shift = 0;      int accum = 0;      int index = 0;        for (int ix = 0; ix < data.length; ix++) {int value = Codes[data[ix] & 0xFF];          if (value >= 0) {Accum <<= 6;          SHIFT + = 6;          Accum |= value;            if (Shift >= 8) {Shift-= 8;          out[index++] = (byte) ((Accum >> shift) & 0xff); }}} if (Index! = out.length) throw new Error ("Miscalculated data length! ");    return out; } static private char[] Alphabet = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=". ToCharArray ()    ;    Static private byte[] codes = new byte[256];            static {for (int i = 0; i <; i++) codes[i] = 1;            for (int i = ' a '; I <= ' Z '; i++) codes[i] = (byte) (i-' a ');            for (int i = ' a '; I <= ' z '; i++) codes[i] = (byte) (+ I-' a ');            for (int i = ' 0 '; I <= ' 9 '; i++) codes[i] = (byte) (+ i-' 0 ');            codes[' + '] = 62;    codes['/'] = 63;      } public static void Main (string[] args) throws Exception {//Encrypt to base64 String strsrc = "Forest";      String strout = new String (Base64.encode (Strsrc.getbytes ()));      System.out.println (Strout);      String strOut2 = new String (Base64.decode (Strout.tochararray ()));    System.out.println (STROUT2); }}


Simple introduction and implementation of BASE64 coding

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.