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通过BF算法实现关键词匹配的方法
Mar 13 Python
使用IPython下的Net-SNMP来管理类UNIX系统的教程
Apr 15 Python
qpython3 读取安卓lastpass Cookies
Jun 19 Python
Python实现将照片变成卡通图片的方法【基于opencv】
Jan 17 Python
pandas数据分组和聚合操作方法
Apr 11 Python
Python 保存矩阵为Excel的实现方法
Jan 28 Python
Python3开发实例之非关系型图数据库Neo4j安装方法及Python3连接操作Neo4j方法实例
Mar 18 Python
python实现梯度下降法
Mar 24 Python
在matplotlib中改变figure的布局和大小实例
Apr 23 Python
Python Opencv轮廓常用操作代码实例解析
Sep 01 Python
Pycharm安装第三方库失败解决方案
Nov 17 Python
Python time库的时间时钟处理
May 02 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/10/09 PHP
Codeigniter实现处理用户登录验证后的URL跳转
2014/06/12 PHP
php+mysql不用递归实现的无限级分类实例(非递归)
2014/07/08 PHP
JS JavaScript获取Url参数,src属性参数
2021/03/09 Javascript
Prototype 学习 Prototype对象
2009/07/12 Javascript
JS创建类和对象的两种不同方式
2014/08/08 Javascript
一系列Bootstrap导航条使用方法分享
2016/04/29 Javascript
javascript实现获取图片大小及图片等比缩放的方法
2016/11/24 Javascript
原生JavaScript实现精美的淘宝轮播图效果示例【附demo源码下载】
2017/05/27 Javascript
解析Vue2 dist 目录下各个文件的区别
2017/11/22 Javascript
javascript字体颜色控件的开发 JS实现字体控制
2017/11/27 Javascript
jQuery模拟12306城市选择框功能简单实现方法示例
2018/08/13 jQuery
详解微信小程序之scroll-view的flex布局问题
2019/01/16 Javascript
JavaScript数据结构与算法之二叉树添加/删除节点操作示例
2019/03/01 Javascript
如何通过setTimeout理解JS运行机制详解
2019/03/23 Javascript
详解微信小程序工程化探索之webpack实战
2020/04/20 Javascript
jQuery 隐藏/显示效果函数用法实例分析
2020/05/20 jQuery
vue根据条件不同显示不同按钮的操作
2020/08/04 Javascript
关于vue的列表图片选中打钩操作
2020/09/09 Javascript
JS如何监听div的resize事件详解
2020/12/03 Javascript
[00:53]TI3正赛第三天 DK怒破A队不败金身 现场国旗飘扬热血激昂
2013/08/10 DOTA
[01:31:03]DOTA2完美盛典全回顾 见证十五项大奖花落谁家
2017/11/28 DOTA
Pandas中DataFrame的分组/分割/合并的实现
2019/07/16 Python
解决Django 在ForeignKey中出现 non-nullable field错误的问题
2019/08/06 Python
Python搭建代理IP池实现存储IP的方法
2019/10/27 Python
numpy.linalg.eig() 计算矩阵特征向量方式
2019/11/29 Python
使用Python操作ArangoDB的方法步骤
2020/02/02 Python
tensorflow 报错unitialized value的解决方法
2020/02/06 Python
AmazeUI中各种的导航式菜单与解决方法
2020/08/19 HTML / CSS
巴西最大的在线约会网站:ParPerfeito
2018/07/11 全球购物
PHP如何防止SQL注入
2014/05/03 面试题
2015年手术室工作总结
2015/05/11 职场文书
初中军训感言
2015/08/01 职场文书
2015年幼儿园班主任个人工作总结
2015/10/22 职场文书
MySQL删除和插入数据很慢的问题解决
2021/06/03 MySQL
一篇文章看懂MySQL主从复制与读写分离
2021/11/07 MySQL