Python Learning note __12.3 base64

Source: Internet
Author: User
Tags assert base64

# This is a learning note for the Liaoche teacher Python tutorial


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

Base64 is the most common binary encoding method used as a binary-to-string conversion

1, the principle of Base64

1) prepare an array containing 64 characters:

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

2) The binary data processing, every 3 bytes a group, altogether is 3x8=24bit, is divided into 4 groups, each group exactly 6 bit:

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

Base64 encoding encodes 3 bytes of binary data into 4-byte text data after, though the length increased 33% , but the advantage is that the encoded text data can be displayed directly in the message body, Web page, and so on.

If the binary data you want to encode is not 1 or 2 bytes. b ase64 \x00 After the end, add to the end of the code. 1 or 2 a =

2 , B ase64 Application

1) Python built -in Base64 base64 can be directly encoded and decoded

>>> Import Base64

>>> base64.b64 encode (b ' binary\x00string ') # code

B ' ymluyxj5ahn0cmluzw== '

>>> base64.b64 Decode (b ' ymluyxj5ahn0cmluzw== ') # decoding

B ' binary\x00string '

2) Base64 encoding for "url safe"

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

>>> 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 '

3 , Summary

1) You can define the order of 64 characters, so that you can customize the Base64 encoding, however, it is not always necessary.

2)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 .

3)Base64 is suitable for the coding of small pieces of content, such as digital certificate signature, cookie content and so on.

4) Because the = character may also appear in the Base64 encoding, but = used in the URL, the cookie will cause ambiguity, so a lot of Base64 encoding will be removed. The decoding is automatically added

# Standard BASE64:

' ABCD ', ' ywjjza== '

# Automatically remove =:

' ABCD ', ' Ywjjza '

    1. Base64 is an arbitrary binary to text string encoding method , often used in URL , Cookies , a small amount of binary data is transmitted in the Web page.

4 , examples

1 , please write a can handle remove = the Base64 decoding function:

#-*-Coding:utf-8-*-
Import Base64

def safe_base64_decode (s):

s + = b ' = = = ' # Add three to the data =

return Base64.b64decode (s[:-1* (Len (s)%4)) # s[:-1* (Len (s)%4)] The last few elements are not displayed

Test
assert b ' abcd ' = = Safe_base64_decode (b ' ywjjza== '), Safe_base64_decode (' ywjjza== ')
Assert b ' abcd ' = = Safe_base64_decode (b ' Ywjjza '), Safe_base64_decode (' Ywjjza ')
Print (' OK ')


Python Learning note __12.3 base64

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.