Introduction to base64 encoding and its use in Java
Base64 is a frequently used encoding method. It was first generated to send email attachments. Today we have fixed a base64 decoding error. Let's take a look at it.
Principle
Base64 encoding converts three 8-bit bytes (3*8 = 24) to four 6-bit bytes (4*6 = 24), and then adds two zeros before the Six-bit encoding, the format is 8 bits per byte.
For example, the ASCII code of the string "123", "1" is 49, "2" is 50, and "3" is 51, as follows:
00110001 00110010 00110011
Divide the preceding 24 bytes into four groups, with each group of 6 bytes, for example:
00110001 00110010 00110011
Then add two zeros to each group, as shown below:
000011000001 0011000010 0000110011
In decimal format:
Then we can find the corresponding encoding according to the following base64 encoding table:
Table 1: the Base 64 alphabet value encoding 0 A 17 R 34 I 51 Z 1 B 18 S 35 J 52 0 2 C 19 t 36 K 53 1 3 D 20 u 37 L 54 2 4 E 21 V 38 m 55 3 5 F 22 W 39 n 56 4 6g 23x40 o 57 5 7 H 24 y 41 P 58 6 8 I 25 Z 42 Q 59 7 9 J 26 A 43 R 60 8 10 k 27 B 44 s 61 9 11 l 28 C 45 t 62 + 12 m 29 D 46 U 63/13 N 30 E 47 v 14 O 31 F 48 W (PAD) = 15 p 32G 49x16 Q 33 H 50 y
Therefore, after base64, It is mtiz.
If the original character is not an integer multiple of 3, 0 is used to fill in the corresponding position for less than 6 digits, and then two zeros are added before the six digits, for example, encode the string "1" as follows:
00110001
A group of six digits. The remaining parts must be zero, as shown in the following code:
001100010000
Then add two zeros to each group, as shown below:
0000110000010000
In decimal format:
12, 16
Corresponding to m and Q respectively. The remaining two digits are null and replaced by =. The encoding result is:
MQ =
Therefore, in many cases, after base64 encoding, it may end with =.
Note: According to rfc822, a carriage return line must be added for every 76 characters.
The principle of base64 decoding is the opposite. I will not repeat it again.
Implementation in Java
Java already has the encapsulated base64 implementation, which is very simple.CodeRight.
1. Encoding
-
- /**
- * Description: Encoding
-
- * @ Version1.0 Jan 2, 2009 11:48:48 by Cui hongbao
-
- * @ Param pbytes
-
- * @ Return
-
- */
-
- Public StaticString base64encode (Byte[] Pbytes ){
- Base64encoder base64 =NewBase64encoder ();
-
- ReturnBase64.encode (pbytes );
-
- }
-
- /**
-
- * Description: Encoding
-
- * @ Version1.0 Jan 2, 2009 11:49:02 by Cui hongbao
-
- * @ Param Str
- * @ Return
-
- */
-
- Public StaticString base64encode (string Str ){
-
- Try{
-
- ReturnBase64encode (Str. getbytes ("UTF-8"));
-
- }Catch(Unsupportedencodingexception e ){
- E. printstacktrace ();
-
- }
-
- Return Null;
-
- }
2. Decoding
-
- /**
-
- * Description: Decode
-
- * @ Version1.0 Jan 2, 2009 11:49:08 by Cui hongbao
- * @ Param Pcontent
-
- * @ Return
-
- * @ Throws ioexception
-
- */
-
- Public Static Byte[] Base64decode (string Pcontent)ThrowsIoexception {
-
- Base64decoder base64 =NewBase64decoder ();
- ReturnBase64.decodebuffer (Pcontent );
-
- }
References
Http://www.faqs.org/rfcs/rfc3548.html
Http://blog.csdn.net/zzyy00/archive/2006/09/06/1186959.aspx