Python3 base64 decoding appears typeerror:incorrect padding

Source: Internet
Author: User

Today, when dealing with the crawler's analysis of cryptographic parameters, it is necessary to use the Base64 decoding. However, a typeerror:incorrect padding error occurred during the process. Here are the workarounds for review.

In fact, the normal use of Base64 is not a problem, such as the following code.

1#!usr/bin/env python2# coding:utf-83 4 Import Base645 6A = b'Hello'7b =Base64.b64encode (a)8 # base64 encoding for a9Print (b) # b'agvsbg8='Ten # base64 decoding of B, that is, decoding a encoded content Onec =Base64.b64decode (b) APrint (c) # b'Hello'

The coding result of the above code is complete, so it is no problem to decode it directly. If the encoding result is incomplete, such as in the above code if the value of the given bytes object is B ' aGVsbG8 ', the Typeerror:incorrect padding exception prompt will appear. For example, the following code is an error.

# !usr/bin/env python # Coding:utf-8 import base64a = b'aGVsbG8'= Base64.b64decode (a)print  #binascii. Error:incorrect padding

Here's how to fix it:

1 #!usr/bin/env python2 #Coding:utf-83 4 ImportBase645 6A = b'aGVsbG8'7missing_padding = 4-len (a)% 48 ifmissing_padding:9A + = b'='*missing_paddingTenb =Base64.b64decode (a) One Print(b) # b ' Hello '

So the problem is solved, in fact, is to add equal sign at the back. And Missing_padding calculates the number of equals. If you calculate the = number, the direct plus = sign is also possible. For example, the following code:

1 #!usr/bin/env python2 #Coding:utf-83 4 ImportBase645 ImportChardet6 7A = b'aGVsbG8'8c = Base64.b64decode (A + b'=')9 Print(c)#b ' Hello '

As for how the calculation comes, it is necessary to understand the principle of base64. Using an equation means that 3x8 = 4x6, that is, 3 bytes can be saved, now can save 4 bytes, but the original bit bit is divided, and each byte is represented by 6 bits. Because each byte after the split is only 6 bits, the insufficient two bits are filled with zeros. And these 4 bytes can be considered as a whole, base64 decoded bytes length of at least 4 and a multiple of 4, the insufficient parts are filled with ' = '.

Are you listening to the fog? In fact, I expressed bad, and lazy to draw. Or look at the code:

1 #!usr/bin/env python2 #Coding:utf-83 4 ImportBase645 6 #Original 1x8 = 8 bits7A = b'h'8 #Base64 Encoded 8/6 = 1 + 2, so at least 2 byte bit, in order to meet can be divisible by 4, need to add two = number9b =Base64.b64encode (a)Ten Print(b)#b ' aa== ' One  A #process The encoded results and remove the ' = ' sign -c = B.decode ('Utf-8'). Rstrip ('=') - #decoding the results, the previous calculation requires 2 = number, directly plus is good theD = Base64.b64decode (C.encode ('Utf-8') + b'='* 2) - #Restore Results - Print(d)#b ' h '

This is a glance, the summary of the Base64 throws exception here.

Python3 base64 decoding appears typeerror:incorrect padding

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.