Python使用PyCrypto实现AES加密功能示例
作者:pythopen 时间:2022-09-18 13:23:57
本文实例讲述了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
PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password
在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
希望本文所述对大家Python程序设计有所帮助。
标签:Python,PyCrypto,AES加密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
巧妙的自关联运用
2012-10-07 10:55:58
Python和perl实现批量对目录下电子书文件重命名的代码分享
2022-01-28 02:51:48
6款jQuery图表插件[译]
2009-06-01 10:34:00
![](https://img.aspxhome.com/file/UploadPic/20096/1/08200312t-81s.png)
kNN算法python实现和简单数字识别的方法
2023-09-05 21:44:36
![](https://img.aspxhome.com/file/2023/1/72751_0s.png)
python绘制字符画视频的示例代码
2023-11-09 16:21:46
![](https://img.aspxhome.com/file/2023/7/63957_0s.jpg)
asp如何对文件进行操作?
2009-11-20 18:31:00
![](https://img.aspxhome.com/file/UploadPic/200911/20/20091120184513823s.gif)
Python random模块用法解析及简单示例
2022-06-02 11:05:14
![](https://img.aspxhome.com/file/2023/8/65958_0s.png)
用js实现放大镜效果
2023-09-19 18:29:29
![](https://img.aspxhome.com/file/2023/0/56200_0s.jpg)
Python实现的FTP通信客户端与服务器端功能示例
2023-10-02 21:36:01
![](https://img.aspxhome.com/file/2023/9/63189_0s.png)
js验证表单(form)中多选框(checkbox)值
2008-03-18 13:39:00
MSSQL中递归SQL查询语句实例说明-
2011-09-30 11:42:43
Go语言中的匿名结构体用法实例
2023-07-07 11:10:21
mysql性能的检查和调优方法
2009-05-17 09:21:00
Python基础知识学习之类的继承
2022-09-02 15:41:05
php中in_array函数用法探究
2023-06-21 21:03:54
一个Access数据库数据传递的实例方法
2008-11-28 16:24:00
MySQL与PHP的基础与应用专题之自连接
2023-11-14 08:52:37
![](https://img.aspxhome.com/file/2023/3/77203_0s.png)
Script 元素 type 属性的妙用
2011-03-07 16:13:00
发一新浪招聘的图片滚动控制JS效果
2011-08-10 19:17:25
Hibernate Oracle sequence的使用技巧
2023-07-06 05:18:42