10

I have extracted base64 string of forecolor, texture and edgemap values of images, I have a list with following structure:

forecolor=AgCEAg4DUQQCBQQGARMBFQE1AmUB
edge=AfCAFg5iIATCPwTAEIiBFggBDw
forecolor=AgAsAQ0CJAMcDRgOGg8DHQYeBzYBPQ4-DU0ETgNtBm4CfQI

I am trying to decode these values, but I am getting Incorrect Padding error, here is the exact error:

Traceback (most recent call last):
  File "ImageVectorData.py", line 44, in <module>
    print "Decoded String: " + decoded.decode('base64', 'strict')
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/base64_codec.py", line 42, in base64_decode
    output = base64.decodestring(input)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 321, in decodestring
    return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

Here is my code:

for item in value:
    print "String before Split: " + item
    if item.split("=")[0] == "forecolor":
        decoded = (item.split("=")[1])
        print "String to be decoded: " + decoded
        print "Decoded String: " + decoded.decode('base64', 'strict')

I also saw an interesting out put when the first forecolor base64 string got decoded: Here is the out put of that:

String before Split: forecolor=AgCEAg4DUQQCBQQGARMBFQE1AmUB
String to be decoded: AgCEAg4DUQQCBQQGARMBFQE1AmUB
Decoded String: ?Q5e

I am not really sure what I am doing wrong here. I looked at following python document and tried that but that didn't work either: http://docs.python.org/library/base64.html

4
  • The value for edge does not seem to be a vlaid base64-encoded string, so Python gives an error message. Use valid inputs, and things should work. Commented Jul 25, 2012 at 18:24
  • If you look at the code I am only trying to decode forecolor so I don't think edge does come into action at this point. Commented Jul 25, 2012 at 18:30
  • @SvenMarnach, you can fix the padding issue by adding a couple of == to the end of the string. The bigger issue is that the decoded result is totally undecipherable. Commented Jul 25, 2012 at 18:30
  • 1
    As others have pointed out- the issue is with your input data. Little we can do to help with that... Junk in, junk out. Commented Jul 25, 2012 at 19:11

1 Answer 1

8

You are trying to decode a Base64 String which does not have padding. Although many flavors of Base64 do not have padding, Python requires padding for standard base64 decoding. This StackOverflow question has a more in-depth explanation: Python: Ignore 'Incorrect padding' error when base64 decoding

For your code I would make modifications similar to below:

for item in value:
    print "String before Split: " + item
    if item.split("=")[0] == "forecolor":
        decoded = (item.split("=")[1])
        print "String to be decoded: " + decoded
        # Add Padding if needed
        decoded += "===" # Add extra padding if needed
        print "Decoded String: " + decoded.decode('base64', 'strict')

Based on your comment it seemed that you also need the byte array returned from the base64 decoding turned into a list of integers. I made an assumption that the integers are little endian short ints.

import struct
x = "AgAsAQ0CJAMcDRgOGg8DHQYeBzYBPQ4-DU0ETgNtBm4CfQI"
x += "==="
y = x.decode('base64', 'strict')
intList = [struct.unpack('<h', y[i] + y[i+1]) for i in xrange(0, len(y), 2)]
print intList

The result was:

[(2,), (300,), (525,), (804,), (3356,), (3608,), (3866,), (7427,), (7686,), (13831,), (15617,), (782,), (16723,), (-32749,), (16859,), (-32613,), (16543,)]
Sign up to request clarification or add additional context in comments.

4 Comments

I still get the same error, I used the exact line you had put.
I found another technique in another answer that works for your data. I have updated the answer. It is simply adding enough padding to account for all scenarios.
Thanks but the problem is decoded value should be a vector like 2,3,4,5 instead its giving me string like d#CSs padding issue is resolve the data is completely wrong. These encoded values are generated by a C++ program do you that has something to do with this?
I have appended my answer to convert from the byte array to a list of ints. If the base64 piece is working, I would recommend closing this question and opening another with the byte array issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.