Base64 is one of the most common encoding methods for transmitting 8Bit bytes of code on the network, and you can view rfc2045~rfc2049, which has a detailed specification of MIME. BASE64 encoding can be used to pass longer identity information in an HTTP environment. For example, in the Java Persistence System hibernate, Base64 is used to encode a long unique identifier (typically 128-bit uuid) as a string that is used as a parameter in an HTTP form and an HTTP GET URL. In other applications, it is often necessary to encode binary data as appropriate in the form of URLs (including hidden form fields). At this time, the use of BASE64 encoding is not readable, that is, the encoded data will not be directly visible to the naked eye.
The idea of BASE64 encoding is to re-encode the data using 64 basic ASCII characters. It splits the data that needs to be encoded into bytes
Array. A group of 3 bytes. The 24-bit data is sorted sequentially, and the 24 bits of data are divided into 4 groups, 6 bits per group. Before the highest bit of each group
Complement two 0 to make a single byte. This re-encodes a 3-byte set of data into 4 bytes. When the number of bytes of data to encode is not
Integer multiples of 3, which means that the last group is less than 3 bytes at the time of grouping. At this point the last group is populated with 1 to 2 0 bytes. And after the final encoding is complete,
Add 1 to 2 "=" at the end.
Example: ABC will be BASE64 encoded:
1, first take the ASCII code value of the ABC corresponding. A (+) B (+) C (67);
2, then take the binary value A (01000001) B (01000010) C (01000011);
3, then the three bytes of the binary code to connect (010000010100001001000011);
4, and then 6 bits of the unit into 4 data blocks, and the highest bit filled two 0 after the formation of 4 bytes of the encoded value, (00010000) (00010100
) (00001001) (00000011), where the blue part is the real data;
5, then the four bytes of data into 10 binary numbers (16) (20) (9) (3);
6. Finally, according to the 64 basic character BASE64, the corresponding ASCII code character (Q) (U) (J) (D) is found, and the value is actually
The index of the data in the character table.
Note: BASE64 character: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/
Encrypted with the BASE64 algorithm, when the string is too long (typically more than 76), it automatically adds a newline character to the middle, and the string ends with a newline character. Result in inconsistent results when docking with other modules.
Android Solutions:
Will
Android.util.Base64.encodeToString (input, Base64.default)
Into
Android.util.Base64.encodeToString (input, base64.no_wrap);
C # frombase64string decoding line-wrapping problems