Python module introduction-base64 RFC 3548: Base16, Base32, and Base64 data encoding

Source: Internet
Author: User

Python module introduction-base64 RFC 3548: Base16, Base32, and Base64 data encoding

Base64 is a representation of binary data based on 64 printable characters. Because the power of 6 in 2 is equal to 64, each 6-digit element is a unit and corresponds 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. Then add two zeros before the six bits to form the 8-bit one-byte format. Some other encoding methods such as uuencode, and later binhex versions use different 64 character sets to represent six binary numbers, but they are not called base64.

Base64 is usually used to represent, transmit, and store binary data when processing text data. Including MIME emails and email via MIME, storing complex data in XML

The Python Base64 module provides data encoding and decoding in RFC3548 (convert binary data into ASCII sequences suitable for plaintext transmission, as specified in RFC3548. This standard defines the Base16, Base32, and Base64 algorithms. Any binary string encoded and decoded is converted to a text string, which can be sent securely by email as part of the URL, or include it in an http post request.

The Base64 module provides two interfaces. The new interface supports encoding and decoding of string objects with three letters. Traditional interfaces provide encoding and decoding of file objects and strings, but only use standard Base64 letters. Traditional interfaces are not described here.

Base64, base32, and base16 can be encoded to 6, 5, and 4 bits. 16, 32, and 64 indicate the number of characters used for encoding.

For more information about base64, see? Http://zh.wikipedia.org/wiki/Base64 ,? Http://tools.ietf.org/html/rfc822 ,? Http://tools.ietf.org/html/rfc1421 ,? Http://tools.ietf.org/html/rfc2045.

Function: RFC 3548: Base16, Base32, and Base64 data encoding. Type: standard module modules: binhex standard modules. Uu standard module. Quopri standard module. Binascii standard module. Base64 encoding and decoding

Base64.b64encode (s [, altchars]): uses a Base64 encoded string. S is the string to be encoded. Altchars is a string used to replace ++ and/. They have special meanings in URLs and file systems and usually need to be replaced.

Base64.b64decode (s [, altchars]): decodes a Base64 encoded string. S is the string to be decoded. Altchars and b64encode are the same.

See the example in the python module introduction:

>>> import base64>>> encoded = base64.b64encode('data to be encoded')>>> encoded'ZGF0YSB0byBiZSBlbmNvZGVk'>>> data = base64.b64decode(encoded)>>> data'data to be encoded'

The following is an instance in Wikipedia that contains a padding character:

>>> import base64>>> s = '''Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.'''>>> encoded_data = base64.b64encode(s)>>> print(encoded_data)TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=>>> base64.b64decode(encoded_data)'Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.'

 

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.