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中用函数作为返回值和实现闭包的教程
Apr 27 Python
Python list操作用法总结
Nov 10 Python
Python随机数random模块使用指南
Sep 09 Python
python 采集中文乱码问题的完美解决方法
Sep 27 Python
关于Python元祖,列表,字典,集合的比较
Jan 06 Python
python cx_Oracle模块的安装和使用详细介绍
Feb 13 Python
用Python实现随机森林算法的示例
Aug 24 Python
基于python内置函数与匿名函数详解
Jan 09 Python
python pygame模块编写飞机大战
Nov 20 Python
Python同步遍历多个列表的示例
Feb 19 Python
python代码如何实现余弦相似性计算
Feb 09 Python
python如何调用java类
Jul 05 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的Yii 2框架的开发优势
2015/07/03 PHP
PHP基本语法实例总结
2016/09/09 PHP
基于jQuery的的一个隔行变色,鼠标移动变色的小插件
2010/07/06 Javascript
jquery datepicker参数介绍和示例
2014/04/15 Javascript
ActiveX控件与Javascript之间的交互示例
2014/06/04 Javascript
jQuery实现可以控制图片旋转角度效果(附demo源码下载)
2016/01/27 Javascript
JS实现JSON.stringify的实例代码讲解
2017/02/07 Javascript
详解vue-cli开发环境跨域问题解决方案
2017/06/06 Javascript
js实现多张图片延迟加载效果
2017/07/17 Javascript
js字符限制(字符截取) 一个中文汉字算两个字符
2017/09/12 Javascript
基于Vue单文件组件详解
2017/09/15 Javascript
基于js 字符串indexof与search方法的区别(详解)
2017/12/04 Javascript
详解JavaScript 中 if / if...else...替换方式
2018/07/15 Javascript
ES6使用新特性Proxy实现的数据绑定功能实例
2020/05/11 Javascript
微信小程序使用GoEasy实现websocket实时通讯
2020/05/19 Javascript
jQuery cookie的公共方法封装和使用示例
2020/06/01 jQuery
[06:25]第二届DOTA2亚洲邀请赛主赛事第二天比赛集锦.mp4
2017/04/03 DOTA
Python和perl实现批量对目录下电子书文件重命名的代码分享
2014/11/21 Python
简单介绍Python中的decode()方法的使用
2015/05/18 Python
Python的爬虫包Beautiful Soup中用正则表达式来搜索
2016/01/20 Python
解析Python中的生成器及其与迭代器的差异
2016/06/20 Python
基于Python代码编辑器的选用(详解)
2017/09/13 Python
在Django model中设置多个字段联合唯一约束的实例
2019/07/17 Python
PyCharm2019安装教程及其使用(图文教程)
2019/09/29 Python
PyTorch中的padding(边缘填充)操作方式
2020/01/03 Python
移动端HTML5开发神器之vconsole详解
2020/12/15 HTML / CSS
法国在线药房:Shop Pharmacie
2019/11/26 全球购物
Unix如何添加新的用户
2014/08/20 面试题
人力资源主管岗位职责
2014/01/29 职场文书
会计专业职业规划:规划自我赢取未来
2014/02/12 职场文书
优秀党务工作者事迹材料
2014/05/07 职场文书
三提三创主题教育活动查摆整改措施
2014/10/25 职场文书
先进班组材料范文
2014/12/25 职场文书
4S店客服专员岗位职责
2015/04/07 职场文书
2015年家长学校工作总结
2015/04/22 职场文书
送达通知书
2015/04/25 职场文书