The php version of the base64 custom encoding table ,. The php version of the base64 user-defined encoding table. during an interface connection, you need to use the base64 user-defined encoding table for encoding and decoding. you searched the php version from the Internet, the principles are usually compared with the php version of the base64 custom encoding table,
During an interface connection, you need to use a base64 custom encoding table for encoding and decoding. after searching for the table on the Internet, the principle is quite thorough, the provided encoding example is not decoded. The following is an example of self-implemented base64 custom dictionary decoding. it is rough and there should be no problem in testing the assembly, if you need this item, you can take a look at it. First, you can take the principles of others' blogs.
Base64 encoding is a commonly used encoding method in program development. It is a representation of binary data based on 64 printable characters. It is usually used to store and transmit binary data encoding methods! It is also a common MIME (multi-purpose Internet mail extension, mainly used as the e-mail standard) encoding method that can print characters to represent binary data! It is actually just a way to define the transfer content with printable characters, and does not produce a new character set! Sometimes, after learning the conversion idea, we can also combine our actual needs to construct some of our own interfaces to define encoding methods. Okay. let's take a look at it. let's take a look at its conversion ideas!
Base64 conversion principle
It uses 64 printable characters to represent all binary data. Because the power of 6 in 2 is equal to 64, each 6-digit dollar can be used as a unit, corresponding to a printable character. We know that the three bytes have 24 bits, which exactly corresponds to four Base64 units, that is, the three bytes need to be expressed by four Base64 printable characters. The printable characters in Base64 include letters A-Z, a-z, and numbers 0-9, which have A total of 62 characters. the Two printable characters are generally different in different systems. However, the other two characters that we often call Base64 are: "+ /". The 64 characters are as follows.
| No. |
Character |
|
No. |
Character |
|
No. |
Character |
|
No. |
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 |
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 |
/ |
During the conversion, three bytes of data are successively put into a 24-bit buffer, and the first byte occupies a high position. If the data is less than 3 bytes, the remaining bit in the buffer zone is supplemented with 0. Then, retrieve 6 bits each time and select the values according to the values.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/As the encoded output. Continue until all input data is converted.
If the last two inputs are left, add 1 "=" after the encoding result. if the last one is left, add 2 "=" after the encoding result. if no data is left, do not add anything to ensure the correctness of data restoration.
The encoded data is slightly longer than the original data, which is 4/3 of the original data. All characters are encoded, so some printable characters are not encoded like Quoted-printable. Therefore, it is not as readable as Quoted-printable encoding!
| Text |
M |
A |
N |
| ASCII code |
77 |
97 |
110 |
| Binary bit |
0 |
1 |
0 |
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 |
The Ascii code of M is 77, the first six digits correspond to 19 characters, and the base64 character is T, and so on. Other character codes can be automatically converted! Let's see if it's not exactly 3 bytes!
| Text (1 Byte) |
A |
|
|
| Binary bit |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Binary bit (0) |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
|
|
|
|
|
|
|
|
|
|
|
|
| Base64 encoding |
Q |
Q |
= |
= |
| Text (2 bytes) |
B |
C |
|
| Binary bit |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
1 |
|
|
X |
X |
X |
X |
X |
X |
| Binary bit (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 |
= |
This lecture is very thorough, the original address: http://www.cnblogs.com/chengmo/archive/2014/05/18/3735917.html
Class base64 {
Public $ base64_config = ['A', 'B', 'C', 'D', 'e', 'F', 'G', 'H ', 'I', 'J', 'K', 'L', 'M', 'n', 'O', 'P', 'Q', 'R ','s ', 'T', 'u', 'V', 'W', 'X', 'y', 'z', 'A',' B ', 'C', 'D', 'e', 'F', 'G', 'H', 'I', 'J', 'K', 'L ', 'M', 'n', 'O', 'P', 'Q', 'R', 'S', 'T', 'u', 'V ', 'W', 'X', 'y', 'z', '0', '1', '2', '3', '4', '5 ', '6', '7', '8', '9', '_', '-'];
Public function getBytes ($ string ){
$ Data = iconv ("UTF-8", "GBK", $ string );
Return unpack ("C *", $ data );
}
Public function array_index ($ t ){
Return array_search ($ t, $ this-> base64_config );
}
Public function decode ($ str ){
$ Str = str_replace ("! "," ", $ Str );
$ Slen = strlen ($ str );
$ Mod = $ slen % 4;
$ Num = floor ($ slen/4 );
$ Desc = [];
For ($ I = 0; $ I <$ num; $ I ++ ){
$ Arr = array_map ("base64: array_index", str_split (substr ($ str, $ I * 4, 4 )));
$ Desc_0 = ($ arr [0] <2) | ($ arr [1] & 48)> 4 );
$ Desc_1 = ($ arr [1] & 15) <4) | ($ arr [2] & 60)> 2 );
$ Desc_2 = ($ arr [2] & 3) <6) | $ arr [3];
$ Desc = array_merge ($ desc, [$ desc_0, $ desc_1, $ desc_2]);
}
If ($ mod = 0) return implode ('', array_map (" chr ", $ desc ));
$ Arr = array_map ("base64: array_index", str_split (substr ($ str, $ num * 4, 4 )));
If (count ($ arr) = 1 ){
$ Desc_0 = $ arr [0] <2;
If ($ desc_0! = 0) $ desc = array_merge ($ desc, [$ desc_0]);
} Else if (count ($ arr) = 2 ){
$ Desc_0 = ($ arr [0] <2) | ($ arr [1] & 48)> 4 );
$ Desc = array_merge ($ desc, [$ desc_0]);
} Else if (count ($ arr) = 3 ){
$ Desc_0 = ($ arr [0] <2) | ($ arr [1] & 48)> 4 );
$ Desc_1 = ($ arr [1] <4) | ($ arr [2] & 60)> 2 );
$ Desc = array_merge ($ desc, [$ desc_0, $ desc_1]);
}
Return implode ('', array_map (" chr ", $ desc ));
}
Public function encode ($ str ){
$ Byte_arr = $ this-> getBytes ($ str );
$ Slen = count ($ byte_arr );
$ Smod = ($ slen % 3 );
$ Snum = floor ($ slen/3 );
$ Desc = array ();
For ($ I = 1; $ I <= $ snum; $ I ++ ){
$ Index_num = ($ i-1) * 3;
$ _ Dec0 = $ byte_arr [$ index_num + 1]> 2;
$ _ Dec1 = ($ byte_arr [$ index_num + 1] & 3) <4) | ($ byte_arr [$ index_num + 2]> 4 );
$ _ Dec2 = ($ byte_arr [$ index_num + 2] & 0xF) <2) | ($ byte_arr [$ index_num + 3]> 6 );
$ _ Dec3 = $ byte_arr [$ index_num + 3] & 63;
$ Desc = array_merge ($ desc, array ($ this-> base64_config [$ _ dec0], $ this-> base64_config [$ _ dec1], $ this-> base64_config [$ _ dec2], $ this-> base64_config [$ _ dec3]);
}
If ($ smod = 0) return implode ('', $ desc );
$ N = ($ snum * 3) + 1;
$ _ Dec0 = $ byte_arr [$ n]> 2;
/// Only one byte
If (! Isset ($ byte_arr [$ n + 1]) {
$ _ Dec1 = ($ byte_arr [$ n] & 3) <4 );
$ _ Dec2 = $ _ dec3 = "! ";
} Else {
/// 2 bytes
$ _ Dec1 = ($ byte_arr [$ n] & 3) <4) | ($ byte_arr [$ n + 1]> 4 );
$ _ Dec2 = $ this-> base64_config [($ byte_arr [$ n + 1] & 0xF) <2];
$ _ Dec3 = "! ";
}
$ Desc = array_merge ($ desc, array ($ this-> base64_config [$ _ dec0], $ this-> base64_config [$ _ dec1], $ _ dec2, $ _ dec3 ));
Return implode ('', $ desc );
}
}
$ Base64 = new base64 ();
// Echo array_search ("E", $ base64-> base64_config );
// Exit;
$ Tt = $ base64-> encode ("The Chinese character is not afraid of asdas23232323 ,. ,");
Echo $ tt ."
";
$ Ttt = $ base64-> decode ($ tt );
Echo $ ttt ."
";
In the php version, you need to use a base64 custom encoding table for encoding and decoding during an interface connection. you searched the internet and compared the principles...