An example analysis of Base64 encryption and decryption method in Python

Source: Internet
Author: User
Tags base64 printable characters in python

This example describes the Base64 encryption and decryption method in Python. Share to everyone for your reference. The specific analysis is as follows:

First, base64

Base64 is a representation that represents binary data based on 64 printable characters. Because 2 of the 6 times equals 64, each 6 bit 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 transport encoding for e-mail messages. printable characters in Base64 include letters A-Z, a-Z, number 0-9, 62 characters in total, and two printable symbols that differ in different systems. The encoded data is slightly longer than the original data, which is 4/3 of the original.

Base64 is often used to represent, transmit, and store binary data (or a string that is not printable) in situations where text data is typically processed. Includes MIME-Email,email via MIME, which stores complex data in XML.

Purpose in the message:

In MIME-formatted e-mail messages, base64 can be used to encode binary byte-sequence data into text consisting of an ASCII character sequence. When used, specifies the base64 in the transmission encoding mode. The characters used include 26 uppercase and lowercase letters, plus 10 digits, and plus "+", Slash "/", a total of 64 characters, and the equal sign "=" is used as a suffix.

What to use in the URL:

The standard Base64 is not suitable for transmission directly to the URL. Because the URL encoder changes the "/" and "+" characters in the standard Base64 to form "%xx", these "%" numbers need to be converted when they are stored in the database, because the "%" number is used as a wildcard in ANSI SQL.

To solve this problem, you can use an improved BASE64 encoding for URLs, it does not populate the ' = ' number at the end, and changes the "+" and "/" in the standard Base64 to "*" and "-", thus eliminating the conversion to be made when the URL is encoded and stored in the database. It avoids the increase of the length of encoded information in this process, and unifies the format of object identifiers such as databases, forms, and so on.

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

Second, use in Python

?

1 2 3 4 5 6 7 8 9 C:python27>python python 2.7.2 (default, June, 14:24:46) [MSC v.1500-bit (AMD64)] on Win32 Type "Help", "copy Right "," credits "or" license "for the more information. >>> Import base64 >>> str = ' haha ' >>> base64.b64encode (str) ' agfoyq== ' >>> base64.b6 4decode (' agfoyq== ') ' haha '

 Third, the other way, this is more important

Base64.b64encode (s[, Altchars])

Base64.b64decode (s[, Altchars])

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

Base64.urlsafe_b64encode (s)

Base64.urlsafe_b64decode (s)

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

Base64.b32encode (s)

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

Base64.b16encode (s)

Base64.b16decode (s[, Casefold])

I hope this article will help you with your Python programming.

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.