Python's commonly used modules (base64) go

Source: Internet
Author: User
Tags base64



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



exejpgWhen we open these files with Notepad,pdfwe 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 a3x8=24bit, 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-inbase64codec that can be base64 directly:


>>> import base64>>> base64.b64encode(‘binary\x00string‘)‘YmluYXJ5AHN0cmluZw==‘>>> base64.b64decode(‘YmluYXJ5AHN0cmluZw==‘)‘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/distinction into-and_:


>>> base64.b64encode(‘i\xb7\x1d\xfb\xef\xff‘)‘abcd++//‘>>> base64.urlsafe_b64encode(‘i\xb7\x1d\xfb\xef\xff‘)‘abcd--__‘>>> base64.urlsafe_b64decode(‘abcd--__‘)‘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:


#标准Base64:‘abcd‘ -> ‘YWJjZA==‘# Automatically 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.



Please write a=base64 decoding function that can handle the removed:


>>> base64.b64decode(‘YWJjZA==‘)‘abcd‘>>> base64.b64decode(‘YWJjZA‘)Traceback (most recent call last): ...TypeError: Incorrect padding>>> safe_b64decode(‘YWJjZA‘)‘abcd‘
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.



Transfer from Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001399413803339f4bbda5c01fc479cbea98b1387390748000



Python's commonly used modules (base64) go


Related Article

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.