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 相关文章推荐
web.py中调用文件夹内模板的方法
Aug 26 Python
Python装饰器的函数式编程详解
Feb 27 Python
Python从使用线程到使用async/await的深入讲解
Sep 16 Python
python中强大的format函数实例详解
Dec 05 Python
python中嵌套函数的实操步骤
Feb 27 Python
详解python实现数据归一化处理的方式:(0,1)标准化
Jul 17 Python
Python 矩阵转置的几种方法小结
Dec 02 Python
Python使用configparser库读取配置文件
Feb 22 Python
Python Opencv实现单目标检测的示例代码
Sep 08 Python
Django生成数据库及添加用户报错解决方案
Oct 09 Python
python实现简单的井字棋
May 26 Python
Python中tqdm的使用和例子
Sep 23 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
BBS(php & mysql)完整版(六)
2006/10/09 PHP
php公用函数列表[正则]
2007/02/22 PHP
php 冒泡排序 交换排序法
2011/05/10 PHP
浅谈PHP匿名函数和闭包
2019/03/08 PHP
使两个iframe的高度与内容自适应,且相等
2006/11/20 Javascript
基于jQuery的ajax功能实现web service的json转化
2009/08/29 Javascript
javascript 折半查找字符在数组中的位置(有序列表)
2010/12/09 Javascript
js简单实现删除记录时的提示效果
2013/12/05 Javascript
AngularJS入门教程(一):静态模板
2014/12/06 Javascript
jQuery中has()方法用法实例
2015/01/06 Javascript
使用Raygun来自动追踪AngularJS中的异常
2015/06/23 Javascript
js实现温度计时间样式代码分享
2015/08/21 Javascript
jquery 遍历数组 each 方法详解
2016/05/25 Javascript
jQuery通过ajax方法获取json数据不执行success的原因及解决方法
2016/10/15 Javascript
weUI应用之JS常用信息提示弹层的封装
2016/11/21 Javascript
ES6 javascript中Class类继承用法实例详解
2017/10/30 Javascript
vue+express+jwt持久化登录的方法
2019/06/14 Javascript
微信小程序通过js实现瀑布流布局详解
2019/08/28 Javascript
小程序最新获取用户昵称和头像的方法总结
2019/09/23 Javascript
微信小程序 自定义弹窗实现过程(附代码)
2019/12/05 Javascript
微信小程序实现日历签到
2020/09/21 Javascript
[01:14:12]2018DOTA2亚洲邀请赛4.7 总决赛 LGD vs Mineski 第二场
2018/04/09 DOTA
python抓取并保存html页面时乱码问题的解决方法
2016/07/01 Python
如何用Python合并lmdb文件
2018/07/02 Python
python使用matplotlib绘制热图
2018/11/07 Python
Python进程间通信Queue消息队列用法分析
2019/05/22 Python
pytorch 中的重要模块化接口nn.Module的使用
2020/04/02 Python
Madewell澳大利亚官方网站:美国休闲服饰品牌
2019/07/18 全球购物
汽车运用工程系毕业生自荐信
2013/12/27 职场文书
社区戒毒工作方案
2014/06/04 职场文书
教师党员整改措施
2014/10/24 职场文书
劳模先进事迹材料
2014/12/24 职场文书
公务员爱岗敬业心得体会
2016/01/25 职场文书
干货干货!2019最新优秀创业计划书
2019/03/21 职场文书
python实现简单区块链结构
2021/04/25 Python
Mysql如何实现不存在则插入,存在则更新
2022/03/25 MySQL