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

2016-07-04 10:49:46 +08:00
 chendajun
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)
3453 次点击
所在节点    Python
6 条回复
lonelinsky
2016-07-04 11:02:43 +08:00
贴 python 代码还是先把代码格式化下,另外把报错信息贴全一点吧 =。=
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
2016-07-04 11:20:31 +08:00
中间笔误:
>>> len(bytes(bytedes_key,'utf-8'))
29
chendajun
2016-07-04 11:22:18 +08:00
@Crossin 对,我也试了 bytedes_key 转成 bytes 后长度是 29 ,谢谢大兄弟!
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
2016-07-04 12:26:19 +08:00
@Crossin @lonelinsky 谢谢两位大牛的帮助,已经可以了。祝好!!!

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

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

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

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

© 2021 V2EX