Python基于pycrypto实现的AES加密和解密算法示例


Posted in Python onApril 10, 2018

本文实例讲述了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__ == '__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])

二 运行结果

E:\python\python可以这样学\第18章 密码学编程\code>python AES_test.py
('key:', 'D5pcO6iu0HIbj3I2')
('mode:', 1)
('Before encryption:', 'Python3.5 is excellent.')
('After encryption:', '\xf4\x15\x9f\xaf\xea\xd0\n\x03\xfdf\xf6}9\xaa\xa34\xb4\x1eL2\x0e \x16\xa5 \xff?\x8bA\x8e\xdd\xa8')
('After decryption:', u'Python3.5 is excellent.')

Python 相关文章推荐
python实现统计代码行数的方法
May 22 Python
在主机商的共享服务器上部署Django站点的方法
Jul 22 Python
利用python实现命令行有道词典的方法示例
Jan 31 Python
使用Python中的tkinter模块作图的方法
Feb 07 Python
python实现excel读写数据
Mar 02 Python
python实现剪切功能
Jan 23 Python
django 邮件发送模块smtp使用详解
Jul 22 Python
python数组循环处理方法
Aug 26 Python
Python TCP通信客户端服务端代码实例
Nov 21 Python
keras获得model中某一层的某一个Tensor的输出维度教程
Jan 24 Python
如何在 Django 模板中输出 "{{"
Jan 24 Python
Python+kivy BoxLayout布局示例代码详解
Dec 28 Python
浅谈Pandas中map, applymap and apply的区别
Apr 10 #Python
对pandas中apply函数的用法详解
Apr 10 #Python
Python 25行代码实现的RSA算法详解
Apr 10 #Python
使用pandas中的DataFrame数据绘制柱状图的方法
Apr 10 #Python
Python基于socket模块实现UDP通信功能示例
Apr 10 #Python
pandas把dataframe转成Series,改变列中值的类型方法
Apr 10 #Python
在pandas中一次性删除dataframe的多个列方法
Apr 10 #Python
You might like
phpmyadmin 3.4 空密码登录的实现方法
2010/05/29 PHP
CodeIgniter常用知识点小结
2016/05/26 PHP
Gambit vs CL BO3 第三场 2.13
2021/03/10 DOTA
原生javascript实现图片轮播效果代码
2010/09/03 Javascript
jQuery实现的一个自定义Placeholder属性插件
2014/08/11 Javascript
jQuery中 delegate使用的问题
2015/07/03 Javascript
非常实用的12个jquery代码片段
2015/11/02 Javascript
AngularJS整合Springmvc、Spring、Mybatis搭建开发环境
2016/02/25 Javascript
浅析如何利用angular结合translate为项目实现国际化
2016/12/08 Javascript
Angular.JS通过指令操作DOM的方法
2017/05/10 Javascript
基于JS实现移动端左滑删除功能
2017/07/28 Javascript
jQuery绑定事件方法及区别(bind,click,on,live,one)
2017/08/14 jQuery
NodeJs通过async/await处理异步的方法
2017/10/09 NodeJs
Angular2学习笔记之数据绑定的示例代码
2018/01/03 Javascript
vue2中使用less简易教程
2018/03/27 Javascript
vue动画之点击按钮往上渐渐显示出来的实例
2018/09/29 Javascript
对angularJs中自定义指令replace的属性详解
2018/10/09 Javascript
利用JavaScript的Map提升性能的方法详解
2019/08/14 Javascript
浅析JS中NEW的实现原理及重写
2020/02/20 Javascript
JS实现简单打字测试
2020/06/24 Javascript
[01:08:32]DOTA2-DPC中国联赛 正赛 DLG vs PHOENIX BO3 第二场 1月18日
2021/03/11 DOTA
剖析Python的Twisted框架的核心特性
2016/05/25 Python
Python引用传值概念与用法实例小结
2017/10/07 Python
使用python获取csv文本的某行或某列数据的实例
2018/04/03 Python
使用 Python ssh 远程登陆服务器的最佳方案
2020/03/06 Python
Pycharm-community-2020.2.3 社区版安装教程图文详解
2020/12/08 Python
纯css3无js实现的Android Logo(有简单动画)
2013/01/21 HTML / CSS
详解使用HTML5 Canvas创建动态粒子网格动画
2016/12/14 HTML / CSS
世嘉游戏英国官方商店:SEGA Shop UK
2019/09/20 全球购物
波兰在线运动商店:YesSport
2020/07/23 全球购物
Marlies Dekkers内衣荷兰官方网店:荷兰奢侈内衣品牌
2020/03/27 全球购物
家长会主持词
2014/03/26 职场文书
银行内勤岗位职责
2014/04/09 职场文书
2019年妇科护士的自我鉴定(3篇)
2019/09/26 职场文书
处世之道:关于真诚相待的名言推荐
2019/12/02 职场文书
python xlwt模块的使用解析
2021/04/13 Python