栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 文本加密与解密

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 文本加密与解密

pip install pycryptodome
from Crypto.Cipher import AES
#from pyrsistent import b

# 秘钥和文本都是byte类型
# 拼接文本,文本长度需为16的倍数,不足则拼接空格
def splice(text):
    while len(text) % 16 != 0:
        text += b' '
    return text
# 拼接秘钥,秘钥长度需为16的倍数,不足则拼接空格
def splice_key(key):
    while len(key) % 16 != 0:
        key += b' '
    return key


if __name__ == '__main__':
    key = b'12345'  # 秘钥
    aes = AES.new(splice_key(key), AES.MODE_ECB)  # 根据秘钥初始化加密器
    text = b'jiamiwenben'  # 加密文本

    encrypted_byte = aes.encrypt(splice(text))  # 使用加密器的加密方法对文本进行加密,返回加密结果(byte类型)
    
    import base64
    b64_string = str(base64.b64encode(encrypted_byte),'utf-8') # 将bytes装换成string,base64.b64decode(b64_string) 这句话将string装换成bytes
    print("encrypted_byte: ", encrypted_byte)
    print(b64_string)

    # print(b'')
    # # print (encrypted_byte.decode(encoding='utf-8'))
    # print('x95!xc9x91xc41
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917857.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号