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

该代码段展示了如何使用Python的`Crypto`库进行DES3解密,以处理阿里云OSS的访问ID和访问密钥。通过将密钥和ID转换为bytes,然后使用DES3Cipher进行解密,最后去除填充位得到原始字符串。
摘要由CSDN通过智能技术生成

5

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值