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
chendajun
V2EX  ›  Python

用 pycrypto 解密报错, python2.7 中可以正常解密,但在 python3.3 和 python3.5 中报错。

  •  
  •   chendajun · 2016-07-04 10:49:46 +08:00 · 3448 次点击
    这是一个创建于 2867 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import binascii
    from Crypto.Cipher.DES3 import DES3Cipher
    from Crypto.Cipher import blockalgo


    def _decode_ossauth(access_id, access_key, secret_key):
    bytedes_key = ""
    for b in secret_key:
    bytedes_key += "%c" % b
    print((type(bytedes_key), len(bytedes_key)))
    byte_id = binascii.a2b_hex(access_id)
    byte_key = binascii.a2b_hex(access_key)
    cipher = DES3Cipher(key=bytedes_key, mode=blockalgo.MODE_ECB)#这一行报错
    plain_id = cipher.decrypt(byte_id)
    plain_id = plain_id[0:len(plain_id) - int(binascii.b2a_hex(plain_id[-1]))]
    plain_key = cipher.decrypt(byte_key)
    plain_key = plain_key[0:len(plain_key) - int(binascii.b2a_hex(plain_key[-1]))]
    return (plain_id, plain_key)


    if __name__ == '__main__':
    SECRET_KEY = (
    0x12, 0x22, 0x4F, 0x58, 0x88, 0x10, 0x40, 0x38,
    0x28, 0x25, 0x79, 0x51, 0xCB, 0xDD, 0x55, 0x66,
    0x77, 0x29, 0x74, 0x98, 0x30, 0x40, 0x36, 0xE2
    )
    access_id = '7159603AA8DAA73353C6C29F6B0BDC42A3BC6F34C78D2BFC'
    access_key = '3DC1BBB7AAE5D8D0469C76899B2B3274902937AE833089D2F9F9D51AA0F7447A'
    result = _decode_ossauth(access_id, access_key, SECRET_KEY)
    print(result)

    报错信息:
    ValueError: Invalid key size (must be either 16 or 24 bytes long)
    6 条回复    2016-07-04 12:26:19 +08:00
    lonelinsky
        1
    lonelinsky  
       2016-07-04 11:02:43 +08:00
    贴 python 代码还是先把代码格式化下,另外把报错信息贴全一点吧 =。=
    Crossin
        2
    Crossin  
       2016-07-04 11:19:30 +08:00
    猜测: python3 里的 str 其实是 unicode ,然后你的 bytedes_key 最终转成 bytes 的时候被按照 utf-8 编码了,结果导致长度不再是 24 。

    >>> len(bytes(bytedes_key.encode('latin'),'utf-8'))
    29

    可试下
    bytedes_key = bytedes_key.encode('latin')
    然后再解密

    ( python3 没装 Crypto ,所以没有亲自验证)
    Crossin
        3
    Crossin  
       2016-07-04 11:20:31 +08:00
    中间笔误:
    >>> len(bytes(bytedes_key,'utf-8'))
    29
    chendajun
        4
    chendajun  
    OP
       2016-07-04 11:22:18 +08:00
    @Crossin 对,我也试了 bytedes_key 转成 bytes 后长度是 29 ,谢谢大兄弟!
    lonelinsky
        5
    lonelinsky  
       2016-07-04 11:32:51 +08:00
    @Crossin 同,我也觉得是编码的问题,不过我觉得直接用 bytes 来得更直接点…
    @chendajun 改成下面的就可以运行了,你可以试下

    1 import binascii
    2 from Crypto.Cipher.DES3 import DES3Cipher
    3 from Crypto.Cipher import blockalgo
    4
    5
    6 def _decode_ossauth(access_id, access_key, secret_key):
    7 #bytedes_key = ""
    8 #for b in secret_key:
    9 #bytedes_key += "%c" % b
    10 bytedes_key = bytes(secret_key)
    11 print((type(bytedes_key), len(bytedes_key)))
    12 byte_id = binascii.a2b_hex(access_id)
    13 byte_key = binascii.a2b_hex(access_key)
    14 cipher = DES3Cipher(key=bytedes_key, mode=blockalgo.MODE_ECB)
    15 plain_id = cipher.decrypt(byte_id)
    16 print(type(plain_id))
    17 plain_id = plain_id[0:len(plain_id) - int(plain_id[-1])].decode('utf-8')
    18 plain_key = cipher.decrypt(byte_key)
    19 plain_key = plain_key[0:len(plain_key) - int(plain_key[-1])].decode('utf-8')
    20 return (plain_id, plain_key)
    21
    22
    23 if __name__ == '__main__':
    24 SECRET_KEY = (
    25 0x12, 0x22, 0x4F, 0x58, 0x88, 0x10, 0x40, 0x38,
    26 0x28, 0x25, 0x79, 0x51, 0xCB, 0xDD, 0x55, 0x66,
    27 0x77, 0x29, 0x74, 0x98, 0x30, 0x40, 0x36, 0xE2
    28 )
    29 access_id = '7159603AA8DAA73353C6C29F6B0BDC42A3BC6F34C78D2BFC'
    30 access_key = '3DC1BBB7AAE5D8D0469C76899B2B3274902937AE833089D2F9F9D51AA0F7447A'
    31 result = _decode_ossauth(access_id, access_key, SECRET_KEY)
    32 print(result)
    chendajun
        6
    chendajun  
    OP
       2016-07-04 12:26:19 +08:00
    @Crossin @lonelinsky 谢谢两位大牛的帮助,已经可以了。祝好!!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1016 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:12 · PVG 04:12 · LAX 13:12 · JFK 16:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.