本文实例讲述了Python基于pycrypto实现的AES加密和解密算法。分享给大家供大家参考,具体如下:
一 代码
# -*- coding: UTF-8 -*-
import string
import random
from Crypto.Cipher import AES
def keyGenerater(length):
'''''生成指定长度的秘钥'''
if length not in (16, 24, 32):
return None
x = string.ascii_letters+string.digits
return ''.join([random.choice(x) for i in range(length)])
def encryptor_decryptor(key, mode):
return AES.new(key, mode, b'0000000000000000')
#使用指定密钥和模式对给定信息进行加密
def AESencrypt(key, mode, text):
encryptor = encryptor_decryptor(key, mode)
return encryptor.encrypt(text)
#使用指定密钥和模式对给定信息进行解密
def AESdecrypt(key, mode, text):
decryptor = encryptor_decryptor(key, mode)
return decryptor.decrypt(text)
if __name__ == '