Python使用PyCrypto实现AES加密功能示例


Posted in Python onMay 22, 2017

本文实例讲述了Python使用PyCrypto实现AES加密功能。分享给大家供大家参考,具体如下:

#!/usr/bin/env python
from Crypto.Cipher import AES
import base64
import os
# the block size for the cipher object; must be 16, 24, or 32 for AES
BLOCK_SIZE = 32
# the character used for padding--with a block cipher such as AES, the value
# you encrypt must be a multiple of BLOCK_SIZE in length. This character is
# used to ensure that your value is always a multiple of BLOCK_SIZE
PADDING = '{'
# one-liner to sufficiently pad the text to be encrypted
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
# one-liners to encrypt/encode and decrypt/decode a string
# encrypt with AES, encode with base64
EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
# generate a random secret key
secret = os.urandom(BLOCK_SIZE)
# create a cipher object using the random secret
cipher = AES.new(secret)
# encode a string
encoded = EncodeAES(cipher, 'password')
print 'Encrypted string:', encoded
# decode the encoded string
decoded = DecodeAES(cipher, encoded)
print 'Decrypted string:', decoded
Python 相关文章推荐
Python2.x中文乱码问题解决方法
Jun 02 Python
Python爬虫包 BeautifulSoup  递归抓取实例详解
Jan 28 Python
数据清洗--DataFrame中的空值处理方法
Jul 03 Python
python3.6.3转化为win-exe文件发布的方法
Oct 31 Python
python消费kafka数据批量插入到es的方法
Dec 27 Python
python支付宝支付示例详解
Aug 22 Python
详解Matplotlib绘图之属性设置
Aug 23 Python
django-crontab 定时执行任务方法的实现
Sep 06 Python
numpy 返回函数的上三角矩阵实例
Nov 25 Python
解决Keras 与 Tensorflow 版本之间的兼容性问题
Feb 07 Python
解决Alexnet训练模型在每个epoch中准确率和loss都会一升一降问题
Jun 17 Python
Python常用扩展插件使用教程解析
Nov 02 Python
django+js+ajax实现刷新页面的方法
May 22 #Python
Python正则表达式经典入门教程
May 22 #Python
Python AES加密模块用法分析
May 22 #Python
Python 安装setuptools和pip工具操作方法(必看)
May 22 #Python
对Python进行数据分析_关于Package的安装问题
May 22 #Python
详解python之配置日志的几种方式
May 22 #Python
多版本Python共存的配置方法
May 22 #Python
You might like
PHP 高手之路(二)
2006/10/09 PHP
PHP实现的解汉诺塔问题算法示例
2018/08/06 PHP
php实现QQ小程序发送模板消息功能
2019/09/18 PHP
javascript EXCEL 操作类代码
2009/07/30 Javascript
基于jquery的跨域调用文件
2010/11/19 Javascript
jQuery(非HTML5)可编辑表格实现代码
2012/12/11 Javascript
jquery 事件冒泡的介绍以及如何阻止事件冒泡
2012/12/25 Javascript
防止浏览器记住用户名及密码的简单实用方法
2013/04/22 Javascript
JQUERY对单选框(radio)操作的小例子
2013/04/25 Javascript
jQuery中Datatables增加跳转到指定页功能
2017/02/08 Javascript
Angularjs分页查询的实现
2017/02/24 Javascript
简单谈谈JS中的正则表达式
2017/09/11 Javascript
JavaScript编程设计模式之观察者模式(Observer Pattern)实例详解
2017/10/25 Javascript
vue项目中使用scss的方法步骤
2019/05/16 Javascript
vue自定义正在加载动画的例子
2019/11/14 Javascript
CentOS 6.5下安装Python 3.5.2(与Python2并存)
2017/06/05 Python
Python 多线程不加锁分块读取文件的方法
2018/12/11 Python
Python实现程序判断季节的代码示例
2019/01/28 Python
Python FFT合成波形的实例
2019/12/04 Python
Python Opencv图像处理基本操作代码详解
2020/08/31 Python
python装饰器代码深入讲解
2021/03/01 Python
解决canvas转base64/jpeg时透明区域变成黑色背景的方法
2016/10/23 HTML / CSS
Myprotein葡萄牙官方网站:英国优质运动营养品牌
2016/09/12 全球购物
伦敦最有品味的百货:Liberty London
2016/11/12 全球购物
国外最大的眼镜网站:Coastal
2017/08/09 全球购物
Shopping happy life西班牙:以最优惠的价格提供最好的时尚配饰
2020/03/13 全球购物
生物制药毕业生自荐信
2013/10/16 职场文书
项目投资意向书
2014/04/01 职场文书
自动化专业毕业生求职信
2014/06/18 职场文书
大专生自我鉴定怎么写
2014/09/16 职场文书
学位证书委托书
2014/09/30 职场文书
社区活动总结范文
2015/05/07 职场文书
困难补助申请报告
2015/05/19 职场文书
酒店开业主持词
2015/07/02 职场文书
解决pycharm下载库时出现Failed to install package的问题
2021/09/04 Python
Redis特殊数据类型bitmap位图
2022/06/01 Redis