Base64 Encoding Rules:
Binary number of 24bit
|
Divided into 4 groups, insufficient to complement \x00
|
Each group is encoded in 1 bytes (denoted by a character in 64 ASCII codes), a total of 4 bytes, and insufficient compensation =
import base64a = base64.b64encode (b ' 12ABC ') print (a) print (str (a, ' utf-8 ')) b = base64.b64decode (a) print (b) print (str (b, ' utf-8 '))
Operation Result:
B ' mtjbymm= ' mtjbymm=b ' 12ABC ' 12ABC
However, there may be a ' + ' or ' \ ' number after encoding, which is not allowed in the URL, so it needs to be converted via the URL safe: +----_
Import base64a = Base64.b64encode (b ' I\xb7\x1d\xfb\xef\xff ') print (a) c = Base64.urlsafe_b64encode (b ' i\xb7\x1d\xfb\xef \xff ') print (c) Print (Base64.urlsafe_b64decode (c))
Operation Result:
B ' abcd++//' B ' abcd--__ ' B ' i\xb7\x1d\xfb\xef\xff '
Python built-in module: base64