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 相关文章推荐
35个Python编程小技巧
Apr 01 Python
python获取android设备的GPS信息脚本分享
Mar 06 Python
Python网络编程中urllib2模块的用法总结
Jul 12 Python
利用scrapy将爬到的数据保存到mysql(防止重复)
Mar 31 Python
Python requests发送post请求的一些疑点
May 20 Python
python 编写简单网页服务器的实例
Jun 01 Python
selenium处理元素定位点击无效问题
Jun 12 Python
python判断链表是否有环的实例代码
Jan 31 Python
python爬虫开发之使用python爬虫库requests,urllib与今日头条搜索功能爬取搜索内容实例
Mar 10 Python
Python urllib3软件包的使用说明
Nov 18 Python
神经网络训练采用gpu设置的方式
Mar 03 Python
python实现监听键盘
Apr 26 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带抄送和密件抄送的邮件发送方法
2015/03/20 PHP
PHP图像处理类库MagickWand用法实例分析
2015/05/21 PHP
10个php函数实用却不常见
2015/10/13 PHP
浅谈php7的重大新特性
2015/10/23 PHP
PHP+AJAX 投票器功能
2017/11/11 PHP
写的htc的数据表格
2007/01/20 Javascript
Javascript 判断客户端浏览器类型代码
2010/03/01 Javascript
ExtJS PropertyGrid中使用Combobox选择值问题
2010/06/13 Javascript
JavaScript 注册事件代码
2011/01/27 Javascript
mailto的使用技巧分享
2012/12/21 Javascript
Jquery submit()无法提交问题
2013/04/21 Javascript
jquery 取子节点及当前节点属性值
2014/07/25 Javascript
jquery利用命名空间移除绑定事件的方法
2015/03/11 Javascript
JavaScript中的setMilliseconds()方法使用详解
2015/06/11 Javascript
jQuery结合AJAX之在页面滚动时从服务器加载数据
2015/06/30 Javascript
分享15个大家都熟知的jquery小技巧
2015/12/02 Javascript
jquery获取img的src值的简单实例
2016/05/17 Javascript
基于vue cli重构多页面脚手架过程详解
2018/01/23 Javascript
微信小程序BindTap快速连续点击目标页面跳转多次问题处理
2019/04/08 Javascript
vue fetch中的.then()的正确使用方法
2020/04/17 Javascript
[40:05]DOTA2上海特级锦标赛A组小组赛#1 EHOME VS MVP.Phx第一局
2016/02/25 DOTA
[01:29:17]RNG vs Liquid 2019国际邀请赛淘汰赛 败者组 BO3 第二场 8.23
2019/09/05 DOTA
Python内置的字符串处理函数整理
2013/01/29 Python
Python中3种内建数据结构:列表、元组和字典
2014/11/30 Python
利用Python获取操作系统信息实例
2016/09/02 Python
Django实现自定义404,500页面教程
2017/03/26 Python
将pandas.dataframe的数据写入到文件中的方法
2018/12/07 Python
vim自动补全插件YouCompleteMe(YCM)安装过程解析
2019/10/21 Python
Python3.7 读取 mp3 音频文件生成波形图效果
2019/11/05 Python
python3 中时间戳、时间、日期的转换和加减操作
2020/07/14 Python
Python+OpenCV图像处理——实现直线检测
2020/10/23 Python
基于python模拟TCP3次握手连接及发送数据
2020/11/06 Python
施华洛世奇匈牙利官网:SWAROVSKI匈牙利
2019/07/06 全球购物
一套软件测试笔试题
2014/07/25 面试题
群众路线批评与自我批评发言稿
2014/10/16 职场文书
详解Oracle块修改跟踪功能
2021/11/07 Oracle