1

I have to decode a string. This one comes from Flash AS3 and I want to decode it in Python. I don't have any problems with PHP, but I cannot decode the following string with Python 2.6 'base64.b64decode'.

f3hvQgQaBFp9IC4NQhYZQiAhNhxBAkwIJC0pDR8fBl12ZjkWXwMEWn57bU0dGgBfcWdsTwAbGB4xLmVLAh0FXXd5a0gGHQRWdy5iQANNVAl/KmNLAhUBXyV8PkFQHwNefntjGgpPU18nK21OURtSC35wPE4FHFUJdi4/TlMUVFwlez9JVxtVDH0TB0IGHAc%Pr

Python returns "TypeError: Incorrect Padding". It seems to have superfious characters at the end of the string (from the '%'). But why Python base64 library do not manage this?

Thank you for your answer.

Acti67
  • 577
  • 4
  • 11
  • Python can decode it if you remove the ending 'r', but I don't know if the decoded string makes sense to you. – dusan Feb 12 '13 at 16:37
  • actually a question about php as much as python – Vorsprung Feb 12 '13 at 16:41
  • Decoded string is a crypted string :) I need to decode the base64 in order to decrypt the results after. Is there a function to clean base64 string before send it to Python base64 decoder? – Acti67 Feb 12 '13 at 16:43
  • Could it be there should be an `=` sign right at the end (that is the usual padding character)?... in principle the character count should be divisible by 4. If it is not, you may be in luck by adding `=` until it is... – Floris Feb 12 '13 at 16:49
  • 1
    I can replace "%Pr" by "=" ; but what is this "%Pr" sent by HTTP POST request to Python from the AS3? – Acti67 Feb 12 '13 at 17:01
  • Try encoding an un-encoded version of the string with Python and see if that matches what you're trying to feed to `b64decode()`. Also, perhaps the PHP base64 decoder just stops without an error when it sees a non-alphabet character. The Python docs for `b32decode()` say a TypeError is raised if the string is incorrectly padded or if there are any non-alphabet characters present in it. – martineau Feb 12 '13 at 18:01
  • Thank you, I have found the following post about the "%Pr" (it is hard to find information about this bug): http://stackoverflow.com/questions/4903088/problem-ajaxing-json-object-on-mac-firefox-version-3-6-12 And more about: https://bugzilla.mozilla.org/show_bug.cgi?id=489575 So, I don't know if I need to modify something or let an error message for these users. – Acti67 Feb 12 '13 at 18:17
  • There is another interesting discussion at http://stackoverflow.com/questions/2941995/python-ignore-incorrect-padding-error-when-base64-decoding - lots of debug suggestions, and with a "solution" to use `openssl` which seems to be robust to padding errors. – Floris Feb 12 '13 at 21:01

1 Answers1

0

It seems to me you are not feeding a valid string to the function - it tells you so. You can't expect a function to "guess" what you wanted, and base its response on that. You have to use a valid parameter, or the function doesn't work.

Floris
  • 45,857
  • 6
  • 70
  • 122