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中使用wxPython开发的一个简易笔记本程序实例
Feb 08 Python
使用Python编写类UNIX系统的命令行工具的教程
Apr 15 Python
Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
Apr 24 Python
Python3 模块、包调用&路径详解
Oct 25 Python
Python打开文件,将list、numpy数组内容写入txt文件中的方法
Oct 26 Python
python实现连连看辅助之图像识别延伸
Jul 17 Python
python将print输出的信息保留到日志文件中
Sep 27 Python
np.newaxis 实现为 numpy.ndarray(多维数组)增加一个轴
Nov 30 Python
基于python实现获取网页图片过程解析
May 11 Python
为什么相对PHP黑python的更少
Jun 21 Python
详解查看Python解释器路径的两种方式
Oct 15 Python
Python数据可视化之用Matplotlib绘制常用图形
Jun 03 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
Yii入门教程之目录结构、入口文件及路由设置
2014/11/25 PHP
PHP魔术方法使用方法汇总
2016/02/14 PHP
PHP实现微信商户支付企业付款到零钱功能
2018/09/30 PHP
php中的explode()函数实例介绍
2019/01/18 PHP
YII框架页面缓存操作示例
2019/04/29 PHP
基于jquery实现的图片在各种分辨率下未知的容器内上下左右居中
2014/05/11 Javascript
关于JS中match() 和 exec() 返回值和属性的测试
2016/03/21 Javascript
JS函数arguments数组获得实际传参数个数的实现方法
2016/05/28 Javascript
JS组件系列之使用HTML标签的data属性初始化JS组件
2016/09/14 Javascript
JS实现根据密码长度显示安全条功能
2017/03/08 Javascript
jQuery动态追加页面数据以及事件委托详解
2017/05/06 jQuery
ajax +NodeJS 实现图片上传实例
2017/06/06 NodeJs
浅谈Node异步编程的机制
2017/10/18 Javascript
Webpack中publicPath路径问题详解
2018/05/03 Javascript
JS实现显示当前日期的实例代码
2018/07/03 Javascript
详解React项目如何修改打包地址(编译输出文件地址)
2019/03/21 Javascript
vue模仿网易云音乐的单页面应用
2019/04/24 Javascript
[48:21]林俊杰圣堂刺客超神杀戮秀
2014/10/29 DOTA
Python批量修改文本文件内容的方法
2016/04/29 Python
Python读写Json涉及到中文的处理方法
2016/09/12 Python
Python排序算法实例代码
2017/08/10 Python
python生成1行四列全2矩阵的方法
2018/08/04 Python
python itchat给指定联系人发消息的方法
2019/06/11 Python
Python with标签使用方法解析
2020/01/17 Python
python实现简单的购物程序代码实例
2020/03/03 Python
opencv python在视屏上截图功能的实现
2020/03/05 Python
CSS3的column-fill属性对齐列内容高度的用法详解
2016/07/01 HTML / CSS
LVMH旗下最大的奢侈品网站平台:24S
2020/05/24 全球购物
自考毕业生自我鉴定
2013/11/04 职场文书
初中生期末考试的自我评价
2013/12/17 职场文书
亲子活动总结
2014/04/26 职场文书
禁止高声喧哗的标语
2014/06/11 职场文书
2015年爱牙日活动总结
2015/02/05 职场文书
让世界充满爱观后感
2015/06/10 职场文书
安全教育观后感
2015/06/17 职场文书
解决python3安装pandas出错的问题
2021/05/20 Python