Example Analysis of base64 encryption and decryption methods in python
This document describes the base64 encryption and decryption methods in python. Share it with you for your reference. The specific analysis is as follows:
I. base64
Base64 is a representation of binary data based on 64 printable characters. Because the power of 6 in 2 is equal to 64, every 6 bits are a unit and correspond to a printable character. The three bytes have 24 BITs, which correspond to four Base64 units. That is, the three bytes are represented by four printable characters. It can be used as the transmission code of the email. The printable characters in Base64 include letters A-Z, a-z, and numbers 0-9, which have A total of 62 characters. The two printable characters are different in different systems. The encoded data is slightly longer than the original data, which is 4/3 of the original data.
Base64 is usually used to represent, transmit, and store binary data (or non-printable strings) when processing text data ). Including MIME emails and email via MIME, storing complex data in XML.
Purpose in the email:
In MIME-format e-mails, base64 can be used to encode binary bytes into text consisting of ASCII character sequences. In use, specify base64 In the Transmission Encoding mode. The characters used include 26 uppercase/lowercase letters, 10 digits, plus the plus sign "+", and slash "/". A total of 64 characters are used as the suffix. The equal sign "=" is used.
Purpose in the URL:
The standard Base64 is not suitable for direct transmission in the URL, because the URL encoder will convert the "/" and "+" characters in the standard Base64 into the form of "% XX, these "%" signs need to be converted when they are stored in the database, because "%" has been used as a wildcard in ansi SQL.
To solve this problem, you can use an improved Base64 encoding for the URL, which does not fill the '=' sign at the end, the "+" and "/" in the standard Base64 are changed to "*" and "-" respectively, thus eliminating the need for conversion during URL encoding/decoding and database storage, this avoids the increase in the length of the encoding information during this process, and unifies the format of object identifiers in databases, forms, and other places.
There is also an improved Base64 variant for regular expressions. It changes "+" and "/" to "!". And "-", because "+" and "*" may have special meanings in regular expressions.
Ii. Use in python
?
1 2 3 4 5 6 7 8 9 |
C: \ Python27> python Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> Import base64 >>> Str = 'hahaha' >>> Base64.b64encode (str) 'Agfoyq =' >>> Base64.b64decode ('agfoyq = ') 'Hahaha' |
3. Other methods. This is important.
Base64.b64encode (s [, altchars])
Base64.b64decode (s [, altchars])
Altchars is an optional parameter used to replace a string of two lengths: + and.
Base64.urlsafe _ b64encode (s)
Base64.urlsafe _ b64decode (s)
In this method, "-" is used to replace "+" and "_" is used to replace "/". This ensures that the encoded string can be accessed normally in the url.
Base64.b32encode (s)
Base64.b32decode (s [, casefold [, map01])
Base64.b16encode (s)
Base64.b16decode (s [, casefold])
I hope this article will help you with Python programming.