博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现 base64+gzip+AES-ECB加密解密
阅读量:7030 次
发布时间:2019-06-28

本文共 1418 字,大约阅读时间需要 4 分钟。

实现base64+gzip+AES-ECB加密解密

# 本代码基于python3.6.1测试通过import base64from Crypto.Cipher import AESfrom io import StringIOimport gzip'''采用AES对称加密算法'''# str不是16的倍数那就补足为16的倍数def add_to_16(value):    while len(value) % 16 != 0:        value += '\0'    return str.encode(value)  # 返回bytes# 加密方法def encrypt_oracle():    # 秘钥    key = 'jiayanmiyao'    # 待加密文本    text = 'testing'    # 初始化加密器    aes = AES.new(add_to_16(key), AES.MODE_ECB)    # 先进行aes加密    encrypt_aes = aes.encrypt(add_to_16(text))    # 用base64转成字符串形式    encrypted_text = base64.encodebytes(encrypt_aes)  # 执行加密并转码返回bytes  # str(encoding=utf-8)        #gzip压缩    en_gzip = gzip.compress(encrypted_text)        #base64编码    en_base64 = base64.b64encode(en_gzip)    return en_base64# 解密方法def decrypt_oralce(text):    # 秘钥    key = 'jiayanmiyao'    # 密文   # base64解码    de_base64 = base64.b64decode(text)   # gzip解压    de_gzip = gzip.decompress(de_base64)    text = str(de_gzip, encoding="utf-8")    # 初始化加密器    aes = AES.new(add_to_16(key), AES.MODE_ECB)    # 优先逆向解密base64成bytes    base64_decrypted = base64.decodebytes(text.encode(encoding='utf-8'))    # 执行解密密并转码返回str    decrypted_text = str(aes.decrypt(base64_decrypted), encoding='utf-8').replace('\0', '')    return decrypted_textif __name__ == '__main__':    en_result = encrypt_oracle()    print(en_result)    de_result = decrypt_oralce(en_result)    print(de_result)

转载于:https://blog.51cto.com/14066389/2315161

你可能感兴趣的文章
Docker容器查看ip地址
查看>>
在PC端或移动端应用中接入商业QQ
查看>>
将python3.6软件的py文件打包成exe程序
查看>>
DataTable 排序
查看>>
大白话5分钟带你走进人工智能-第二十节逻辑回归和Softmax多分类问题(5)
查看>>
嵌入式系统在工业控制中的应用
查看>>
使用httpclient异步调用WebAPI接口
查看>>
c++ 类的对象与指针
查看>>
SSTI(模板注入)
查看>>
rbac models
查看>>
[2615]传纸条 sdutOJ
查看>>
类图标注的使用范例
查看>>
NumberFormat注解 DateTimeFormat
查看>>
[转载]PV操作简单理解
查看>>
Acm Dima and Lisa的题解
查看>>
深入浅出Tomcat系列
查看>>
从网页提取的关键字
查看>>
位运算符
查看>>
PHP str_replace() 和str_ireplace()函数
查看>>
什么是全栈工程师
查看>>