Python编码功能无法解码

时间:2010-07-01 19:10:23

标签: python encoding pickle decoding

我编写了这个python代码,试图将对象转换为1和0的字符串,但解码失败,因为数据不能被打开。这是代码:

def encode(obj):
    'convert an object to ones and zeros'
    def tobin(str):
        rstr = ''
        for f in str:
            if f == "0": rstr += "0000"
            elif f == "1": rstr += "0001"
            elif f == "2": rstr += "0010"
            elif f == "3": rstr += "0100"
            elif f == "4": rstr += "1000"
            elif f == "5": rstr += "1001"
            elif f == "6": rstr += "1010"
            elif f == "7": rstr += "1100"
            elif f == "8": rstr += "1101"
            elif f == "9": rstr += "1110"
            else: rstr += f
        return rstr
    import pickle, StringIO
    f = StringIO.StringIO()
    pickle.dump(obj, f)
    data = f.getvalue()
    import base64
    return tobin(base64.b16encode(base64.b16encode(data)))
def decode(data):
    def unbin(data):
        rstr = ''
        for f in data:
            if f == "0000": rstr += "0"
            elif f == "0001": rstr += "1"
            elif f == "0010": rstr += "2"
            elif f == "0100": rstr += "3"
            elif f == "1000": rstr += "4"
            elif f == "1001": rstr += "5"
            elif f == "1010": rstr += "6"
            elif f == "1100": rstr += "7"
            elif f == "1101": rstr += "8"
            elif f == "1110": rstr += "9"
        return rstr
    import base64
    ndata = base64.b16decode(base64.b16decode(unbin(data)))
    import pickle, StringIO
    f = StringIO.StringIO(ndata)
    obj = pickle.load(f)
    return obj

4 个答案:

答案 0 :(得分:2)

我认为有几个问题,但有一个问题是,当你解码时,你需要在你的unbin()函数中迭代4个字符的组,而不是你正在做的单个字符。

答案 1 :(得分:1)

我想我有更好的解决方案。这应该更安全,因为它“加密”所有内容,而不仅仅是数字:

MAGIC = 0x15 # CHOOSE ANY TWO HEX DIGITS YOU LIKE

# THANKS TO NAS BANOV FOR THE FOLLOWING:
unbin = tobin = lambda s: ''.join(chr(ord(c) ^ MAGIC) for c in s)

答案 2 :(得分:0)

您的binunbin函数不是彼此的反转,因为bin有一个else子句,只是将字符逐字地放入输出中,但是unbin没有其他条款可以将它们传回去。

答案 3 :(得分:0)

顺便说一句...... base64.b16encode(base64.b16encode(data))相当于data.encode('hex').encode('hex')。并且有更简单,更快速的方法来进行映射,

def tobin(numStr):
    return ''.join(("0000","0001","0010","0100","1000","1001","1010","1100","1101","1110")[int(c)] for c in numStr)

这种编码的整个想法虽然表面看起来很复杂,但并不是很好。首先,它没有做太多的加密,因为十六进制转储中的每个数字总是匹配相同的8长度0和1的字符串:

>>> hexd = '0123456789ABCDEF'
>>> s = hexd.encode('hex')
>>> s
'30313233343536373839414243444546'
>>> s=''.join(["0000","0001","0010","0100","1000","1001","1010","1100","1101","1110"][int(c)] for c in s)
>>> s
'01000000010000010100001001000100010010000100100101001010010011000100110101001110100000011000001010000100100010001000100110001010'
>>> for i in range(0,len(s),8):
...     print hexd[i/8], s[i:i+8], chr(int(s[i:i+8],2))
... 
0 01000000 @
1 01000001 A
2 01000010 B
3 01000100 D
4 01001000 H
5 01001001 I
6 01001010 J
7 01001100 L
8 01001101 M
9 01001110 N
A 10000001 
B 10000010 ‚
C 10000100 „
D 10001000 ˆ
E 10001001 ‰
F 10001010 Š

其次,它会炸掉腌制物体的大小 16次!即使你通过将每8位“0”和“1”转换为字节(比如chr(int(encoded[i:i+8],2)))来打包它,那仍然是 2x 泡菜。