close

[Solved] Python: Ignore ‘Incorrect padding’ error when base64 decoding

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python: Ignore ‘Incorrect padding’ error when base64 decoding in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python: Ignore ‘Incorrect padding’ error when base64 decoding Error Occurs?

Today I get the following error Python: Ignore ‘Incorrect padding’ error when base64 decoding in python.

How To Solve Python: Ignore ‘Incorrect padding’ error when base64 decoding Error ?

  1. How To Solve Python: Ignore 'Incorrect padding' error when base64 decoding Error ?

    To Solve Python: Ignore 'Incorrect padding' error when base64 decoding Error You can simply use base64.urlsafe_b64decode(data) if you are trying to decode a web image. It will automatically take care of the padding.

  2. Python: Ignore 'Incorrect padding' error when base64 decoding

    To Solve Python: Ignore 'Incorrect padding' error when base64 decoding Error You can simply use base64.urlsafe_b64decode(data) if you are trying to decode a web image. It will automatically take care of the padding.

Solution 1

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: b'abc=' works just as well as b'abc==' (as does b'abc=====').

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

Solution 2

You can simply use base64.urlsafe_b64decode(data) if you are trying to decode a web image. It will automatically take care of the padding.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment