Python: Ignore Incorrect padding error when base64 decoding

Python: Ignore Incorrect padding error when base64 decoding

As said in other responses, there are various ways in which base64 data could be corrupted.

However, as Wikipedia says, removing the padding (the = characters at the end of base64 encoded data) is lossless:

From a theoretical point of view, the padding character is not needed,
since the number of missing bytes can be calculated from the number
of Base64 digits.

So if this is really the only thing wrong with your base64 data, the padding can just be added back. I came up with this to be able to parse data URLs in WeasyPrint, some of which were base64 without padding:

import base64
import re

def decode_base64(data, altchars=b+/):
    Decode base64, padding being optional.

    :param data: Base64 data as an ASCII byte string
    :returns: The decoded byte string.

    data = re.sub(rb[^a-zA-Z0-9%s]+ % altchars, b, data)  # normalize
    missing_padding = len(data) % 4
    if missing_padding:
        data += b=* (4 - missing_padding)
    return base64.b64decode(data, altchars)

Tests for this function: weasyprint/tests/

It seems you just need to add padding to your bytes before decoding. There are many other answers on this question, but I want to point out that (at least in Python 3.x) base64.b64decode will truncate any extra padding, provided there is enough in the first place.

So, something like: babc= works just as well as babc== (as does babc=====).

What this means is that you can just add the maximum number of padding characters that you would ever need—which is two (b==)—and base64 will truncate any unnecessary ones.

This lets you write:

base64.b64decode(s + b==)

which is simpler than:

base64.b64decode(s + b= * (-len(s) % 4))

Python: Ignore Incorrect padding error when base64 decoding

Just add padding as required. Heed Michaels warning, however.

b64_string += = * ((4 - len(b64_string) % 4) % 4) #ugh

Leave a Reply

Your email address will not be published. Required fields are marked *