Home > Article > Backend Development > Base64 encryption and decryption operation methods in Python and differences between versions
Today, let’s take a look at the use of base64 encryption functions and the differences between Python2 and Python3.
Base64 is a representation method based on 64 printable characters to represent binary data. Since 2 to the 6th power is equal to 64, every 6 bits is a unit, corresponding to a printable character. Three bytes have 24 bits, corresponding to 4 Base64 units, that is, 3 bytes need to be represented by 4 printable characters. It can be used as a transfer encoding for email. The printable characters in Base64 include letters A-Z, a-z, and numbers 0-9, so there are 62 characters in total. The other two printable symbols vary in different systems. The encoded data is slightly longer than the original data, 4/3 of the original.
Base64 is often used to represent, transmit, and store some binary data (or non-printable strings) in situations where text data is usually processed. Including MIME email, email via MIME, storing complex data in XML.
Use in email:
In MIME format emails, base64 can be used to convert binary characters Section sequence data is encoded into text consisting of a sequence of ASCII characters. When used, specify base64 in the transfer encoding method. The characters used include 26 uppercase and lowercase letters, plus 10 numbers, plus sign "+", slash "/", a total of 64 characters, and the equal sign "=" is used as a suffix.
Use in URL:
Standard Base64 is not suitable for transmission directly in the URL, because the URL encoder will change the "/" and "+" characters in standard Base64 into In the form of "%XX", these "%" signs need to be converted when stored in the database, because the "%" sign has been used as a wildcard character in ANSI SQL.
To solve this problem, an improved Base64 encoding for URLs can be used, which does not fill the '=' sign at the end, and changes the "+" and "/" in standard Base64 to " *" and "-", this eliminates the need for conversion during URL encoding, decoding and database storage, avoids the increase in the length of encoded information in the process, and unifies the format of object identifiers in databases, forms, etc. .
There is another improved Base64 variant for regular expressions, which changes "+" and "/" to "!" and "-", because "+" and "*" are used in regular expressions All formulas may have special meanings.
Python2 and Python3 are different in base64 processing. The parameters passed in under Python3 cannot be Unicode strings and need to be converted
Python 2.7.10 (default, Oct 23 2015, 19:19:21) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import base64 >>> str = 'pythontab.com' >>> base64.b64encode(str) 'cHl0aG9udGFiLmNvbQ==' >>> base64.b64decode('cHl0aG9udGFiLmNvbQ==') 'pythontab.com' >>>
Python 3.5.2 (default, Aug 24 2016, 16:48:29) [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import base64 >>> str = 'pythontab.com' >>> bytesStr = str.encode(encoding='utf-8') >>> bytesStr.decode() 'pythontab.com' >>> b64str = base64.b64encode(bytesStr) >>> b64str b'cHl0aG9udGFiLmNvbQ==' >>> base64.b64decode(b64str) b'pythontab.com' >>>
Note:
First of all, we must understand that the internal representation of strings in Python is unicode encoding.
Therefore, when doing encoding conversion, it is usually necessary to use unicode as the intermediate encoding, that is, first decode (decode) other encoded strings into unicode, and then encode (encode) from unicode into another encoding. kind of encoding.
The function of decode is to convert other encoded strings into unicode encoding.
For example, str1.decode('gb2312') means converting gb2312 encoded strings into unicode encoding.
The function of encode is to convert unicode encoding into other encoded strings,
such as str2.encode('gb2312'), which means converting unicode encoded string into gb2312 encoding.
base64.b64encode(s[, altchars])
base64.b64decode(s[, altchars])
altchars An optional parameter, a two-length string used to replace + and /.
base64.urlsafe_b64encode(s)
base64.urlsafe_b64decode(s)
In this method, - is used instead of +, and _ is used instead of /, which ensures that after encoding The string can be accessed normally when placed in the url
base64.b32encode(s)
base64.b32decode(s[, casefold[, map01]])
base64. b16encode(s)
base64.b16decode(s[, casefold])
The above is the detailed content of Base64 encryption and decryption operation methods in Python and differences between versions. For more information, please follow other related articles on the PHP Chinese website!