#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’)

你可能感兴趣的:(Python)