专题教程:
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)