Base64 encryption and decryption operation methods and differences between versions in Python

Source: Internet
Author: User
Tags printable characters
Today, let's look at the use of base64 cryptographic functions, and the difference between Python2 and Python3.





First, base64



Base64 is a representation of binary data based on 64 printable characters. Since 2 of the 6 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, 3 bytes need to be represented by 4 printable characters. It can be used as the transmission encoding for e-mail. The printable characters in Base64 include the letter A-Z, A-Z, the number 0-9, which is 62 characters in total, and two printable symbols that differ from one system to another. The encoded data is slightly longer than the original data for the original 4/3.



Base64 are often used to represent, transmit, and store some binary data (or non-printable strings) in situations where text data is normally processed. Includes the MIME Email,email via mime, which stores complex data in XML.



What to use in the mail:



In MIME-formatted e-mail messages, base64 can be used to encode binary byte sequence data into text that consists of sequences of ASCII characters. When used, specify Base64 in the transfer encoding mode. The characters used include 26 uppercase and lowercase letters, plus 10 digits, and a plus sign "+", a slash "/", a total of 64 characters, and an equal sign "=" to use as a suffix.



What to use in the URL:



The standard Base64 is not intended to be transmitted directly in the URL because the URL encoder will change the "/" and "+" characters in the standard Base64 into forms such as "%XX", and these "%" numbers need to be converted when they are deposited into the database because ANSI SQL has used the "%" number as a wildcard character.



To solve this problem, an improved BASE64 encoding for URLs is used, which does not populate the ' = ' at the end and changes the "+" and "/" in standard Base64 to "*" and "-" respectively, thus eliminating the need for conversion in URL codec and database storage. Avoids the increase in the length of encoded information in this process, and unifies the format of object identifiers in databases, forms, and so on.



There is also an improved BASE64 variant for regular expressions that changes "+" and "/" to "!" and "-", because "+", "*" may have special meanings in regular expressions.



Second, use in Python



Python2 and Python3 are different on base64 processing, the arguments passed in Python3 cannot be Unicode strings and need to be converted



Python2:


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'
>>>


Python3:


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'
>>>


Attention:



The first thing to figure out is that the representation of a string inside Python is Unicode encoding.



Therefore, in encoding conversion, it is often necessary to use Unicode as an intermediate encoding, that is, decoding other encoded strings (decode) into Unicode, and then encoding from Unicode (encode) to another.



The role of Decode is to convert other encoded strings to Unicode encoding.



such as Str1.decode (' gb2312 '), means that the gb2312 encoded string is converted to Unicode encoding.



The role of encode is to convert Unicode encoding into other encoded strings,



such as Str2.encode (' gb2312 '), means converting a Unicode-encoded string to GB2312 encoding.



Third, the other method



Base64.b64encode (s[, Altchars])



Base64.b64decode (s[, Altchars])



Altchars is an optional parameter that is used to replace a string of two lengths of + and/.



Base64.urlsafe_b64encode (s)



Base64.urlsafe_b64decode (s)



This method used-instead of +, with _ instead of/, so that the encoded string can be placed in the URL can be normal access



Base64.b32encode (s)



Base64.b32decode (s[, casefold[, MAP01])



Base64.b16encode (s)



Base64.b16decode (s[, Casefold])


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.