python AES加密解密Crypto模块代码

发布时间:2020-09-16编辑:脚本学堂
有关python AES加密解密模块Crypto模块的用法,python中使用Crypto模块进行代码加密与解密,需要的朋友参考下。
专题教程:
AES加密解密算法与实现代码
python aes加密解密算法与模块用法教程

例1,python AES加密解密代码(Crypto.模块)。
 

复制代码 代码示例:

#!/usr/bin/env python
#
from Crypto.Cipher import AES

key = 'F74E66709E1C4647323DBEA92412411E'
obj = AES.new(key, AES.MODE_ECB)
msg = obj.encrypt('F74E66709E1C4647323DBEA92412411EF74E66709E1C4647323DBEA92412411E')
print repr(msg)
msg1 = obj.dencrypt(msg)

例2,An example usage of an encryption algorithm (AES, in this case) is:
 

>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> message = "The answer is no"
>>> ciphertext = obj.encrypt(message)
>>> ciphertext
'xd6x83x8dd!VTx92xaa`Ax05xe0x9bx8bxf1'
>>> obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> obj2.decrypt(ciphertext)
'The answer is no'

例3,python ASE加密代码。
 

复制代码 代码示例:
#encoding:utf-8
"""
示例代码
"""
from Crypto.Cipher import AES
    key = '0123456789abcdef'
    mode = AES.MODE_CBC
    encryptor = AES.new(key, mode)
    text = 'j' * 64 + 'i' * 128
    ciphertext = encryptor.encrypt(text)
 
"""
上例中的key是16位, 还可以是24 或 32 位长度, 其对应为 AES-128, AES-196 和 AES-256.
解密则可以用以下代码进行:
"""
 
#decryptor = AES.new(key, mode)
#plain = decryptor.decrypt(ciphertext)