43 common built-in modules base64

Source: Internet
Author: User

Base64 is a method that uses 64 characters to represent arbitrary binary data.

exe jpg When we open these files with Notepad, pdf we all see a lot of garbled characters, because the binaries contain many character that can't be displayed and printed, so if you want text processing software like Notepad to handle binary data, you need a binary-to-string conversion method. Base64 is one of the most common binary encoding methods.

The Base64 principle is simple, first, to prepare a 64-character array:

['A','B','C', ...'a','b','C', ...'0','1', ...'+','/']

Then, the binary data processing, every 3 bytes a group, is a 3x8=24 bit, divided into 4 groups, each group of exactly 6 bit:

So we get 4 numbers as index, then look up the table, get the corresponding 4 characters, is the encoded string.

Therefore, the BASE64 encoding will encode 3 bytes of binary data into 4 bytes of text data, the length of 33%, the advantage is that the encoded text data can be displayed directly in the message body, Web pages and so on.

What if the binary data to be encoded is not a multiple of 3 and the last 1 or 2 bytes are left? Base64 with the byte at the end of the top \x00 , and then add 1 or 2 at the end of the code = , indicating how many bytes, decoding the time, will be automatically removed.

Python's built-in base64 codec that can be base64 directly:

>>> Import base64>>> Base64.b64encode (b'binary\x00string' b'ymluyxj5ahn0cmluzw=='>>> base64.b64decode (b' ) ymluyxj5ahn0cmluzw==') b'binary\x00string'

Since the standard BASE64 encoding may appear after the character + and / , in the URL can not be directly as a parameter, so there is a "url safe" base64 encoding, in fact, the character + and the / difference between - the _:

>>> Base64.b64encode (b'I\xb7\x1d\xfb\xef\xff') b'abcd++//'>>> Base64.urlsafe_b64encode (b'I\xb7\x1d\xfb\xef\xff') b'abcd--__'>>> Base64.urlsafe_b64decode ('abcd--__') b'I\xb7\x1d\xfb\xef\xff'

You can also define the order of 64 characters yourself, so that you can customize the BASE64 encoding, but it is generally not necessary at all.

Base64 is a method of encoding by looking up a table and cannot be used for encryption, even if a custom encoding table is used.

BASE64 is suitable for encoding small pieces of content, such as digital certificate signatures, cookie content, and so on.

Since the = characters may also appear in the Base64 encoding, but = used in the URL, the cookie will cause ambiguity, so a lot of Base64 code will be = removed:

# Standard BASE64: ' ABCD ' ' ywjjza== ' # Auto Remove =:'abcd'ywjjza '

=How do you decode it after you remove it? Because Base64 is to change 3 bytes to 4 bytes, the length of the BASE64 encoding is always a multiple of 4, so you need to add the length of the = Base64 string to a multiple of 4 to decode it normally.

Summary

Base64 is an arbitrary binary-to-text string encoding method that is commonly used to transmit small amounts of binary data in URLs, cookies, and Web pages.

43 common built-in modules base64

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.