Python uses the base64 module to process base64 encoding.

Source: Internet
Author: User

Python uses the base64 module to process base64 encoding.

The base64 module is used for base64 encoding and decoding. This encoding method is very common in email.
It can encode binary data that cannot be displayed as text information to be displayed. The encoded text size increases by 1/3.

The base64 module only uses eight methods: encode, decode, encodestring, decodestring, b64encode, b64decode, urlsafe_b64decode, and urlsafe_b64encode. The eight of them can be divided into four groups: encode and decode, which are used to encode and decode files. They can also perform encoding and decoding on the data in StringIO; encodestring and decodestring, this function is used to encode and decode strings. b64encode and b64decode are used to encode and decode strings and replace symbol characters. This function is like this: the base64 encoded characters include three letters +/=, here, the value = is only used to encode the number of characters with four integers, while the value + and/must be replaced in some cases. b64encode and b64decode provide this function. When + and/need to be replaced, the most common is the base64 encoding of the url. Urlsafe_b64encode and urlsafe_b64decode are used to encode and encode the url in base64. in fact, they are also called by the previous functions.

Basic usage
First, prepare A 64-character array ['A', 'B', 'C ',... , '1', '2 ',... , '+']
Then, the binary data is processed. Each three bytes (byte) is a group of 3x8 = 24, which is divided into 4 groups, each with 6 bits. in this way, we get four numbers as indexes, and then look up the table to obtain the corresponding 4 characters, which are encoded strings.
What if the number of encoded binary values is not a multiple of 3? After base64 is insufficient at \ x00, add 1 ~ to the end of the Code ~ 2 = signs, decoding is automatically removed

import base64me = base64.b64encode("mink")print me            # print 'bWluaw=='

The = sign is used to add fewer characters.

print base64.b64decode(me)   # print 'mink'

Decodes the original string.

Urlsafe_b64encode and urlsafe_b64decode

Base64 also provides url encoding methods urlsafe_b64encode and urlsafe_b64decode.

me = base64.urlsafe_b64encode('i\xb7\x1d\xfb\xef\xff')print me            # print 'abcd--__'print base64.urlsafe_b64decode(me) print 'abcd++//'

Because base64 encoded + and-may appear in characters, you can use urlsafe to convert + to-and/_.

Because the = character may also appear in Base64 encoding, but the = character will cause ambiguity in the URL Cookie, many Base64 encoding will remove =. Remove = and then how to decode it? Because Base64 converts three bytes into four bytes, the length of Base64 encoding is always a multiple of four. therefore, you need to add = to change the length of the Base64 string to a multiple of 4, and then you can properly decode it.

Comprehensive example

#-*-Encoding: gb2312-*-import base64import StringIOa = "this is a test" B = base64.encodestring (a) # print bprint base64.decodestring (B) # decodes string c = StringIO. stringIO () c. write (a) d = StringIO. stringIO () e = StringIO. stringIO () c. seek (0) base64.encode (c, d) # encode the data in StringIO print d. getvalue () d. seek (0) base64.decode (d, e) # decode the data in StringIO print e. getvalue () a = "this is a + test" B = base64.urlsafe _ b64encode (a) # encode the url string print bprint base64.urlsafe _ b64decode (B)


The above encode function and decode function parameters can also be file objects, which is like this:

f1 = open('aaa.txt', 'r')f2 = open('bbb.txt', 'w')base64.encode(f1, f2)f1.close()f2.close()

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.