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益智游戏计算汉诺塔问题示例
Mar 05 Python
python处理圆角图片、圆形图片的例子
Apr 25 Python
用python制作游戏外挂
Jan 04 Python
基于python内置函数与匿名函数详解
Jan 09 Python
django文档学习之applications使用详解
Jan 29 Python
基于python list对象中嵌套元组使用sort时的排序方法
Apr 18 Python
python版本的仿windows计划任务工具
Apr 30 Python
解决pycharm界面不能显示中文的问题
May 23 Python
解决安装tensorflow遇到无法卸载numpy 1.8.0rc1的问题
Jun 13 Python
如何在Django中添加没有微秒的 DateTimeField 属性详解
Jan 30 Python
python fuzzywuzzy模块模糊字符串匹配详细用法
Aug 29 Python
Python操作word文档插入图片和表格的实例演示
Oct 25 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字符转义相关函数小结(php下的转义字符串)
2007/04/12 PHP
关于php fread()使用技巧
2010/01/22 PHP
php skymvc 一款轻量、简单的php
2011/06/28 PHP
linux使用crontab实现PHP执行计划定时任务
2014/05/10 PHP
php过滤html标记属性类用法实例
2014/09/23 PHP
PHP实践教程之过滤、验证、转义与密码详解
2017/07/24 PHP
TP5(thinkPHP5框架)基于bootstrap实现的单图上传插件用法示例
2019/05/29 PHP
Firefox中beforeunload事件的实现缺陷浅析
2012/05/03 Javascript
javaScript中的this示例学习详解及工作原理
2014/01/13 Javascript
JavaScript中诡异的delete操作符
2015/03/12 Javascript
jquery仿ps颜色拾取功能
2017/03/08 Javascript
Zepto实现密码的隐藏/显示
2017/04/07 Javascript
JavaScript ES6中const、let与var的对比详解
2017/06/18 Javascript
vue自定义指令用法经典实例小结
2019/03/16 Javascript
微信小程序 高德地图路线规划实现过程详解
2019/08/05 Javascript
js实现点击图片在屏幕中间弹出放大效果
2019/09/11 Javascript
vue通过过滤器实现数据格式化
2020/07/20 Javascript
[01:03:59]2018DOTA2亚洲邀请赛3月30日 小组赛B组VGJ.T VS Secret
2018/03/31 DOTA
python3监控CentOS磁盘空间脚本
2018/06/21 Python
Django  ORM 练习题及答案
2019/07/19 Python
利用Python库Scapy解析pcap文件的方法
2019/07/23 Python
pygame实现贪吃蛇游戏(上)
2019/10/29 Python
Python3 main函数使用sys.argv传入多个参数的实现
2019/12/25 Python
浅谈Python3实现两个矩形的交并比(IoU)
2020/01/18 Python
python 使用elasticsearch 实现翻页的三种方式
2020/07/31 Python
python字典key不能是可以是啥类型
2020/08/04 Python
Python爬虫之App爬虫视频下载的实现
2020/12/08 Python
英国航空官网:British Airways
2016/09/11 全球购物
美国购买汽车零件网站:Buy Auto Parts
2018/04/02 全球购物
Under Armour西班牙官网:美国知名的高端功能性运动品牌
2018/12/12 全球购物
教师节倡议书
2014/08/30 职场文书
自荐信怎么写
2015/03/04 职场文书
黑暗中的舞者观后感
2015/06/18 职场文书
如何使用vue3打造一个物料库
2021/05/08 Vue.js
Qt自定义Plot实现曲线绘制的详细过程
2021/11/02 Python
Java字符串逆序方法详情
2022/03/21 Java/Android