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修改Excel数据的实例代码
Nov 01 Python
linux系统使用python获取内存使用信息脚本分享
Jan 15 Python
python实现360皮肤按钮控件示例
Feb 21 Python
python使用Berkeley DB数据库实例
Sep 26 Python
python中常用的九种预处理方法分享
Sep 11 Python
解决pyinstaller打包exe文件出现命令窗口一闪而过的问题
Oct 31 Python
python引用(import)某个模块提示没找到对应模块的解决方法
Jan 19 Python
Python实现微信消息防撤回功能的实例代码
Apr 29 Python
python 设置输出图像的像素大小方法
Jul 04 Python
python中的函数递归和迭代原理解析
Nov 14 Python
如何使用Python进行PDF图片识别OCR
Jan 22 Python
Python实战之疫苗研发情况可视化
May 18 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
本地机apache配置基于域名的虚拟主机详解
2013/08/10 PHP
php漏洞之跨网站请求伪造与防止伪造方法
2013/08/15 PHP
PHP设计模式之原型模式定义与用法详解
2018/04/03 PHP
PHP基于递归算法解决兔子生兔子问题
2018/05/11 PHP
js 页面刷新location.reload和location.replace的区别小结
2009/12/24 Javascript
通过正则格式化url查询字符串实现代码
2012/12/28 Javascript
使用jQuery.fn自定义jQuery翻页插件
2013/01/20 Javascript
自己动手实现jQuery Callbacks完整功能代码详解
2013/11/25 Javascript
js输出阴历、阳历、年份、月份、周示例代码
2014/01/29 Javascript
JavaScript中join()方法的使用简介
2015/06/09 Javascript
浅析AngularJS中的指令
2016/03/20 Javascript
JavaScript基础——使用Canvas绘图
2016/11/02 Javascript
JavaScript的事件机制详解
2017/01/17 Javascript
微信小程序 获取二维码实例详解
2017/06/23 Javascript
深入探讨JavaScript的最基本部分之执行上下文
2019/02/12 Javascript
vue中解决chrome浏览器自动播放音频和MP3语音打包到线上的实现方法
2020/10/09 Javascript
[05:09]2016国际邀请赛中国区预选赛淘汰赛首日精彩回顾
2016/06/29 DOTA
Python中的localtime()方法使用详解
2015/05/22 Python
Python中捕获键盘的方式详解
2019/03/28 Python
python利用selenium进行浏览器爬虫
2019/04/25 Python
利用Python进行图像的加法,图像混合(附代码)
2019/07/14 Python
DC Shoes官网:美国滑板鞋和服饰品牌
2017/09/03 全球购物
Bose法国官网:购买耳机、扬声器、家庭影院、专业音响
2017/12/21 全球购物
世界各地的旅游、观光和活动:Isango!
2019/10/29 全球购物
编码实现字符串转整型的函数
2012/06/02 面试题
护士长竞聘演讲稿
2014/04/30 职场文书
施工安全汇报材料
2014/08/17 职场文书
文明社区申报材料
2014/08/21 职场文书
教师党员批评与自我批评
2014/10/15 职场文书
2014年大学生村官工作总结
2014/11/19 职场文书
初三学生语文考试作弊检讨书
2014/12/14 职场文书
2016大一新生军训心得体会
2016/01/11 职场文书
高考升学宴主持词
2019/06/21 职场文书
win10下go mod配置方式
2021/04/25 Golang
如何理解及使用Python闭包
2021/06/01 Python
ConditionalOnProperty配置swagger不生效问题及解决
2022/06/14 Java/Android