德code蟒蛇的base64字符串 [英] Decode python base64 String

查看:170
本文介绍了德code蟒蛇的base64字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提取的base64字符串,我有以下结构的列表:

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

我试图去code这些值,但是我得到不正确的填充错误,这里是确切的错误:

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

下面是我的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')

我还看到一个有趣的把出来时先前景色的base64字符串去了codeD:
这里是放出来的那:

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

我真的不知道我在做什么错在这里。我看了看下面的Python文件,并尝试过,但没有工作,要么:
http://docs.python.org/library/base64.html

推荐答案

您正试图去code Base64的字符串,没有填充。虽然Base64编码的许多味道不具有填充,Python的需要标准Base64编码解码填充。这个计算器问题有一个更深入的解释:<一href=\"http://stackoverflow.com/questions/2941995/python-ignore-incorrect-padding-error-when-base64-decoding\">Python:忽略填充不正确的错误时的base64解码

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

有关您的code我会做修改,类似如下:

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

根据您的评论似乎你还需要字节数组从解码的base64返回化作整数列表。我做了一个假设,即整数是小端短整数。

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

结果是:

[(2,), (300,), (525,), (804,), (3356,), (3608,), (3866,), (7427,), (7686,), (13831,), (15617,), (782,), (16723,), (-32749,), (16859,), (-32613,), (16543,)]

这篇关于德code蟒蛇的base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆