换行:
The encoded output stream must be represented in lines of no more than 76 characters each。
MIME规定,base64编码中,每76个字符后,要加一个\n
避免换行:
Android:
通过指定标志位:Base64.NO_WRAP
Base64编码表及示例:
| Char | Value | Char | Value | Char | Value | Char | ||||
|---|---|---|---|---|---|---|---|---|---|---|
| 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 |
21 | 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 | / |
Text content
| M | ||||||||||||||||||||||||
| ASCII | 77 (0x4d) | 0 (0x00) | 0 (0x00) | |||||||||||||||||||||
| Bit pattern | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| Index | 19 | 16 | 0 | 0 | ||||||||||||||||||||
| Base64-encoded | T | Q | = | = | ||||||||||||||||||||
If there were two significant input bytes (say ‘Ma‘), the first three base64 digits are picked (18 bits). ‘=‘ characters might be added to make the last block contain four base64 characters.
| Text content | M | a | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ASCII | 77 (0x4d) | 97 (0x61) | 0 (0x00) | |||||||||||||||||||||
| Bit pattern | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| Index | 19 | 22 | 4 | 0 | ||||||||||||||||||||
| Base64-encoded | T | W | E | = | ||||||||||||||||||||
As a result, when the last input group contains only one octet, the four least significant bits of the last used 6-bit block are set to zero:
| Bit pattern | 0 | 1 | 0 | 0 | 0 | 0 |
|---|---|---|---|---|---|---|
| Index | 16 | |||||
| Base64-encoded | Q | |||||
And when the last input group contains two octets, the two least significant bits of the last used 6-bit block are set to zero:
| Bit pattern | 0 | 0 | 0 | 1 | 0 | 0 |
|---|---|---|---|---|---|---|
| Index | 4 | |||||
| Base64-encoded | E | |||||
The ‘==‘ sequence indicates that the last group contained only one byte, and ‘=‘ indicates that it contained two bytes. The example below illustrates how truncating the input of the whole of the above quote changes the output padding:
| Length | Input | Length | Output | Padding |
|---|---|---|---|---|
| 20 | any carnal pleasure. | 28 | YW55IGNhcm5hbCBwbGVhc3VyZS4= | 1 |
| 19 | any carnal pleasure | 28 | YW55IGNhcm5hbCBwbGVhc3VyZQ== | 2 |
| 18 | any carnal pleasur | 24 | YW55IGNhcm5hbCBwbGVhc3Vy | 0 |
| 17 | any carnal pleasu | 24 | YW55IGNhcm5hbCBwbGVhc3U= | 1 |
| 16 | any carnal pleas | 24 | YW55IGNhcm5hbCBwbGVhcw== | 2 |
The same characters will be encoded differently depending on their position within the three-octet group which is encoded to produce the four characters. For example:
| Input | Output |
|---|---|
| pleasure. | cGxlYXN1cmUu |
| leasure. | bGVhc3VyZS4= |
| easure. | ZWFzdXJlLg== |
| asure. | YXN1cmUu |
| sure. | c3VyZS4= |
原文:http://www.cnblogs.com/rushoutasia/p/5628361.html