Python基于pycrypto实现的AES加密和解密算法示例-创新互联
本文实例讲述了Python基于pycrypto实现的AES加密和解密算法。分享给大家供大家参考,具体如下:
创新互联公司从2013年成立,是专业互联网技术服务公司,拥有项目成都网站制作、做网站、外贸营销网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元邵武做网站,已为上家服务,为邵武各地企业和个人服务,联系电话:028-86922220一 代码
# -*- 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__ == '__main__': text = 'Python3.5 is excellent.' key = keyGenerater(16) #随机选择AES的模式 mode = random.choice((AES.MODE_CBC, AES.MODE_CFB, AES.MODE_ECB, AES.MODE_OFB)) if not key: print('Something is wrong.') else: print('key:', key) print('mode:', mode) print('Before encryption:', text) #明文必须以字节串形式,且长度为16的倍数 text_encoded = text.encode() text_length = len(text_encoded) padding_length = 16 - text_length%16 text_encoded = text_encoded + b'0'*padding_length text_encrypted = AESencrypt(key, mode, text_encoded) print('After encryption:', text_encrypted) text_decrypted =AESdecrypt(key, mode, text_encrypted) print('After decryption:', text_decrypted.decode()[:-padding_length])
本文名称:Python基于pycrypto实现的AES加密和解密算法示例-创新互联
文章来源:http://ybzwz.com/article/hsdop.html