V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ydhcui
V2EX  ›  Python

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

  •  
  •   ydhcui · 2015-06-03 18:15:58 +08:00 · 2887 次点击
    这是一个创建于 3493 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码在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('=', '_')
    )
    
    6 条回复    2015-06-05 19:18:29 +08:00
    ydhcui
        1
    ydhcui  
    OP
       2015-06-03 20:56:19 +08:00
    czheo
        2
    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
        3
    ydhcui  
    OP
       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
        4
    fzinfz  
       2015-06-05 19:10:13 +08:00   ❤️ 1
    看错误貌似是因为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
        5
    ydhcui  
    OP
       2015-06-05 19:17:09 +08:00
    @fzinfz ... 感谢回复
    问题已经解决了 虽然没有从根本上解决。
    ydhcui
        6
    ydhcui  
    OP
       2015-06-05 19:18:29 +08:00
    我把
    fills = fills + chr(randint(0,0xff))
    v = (chr((filln - 2)|0xF8)
    这两个会产生str的地方直接写死成bytes类型了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5535 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 07:43 · PVG 15:43 · LAX 23:43 · JFK 02:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.