Base64 Module Method:
[
' Encode ',' Decode ',' Encodebytes ',' Decodebytes ',
' B64encode ',' B64decode ',' B32encode ',' B32decode ',
' B16encode ',' B16decode ',
' B85encode ',' B85decode ', ' A85encode ', ' A85decode ',
' Standard_b64encode ', ' Standard_b64decode ',
' Urlsafe_b64encode ', ' Urlsafe_b64decode ',
]the /c3>
Here to explain< Span style= "color: #008080" > < Span style= "color: #7a7a7a" >b64encode ' ' b64decode ' , < Span style= "color: #008080" > encodebytes ' , ' decodebytes ' These four methods are more commonly used.
Importbase64s1= b'Hello world' #byte stringA1 = Base64.b64encode (s1)#Base64 encoded byte stringPrint('A1', A1) B1= Base64.b64decode (a1)#Base64 decoding, Returning the original byte stringPrint('B1', B1.decode ())#convert a byte string to a stringPrint('------') S2= b'Hello world' #byte stringA2 = Base64.encodebytes (s2)#base64 encoding into multi-line byte stringsPrint('A2', A2) B2= Base64.decodebytes (a2)#Base64 decoding, Returning the original byte stringPrint('B2', B2.decode ())#convert a byte string to a stringPrint('------')
Log
A1 b'sgvsbg8gv29ybgq='b1 Helloworld------A2 b' sgvsbg8gv29ybgq=\n'B2 Helloworld------
The following is the principle of Base64 coding: Https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001399413803339f4bbda5c01fc479cbea98b1387390748000
Base64 is a method that uses 64 characters to represent arbitrary binary Data.
exe
jpg
When we open these files with notepad, pdf
We all see a lot of garbled characters, because the binaries contain many character that can't be displayed and printed, so if you want text processing software like Notepad to handle binary data, you need a binary-to-string conversion method. Base64 is one of the most common binary encoding methods.
The Base64 principle is simple, first, to prepare a 64-character array:
[‘A‘, ‘B‘, ‘C‘, ... ‘a‘, ‘b‘, ‘c‘, ... ‘0‘, ‘1‘, ... ‘+‘, ‘/‘]
then, the binary data processing, every 3 bytes a group, is a 3x8=24
bit, divided into 4 groups, each group of exactly 6 bit:
So we get 4 numbers as index, then look up the table, get the corresponding 4 characters, is the encoded string.
therefore, the BASE64 encoding will encode 3 bytes of binary data into 4 bytes of text data, the length of 33%, the advantage is that the encoded text data can be displayed directly in the message body, Web pages and so On.
What if the binary data to be encoded is not a multiple of 3 and the last 1 or 2 bytes are left? Base64 with the byte at the end of the top \x00
, and then add 1 or 2 at the end of the code =
, indicating how many bytes, decoding the time, will be automatically removed.
Python module-base64