#Python3使用Crypto加密,报错解决

Python3使用Crypto加密,报错解决

pip install crypto 安装报错如下:

ERROR: Command "'e:\python\python3.6\python.exe' -u -c 'import setuptools, tokenize;__file__='"'"'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\pip-install-o39b_k37\\pycrypto\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\ADMINI~1\AppData\Local\Temp\pip-record-j7duqjgy\install-record.txt' --single-version-externally-managed --compile" failed with error code 1 in C:\Users\ADMINI~1\AppData\Local\Temp\pip-install-o39b_k37\pycrypto\

原因;PyCrypto 已死,请替换为 PyCryptodomex
解决:
pip3 install pycryptodomex

用法如下:

from Cryptodome.Cipher import AES

#加密:
obj = AES.new(("This is a key123").encode(), AES.MODE_CBC, ('This is an IV456').encode())
message = "The answer is no"
ciphertext = obj.encrypt(message.encode())
print(ciphertext)
#(输出)
'\xd6\x83\x8dd!VT\x92\xaa`A\x05\xe0\x9b\x8b\xf1'

#解密:
obj2 = AES.new(('This is a key123').encode(), AES.MODE_CBC, ('This is an IV456').encode())
print(obj2.decrypt(ciphertext))
#(输出)
'The answer is no'

一般设置key 和 IV报错:
ValueError: Incorrect AES key length (23 bytes)
It must be 16, 24 or 32 bytes long (必须为16,24或者32自己长度,即上文中(‘This is a key123’))

ValueError: Incorrect IV length (it must be 16 bytes long) :
IV的长度必须为16个字节,上文中(‘This is an IV456’)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值