Python Base64 encoding and pythonbase64 Encoding
0x00 Base64 Introduction
0x01 examples of common scenarios
0x02 encoding and decoding process
0x03 Base64 encoding and decoding in Python
0x00 Base64 Introduction
We know that any data in the computer is stored in ascii code, and the ascii code 128 ~ The value between 255 is an invisible character. When exchanging data on the network, for example, transferring data from A to B Usually goes through multiple routing devices. Because different devices have different character processing methods, in this way, invisible characters may be processed incorrectly, which is not conducive to transmission. Therefore, we need to first encode the data with Base64 and convert all the data into visible characters. This reduces the possibility of errors.
From here: https://www.zhihu.com/question/36306744/answer/71626823
Encoding value range: upper-case letters (A-Z), lower-case letters (a-z), numbers (0-9), special characters: '+' and '/' a total of 64 characters.
0x01 examples of common scenarios
Example 1: Base64 Encoding is most common in mail and webpage transmission. For example, you can view the Content-Transfer-Encoding: base64 in the original email text in QQ mail, the end of the email is base64 encoded content, for example:
Content-Type:text/html; charset="UTF-8"Content-Transfer-Encoding:base64X-SMTPAPI:List-Unsubscribe:
Original Email:
You have used the password reset function in the blog garden, please change the password through the address below: https://passport.cnblogs.com/ResetPassword.aspx? Id = f8b23500 ****** 00b5606d9c3e6416dc1_10211802598811938222461
Example 2: the website http://pythontutor.com/visualize.html?mode=edit,base64;used to transmit image data during webpage transmission:
Here, base64 is used to transmit image data during webpage protocol transmission. The original image data is as follows:
GIF89a!,L;
0x02 encoding and decoding process
Base64 index table:
| Value |
Character |
|
Value |
Character |
|
Value |
Character |
|
Value |
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 |
/ |
Here we use two letters 'AB' to analyze the detailed Encoding Process of base64:
| Text |
A |
B |
|
| ASCII code |
97 |
98 |
|
| Binary bit |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
1 |
0 |
|
|
|
|
|
|
|
|
| Index |
24 |
22 |
8 |
|
| Base64 encoding |
Y |
W |
I |
= |
After encoding: YWI =
Coding Process:
- Base64 regards the two-byte 'AB' as three-byte text data.
- Convert 'A' and 'B' to a 10-digit ASCII Code, for example, 97, 98, and 0 (add 1 0)
- Convert the 10th to 2nd, which correspond to 01100001, 01100010, and 00000000, respectively.
- Divide every 6 bits into 4 6 bits and 6 bits. After the split, 011000, 010110, and 001000 (0010 tail is supplemented with 0 to make up 6 bits)
- Convert three valid 6-bit binary into a 10-digit system, that is, the index number of the base64 encoded index, corresponding to: 24, 22, 8
- Convert the three 10-digit index numbers into the corresponding characters in turn, which is the encoding result: Y, W, I
- However, only three bytes of the encoding result are returned. Therefore, an empty byte is supplemented and '=' is used to fill the upper space, which is then combined into a 4-byte character: YWI =
Text character --> decimal --> binary --> cut at every 6 bits --> convert the cut value to 10 hexadecimal (index value) --> convert the character
Test the encoding Letter 'A' in ipython ':
In [9]: ord ('A') Out [9]: 97In [10]: bin (97) Out [10]: '0b1100001 'In [11]: '{:> 08b }'. format (97) # convert to 2 hexadecimal Out [11]: '000000' In [12]: int ('000000', 2) # Add 0 to the first 6 digits and then perform the 10-digit conversion. That is, the index value of the base64 table is 24. Corresponding character: YOut [12]: 24.
Decoding process:
Encode --> convert to base64 index value --> convert index value to 6-bit binary --> convert every 8-bit into 2-digit --> convert to 10-digit --> convert to original text
0x03 Base64 encoding and decoding in Python
You need to import the python base64 Library
In python 3 or later versions, you must convert the character type to bytes before encoding or decoding it.
In [1]: import base64In [2]: str = 'ab'In [3]: base64.b64encode(str.encode())Out[3]: b'YWI='In [4]: str2 = b'YWI='In [5]: base64.b64decode(str2.decode())Out[5]: b'ab'In [6]: base64.b64decode(str2.decode()).decode()Out[6]: 'ab'
Summary:
With base64 encoding, we review the ascii, binary, 10-hexadecimal, and Hex Encoding, which further enhances understanding of data transmission between computers and networks, it will be of great help to the subsequent network programming courses.