python简单实现AES加密和解密
作者:Frankssss 时间:2021-08-12 06:05:48
本文实例为大家分享了python实现AES加密和解密的具体代码,供大家参考,具体内容如下
参考:python实现AES加密和解密
AES加密算法是一种对称加密算法, 他有一个密匙, 即用来加密, 也用来解密
import base64
from Crypto.Cipher import AES
# 密钥(key), 密斯偏移量(iv) CBC模式加密
def AES_Encrypt(key, data):
vi = '0102030405060708'
pad = lambda s: s + (16 - len(s)%16) * chr(16 - len(s)%16)
data = pad(data)
# 字符串补位
cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
encryptedbytes = cipher.encrypt(data.encode('utf8'))
# 加密后得到的是bytes类型的数据
encodestrs = base64.b64encode(encryptedbytes)
# 使用Base64进行编码,返回byte字符串
enctext = encodestrs.decode('utf8')
# 对byte字符串按utf-8进行解码
return enctext
def AES_Decrypt(key, data):
vi = '0102030405060708'
data = data.encode('utf8')
encodebytes = base64.decodebytes(data)
# 将加密数据转换位bytes类型数据
cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
text_decrypted = cipher.decrypt(encodebytes)
unpad = lambda s: s[0:-s[-1]]
text_decrypted = unpad(text_decrypted)
# 去补位
text_decrypted = text_decrypted.decode('utf8')
return text_decrypted
key = '0CoJUm6Qyw8W8jud'
data = 'sdadsdsdsfd'
AES_Encrypt(key, data)
enctext = AES_Encrypt(key, data)
print(enctext)
text_decrypted = AES_Decrypt(key, enctext)
print(text_decrypted)
hBXLrMkpkBpDFsf9xSRGQQ==
sdadsdsdsfd
来源:https://www.cnblogs.com/frank-shen/p/10281708.html
标签:python,AES,加密,解密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
简述Python中的进程、线程、协程
2021-04-07 11:19:02
python实现扫描局域网指定网段ip的方法
2023-09-28 16:44:45
linux下安装php扩展memcache的方法
2023-11-21 19:49:02
![](https://img.aspxhome.com/file/2023/4/112714_0s.jpg)
适合Python初学者的一些编程技巧
2022-01-14 11:19:09
Python生成pdf目录书签的实例方法
2023-10-11 15:43:21
![](https://img.aspxhome.com/file/2023/0/78780_0s.png)
使用keras内置的模型进行图片预测实例
2021-12-27 17:54:29
![](https://img.aspxhome.com/file/2023/7/99577_0s.jpg)
Python中pycharm编辑器界面风格修改方法
2023-11-04 08:43:42
![](https://img.aspxhome.com/file/2023/3/75263_0s.jpg)
python数据结构算法分析
2022-06-11 02:57:15
![](https://img.aspxhome.com/file/2023/8/72718_0s.png)
python验证码识别教程之灰度处理、二值化、降噪与tesserocr识别
2023-09-07 18:25:35
![](https://img.aspxhome.com/file/2023/2/126912_0s.png)
python树莓派红外反射传感器
2022-12-05 16:11:47
![](https://img.aspxhome.com/file/2023/9/89399_0s.jpg)
安装多个版本的TensorFlow的方法步骤
2022-12-10 00:13:02
![](https://img.aspxhome.com/file/2023/3/93473_0s.png)
python实现的AES双向对称加密解密与用法分析
2022-08-11 00:42:32
python web框架中实现原生分页
2021-05-15 12:25:42
Python当中的array数组对象实例详解
2022-02-12 23:22:47
![](https://img.aspxhome.com/file/2023/8/81708_0s.png)
python解决网站的反爬虫策略总结
2022-11-04 12:57:34
Ubuntu下Python+Flask分分钟搭建自己的服务器教程
2023-11-12 04:37:43
![](https://img.aspxhome.com/file/2023/2/63182_0s.jpg)
科学与艺术兼顾的有效网页视觉设计
2007-09-18 18:03:00
![](https://img.aspxhome.com/file/uploadpic/20079/18/200791818916873.jpg)
使用python tkinter开发一个爬取B站直播弹幕工具的实现代码
2021-06-02 19:15:06
![](https://img.aspxhome.com/file/2023/5/81495_0s.png)
五个简单有效的Python清理数据脚本分享
2021-11-01 08:08:50
![](https://img.aspxhome.com/file/2023/8/124118_0s.jpg)
关于python写入文件自动换行的问题
2021-09-01 00:03:13