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 相关文章推荐
python获取指定目录下所有文件名列表的方法
May 20 Python
编写Python脚本抓取网络小说来制作自己的阅读器
Aug 20 Python
详解Python多线程Selenium跨浏览器测试
Apr 01 Python
python中实现控制小数点位数的方法
Jan 24 Python
python3 反射的四种基本方法解析
Aug 26 Python
Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件封装函数的方法
Sep 23 Python
基于nexus3配置Python仓库过程详解
Jun 15 Python
学会迭代器设计模式,帮你大幅提升python性能
Jan 03 Python
Python中的min及返回最小值索引的操作
May 10 Python
我对PyTorch dataloader里的shuffle=True的理解
May 20 Python
Python实现8种常用抽样方法
Jun 27 Python
基于PyQT5制作一个桌面摸鱼工具
Feb 15 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/11/25 PHP
php 操作数组(合并,拆分,追加,查找,删除等)
2012/07/20 PHP
PHP不用递归遍历目录下所有文件的代码
2014/07/04 PHP
PHP-FPM运行状态的实时查看及监控详解
2016/11/18 PHP
PHP数字金额转换成中文大写显示
2019/01/05 PHP
laravel框架实现敏感词汇过滤功能示例
2020/02/15 PHP
SWFObject Flash js调用类
2008/07/08 Javascript
Ajax 数据请求的简单分析
2011/04/05 Javascript
JS中toFixed()方法引起的问题如何解决
2012/11/20 Javascript
JS模式之单例模式基本用法
2015/06/30 Javascript
jquery实现LED广告牌旋转系统图片切换效果代码分享
2015/08/26 Javascript
Js获取图片原始宽高的实现代码
2016/05/17 Javascript
浅谈EasyUI常用控件的禁用方法
2016/11/09 Javascript
纯javascript版日历控件
2016/11/24 Javascript
javascript简单链式调用案例分析
2017/05/10 Javascript
jquery.validate.js 多个相同name的处理方式
2017/07/10 jQuery
浅谈vuejs实现数据驱动视图原理
2018/02/23 Javascript
JS实现4位随机验证码
2020/10/19 Javascript
python实现的阳历转阴历(农历)算法
2014/04/25 Python
Python文件操作,open读写文件,追加文本内容实例
2016/12/14 Python
python针对excel的操作技巧
2018/03/13 Python
基于python实现KNN分类算法
2020/04/23 Python
python将视频转换为全字符视频
2019/04/26 Python
Python爬取知乎图片代码实现解析
2019/09/17 Python
jenkins配置python脚本定时任务过程图解
2019/10/29 Python
使用Python来做一个屏幕录制工具的操作代码
2020/01/18 Python
django queryset相加和筛选教程
2020/05/18 Python
纯css3实现图片翻牌特效
2015/03/10 HTML / CSS
投资合作协议书
2014/04/17 职场文书
环保建议书600字
2014/05/14 职场文书
党员群众路线教育实践活动学习笔记
2014/11/05 职场文书
水电工程师岗位职责
2015/02/13 职场文书
2015年学校少先队工作总结
2015/07/20 职场文书
计算机实训心得体会
2016/01/14 职场文书
会议开幕致辞怎么写
2016/03/03 职场文书
python 进阶学习之python装饰器小结
2021/09/04 Python