1
lonelinsky 2016-07-04 11:02:43 +08:00
贴 python 代码还是先把代码格式化下,另外把报错信息贴全一点吧 =。=
|
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 ,所以没有亲自验证) |
3
Crossin 2016-07-04 11:20:31 +08:00
中间笔误:
>>> len(bytes(bytedes_key,'utf-8')) 29 |
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) |
6
chendajun OP @Crossin @lonelinsky 谢谢两位大牛的帮助,已经可以了。祝好!!!
|