python可以编写数据加密解密吗(python简单实现AES加密和解密)
类别:脚本大全 浏览量:238
时间:2021-10-24 10:09:19 python可以编写数据加密解密吗
python简单实现AES加密和解密本文实例为大家分享了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自带的ide的使用(基于python的ini配置文件操作工具类)
- python3.x base64怎么加密解密(python3.x实现base64加密和解密)
- python数据表教程(详解Python sys.argv使用方法)
- pythondjango后台管理(基于腾讯云服务器部署微信小程序后台服务Python+Django)
- python最火开源项目(5月份Github上Python开源项目排行)
- java入坑rabbitmq(Python操作rabbitMQ的示例代码)
- python3html怎么转换成pdf(Python实现html转换为pdf报告生成pdf报告功能示例)
- 如何用python创建单链表(Python3实现的反转单链表算法示例)
- python 的常用工具(Python静态类型检查新工具之pyright 使用指南)
- python解析excel例子(Python玩转Excel的读写改实例)
- python关闭程序强制退出线程(python多线程调用exit无法退出的解决方法)
- ubuntu python3.7安装(详解Ubuntu16.04安装Python3.7及其pip3并切换为默认版本)
- python如何对参数长度进行限制(python 多个参数不为空校验方法)
- pythonflask系列教程(Python安装Flask环境及简单应用示例)
- python怎么从数组中取内容(python调用c++ ctype list传数组或者返回数组的方法)
- 用python编写一个gui(用 Python 构建漂亮的 GUI)
- 越南新娘(越南新娘婚介网站)
- 越南新娘(越南新娘骗婚套路流程)
- 菲律宾游学(菲律宾游学中介机构)
- 菲律宾美食(菲律宾美食排行榜前十名)
- 菲律宾特产(菲律宾特产最值得买回国)
- 越南特产(越南特产首饰)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9