Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binascii.Error: Incorrect padding, even when string length is multiple of 4

I am trying to convert base64 string to image by python code, but I am getting binascii.Error: Incorrect padding I have gone through with my solution but they only suggest check string length is divisible 4, if not make it divisible by 4 by adding '=' characters at the end of base64 encoded sting. Please help in this.

PYTHON CODE: (please check code from drive for more visibility)

import base64

strOne= 'data:image/png;base64,iVBORw0KGgoAAAANSU...string has 200000 character thats why I couldn t paste'
 print 'strOne Length',len(strOne)
 print 'StrOne Length is completely divisible by 4 (len%4),(len/4):', len(strOne)%4,len(strOne)/4

 with open("imageToSave.png", "wb") as fh:
     fh.write(strOne.strip().decode('base64'))

output:

strOne Length 200000
StrOne Length is completely divisible by 4 (len%4),(len/4): 0 50000
Traceback (most recent call last):
  File "/tests.py", line 13, in <module>
    fh.write(strOne.strip().decode('base64'))
  File "/usr/lib/python2.7/encodings/base64_codec.py", line 42, in base64_decode
    output = base64.decodestring(input)
  File "/usr/lib/python2.7/base64.py", line 328, in decodestring
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
like image 439
Vikram Singh Chandel Avatar asked Aug 25 '17 10:08

Vikram Singh Chandel


People also ask

What is binascii error?

'binascii. Error: Incorrect Padding'. I understand that this is because the strings are not of length multiple of 4, a requirement of base64 encoded text. Rather than just adding '='s to the end of the string to make it a multiple of 4, I want to catch the error and simply state that the string is not base64 encoded.

What does incorrect padding mean in Python?

If there's a padding error it probably means your string is corrupted; base64-encoded strings should have a multiple of four length. You can try adding the padding character ( = ) yourself to make the string a multiple of four, but it should already have that unless something is wrong.06-Jun-2022.

What is incorrect padding?

Incorrect padding error is caused because sometimes, metadata is also present in the encoded string If your string looks something like: 'data:image/png;base64,... base 64 stuff....' then you need to remove the first part before decoding it. Say if you have image base64 encoded string, then try below snippet..

Why does base64 have padding?

With padding, a base64 string always has a length that is a multiple of 4 (if it doesn't, the string has been corrupted for sure) and thus code can easily process that string in a loop that processes 4 characters at a time (always converting 4 input characters to three or less output bytes).


1 Answers

by checking your link, your string has 200000 bytes all right, but it contains the header:

strOne = b"data:image/png;base64,iVBORw0KGgoAAAANSU...

This is part of MIME message or something. You have to strip this first.

strOne = strOne.partition(",")[2]

then pad (if needed)

pad = len(strOne)%4
strOne += b"="*pad

then decode using codecs (python 3 compliant)

codecs.decode(strOne.strip(),'base64')

=> "we believe in team work" :)

like image 90
Jean-François Fabre Avatar answered Oct 14 '22 11:10

Jean-François Fabre