JAVA-BASE64 algorithm

Source: Internet
Author: User
Tags control characters

The BASE64 algorithm was originally designed to solve the problem of e-mail transmission, the previous Mail Transfer Protocol only support ASCII code transmission, if you want to transfer binary files, tablets and video, is not transmitted, and BASE64 can encode the binary content into ASCII-only content, So it can be transferred.

Base64 algorithms are often said to be cryptographic algorithms, but accurately, Base64 is not a cryptographic algorithm, can only be considered a 64-character encoding algorithm.

It has a character mapping table, each word mappings a decimal code, a total of 64 characters mapped. Base64 the given data after the binary conversion and the character mapping table corresponds to the so-called ciphertext; the map table is as follows, and at the end of the mapping table is an equal sign, which is used as a complement for the complement.

number character number character number character
numbering 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 /

The implementation steps of the BASE64 algorithm are as follows:

1 converts the given string in characters to the corresponding character encoding (such as ASCII code); 2 convert the obtained character encoding to a binary code; 3 to do a group conversion operation on the obtained binary code, each of the 3 8-bit binary codes is a group that is converted to a group of 4 6-bit binary codes (less than 6 bits when the status is 0). This is a grouping change process, 3 8-bit binary codes and 4 6-bit binary codes are 24 bits in length; 4 for the 4 6-bit binary complements obtained, add 2-bit high 0 to the 6-bit binary code, comprising 4 8-bit binary codes; 5 4 8-bit binary code conversion bit 10 binary code will be obtained; 6. Convert the obtained decimal code to the corresponding character in the Base64 character table;

ASCII Code character encoding

For example, A is BASE64 encoded;

Final: qq==

The resulting encoding has two equal signs, because the original binary code is less than 24 bits, the most recent conversion to decimal code is less than 4, then you need to use equal-sign;

The BASE64 encoded string has a maximum of 2 equals signs because the remainder = the original byte number MOD 3, so the remainder can only be 0,1,2. Usually the first step in judging whether a string is a Base64 encoding is to determine if the end of the string is an equal sign. This also means that the BASE64 encoded string is 4 characters in length and can only be multiples of 4 characters.

Non-ASCII code character encoding

ASCII code can represent a decimal range of 0~127 characters, corresponding to the binary range is the 00000000~01111111,ASCII code contains Arabic numerals, uppercase and lowercase letters and some control characters, but does not contain Chinese, so there is GB2312, Codes such as GBK and UTF-8. gbk,gb2312 uses 2 bytes to represent a Chinese character, and UTF-8 uses 3 bytes to represent a Chinese character.

For example: The word UTF-8 in the form of Base64 coding;

Final: 5a+g

Of course, if we do not use UTF-8, but instead use GBK to encode, then the encoded result is not 5a+g.

The implementation of BASE64 in the JDK was not external to the public interface before JDK1.7, only a nonstandard implementation, located in the Sun.misc package, provides Base64encoder classes and Base64decoder classes. Because it is not, it is not recommended because subsequent JDK versions may remove support for these two classes.

And after the JDK8 has already had the corresponding interface, is located under the Java.util package.

Refer to the art of encryption and decryption from Java

JAVA-BASE64 algorithm

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.