请教一个 py2 和 py3 的转换问题 。

2015-06-03 18:15:58 +08:00
 ydhcui

代码在python2.7下面跑得很正常 但是到了python3.3 下面就不行了

#coding=utf8

import base64
import struct
from random import seed
from random import randint
from hashlib import md5
import codecs

seed(1)
op = 0xffffffff
delta = 0x9e3779b9
value = b'\x00\x80\xe8\xdec\xd5{\xe0\x1b6\x8f\x97\xbf\xc2F\xb4\x0b\xe1\xa5\xc7\x84h33M\xc2\x18\xc7Y\xb2B\\\xa3\xf8\x9dFH^-p\x07\x13\xa3\xd1\xe8\xb5\xe2\xe8\x82\xb9\xe1\xab\x15\xf1I\xcf\xd6\x98\xb7\x02\xe4\x82\xc8\xb2N\x95\xd4\x03b\x1dF@\\\xf5\xaez/\xc0-\xcaOA\x84\x19\x93l\xf0"\x0b\'\x8f\x1f\xf7\xc9\x830\xa1\xc9\x98\x9d\xad\xd8\x07\x0b\xcdS\x9c\x13[\xddxK\x91\xa4J\xb5\x9b`0%\xccd\xd4\x98\xed\xa4xc)\t\x00\x00\x00\x00D\x87\xe9\xe9\x00\x04WUWV'
key = b")7\xf7:!\xb7\xdetp0'\xf5\xd8\xfaK\x82"


def xor(a, b):
    a1,a2 = struct.unpack('>LL', a[0:8])
    b1,b2 = struct.unpack('>LL', b[0:8])
    r = struct.pack('>LL', ( a1 ^ b1) & op, ( a2 ^ b2) & op)
    return r

def code(v, k):
    n=16
    k = struct.unpack('>LLLL', k[0:16])
    y, z = struct.unpack('>LL', v[0:8])
    s = 0
    for i in range(n):
        s += delta
        y += (op &(z<<4))+ k[0] ^ z+ s ^ (op&(z>>5)) + k[1]
        y &= op
        z += (op &(y<<4))+ k[2] ^ y+ s ^ (op&(y>>5)) + k[3]
        z &= op
    r = struct.pack('>LL',y,z)
    return r

def encrypt(v, k):
    END_CHAR = b'\0'
    fills = b''
    filln = ((8-(len(v)+2))%8) + 2
    for i in range(filln):
        fills = fills + chr(randint(0,0xff))
    v = (chr((filln - 2)|0xF8)
        + fills
        + v
        + END_CHAR *7)
    tr = to = o = END_CHAR * 8
    r = b''
    for i in range(0, len(v), 8):
        g = v[i:i+8]
        o = xor(g,tr)
        tr = xor(code(o,k),to)
        to = o
        r += tr
    return r

print(
 base64.b64encode(encrypt(value,key)).replace('/', '-').replace('+', '*').replace('=', '_')
)
2860 次点击
所在节点    Python
6 条回复
ydhcui
2015-06-03 20:56:19 +08:00
czheo
2015-06-04 01:19:20 +08:00
你至少也贴一个stack trace log。才有人看
chr返回一个string,无法和bytes想加
http://stackoverflow.com/questions/25042212/typeerror-cant-concat-bytes-to-str-trying-to-use-python3
ydhcui
2015-06-04 09:15:39 +08:00
@czheo
然后我把
'''python
fills = fills + chr(randint(0,0xff))
v = (chr((filln - 2)|0xF8)
换成
fills = fills + chr(randint(0,0xff)).encode()
v = (chr((filln - 2)|0xF8).encode()
提示错误
Traceback (most recent call last):
File "DASDA.PY", line 58, in <module>
base64.b64encode(encrypt(value,key)).replace('/', '-').replace('+', '*').rep
lace('=', '_')
File "DASDA.PY", line 51, in encrypt
o = xor(g,tr)
File "DASDA.PY", line 18, in xor
a1,a2 = struct.unpack('>LL', a[0:8])
struct.error: unpack requires a bytes object of length 8
fzinfz
2015-06-05 19:10:13 +08:00
看错误貌似是因为a[0:7] 的长度不是8。

加几个print调试:

def xor(a, b):
print(a)
print(len(a))
print(a[0:7])
print(len(a[0:7]))


output:

b'\xc3\xbeD \xc2\x82<\xc3'
8
b'\xc3\xbeD \xc2\x82<'
7
ydhcui
2015-06-05 19:17:09 +08:00
@fzinfz ... 感谢回复
问题已经解决了 虽然没有从根本上解决。
ydhcui
2015-06-05 19:18:29 +08:00
我把
fills = fills + chr(randint(0,0xff))
v = (chr((filln - 2)|0xF8)
这两个会产生str的地方直接写死成bytes类型了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/195875

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX