python 加密和解密
程序员文章站
2024-03-14 15:34:16
...
python 加密和解密
安装
linux
pip install pycryptodome
windows
pip3 install pycryptodomex
创建**
from Cryptodome.Random import get_random_bytes
def creat_key():
"""
随机**
:return: **16位
"""
key = get_random_bytes(16)
return key
if __name__ == '__main__':
key = creat_key()
print("key:", key)
# 16位**也可以自己指定
# key = b'1234567890123456'
加密
from Cryptodome.Cipher import AES
def encrypt_data(key,data,text_path):
"""
数据加密到文件
:param key:**
:param data:加密数据
:param text_path:数据加密后写入的文件路径
:return:
"""
cipher = AES.new(key, AES.MODE_EAX)
data = data.encode("utf-8")
ciphertext, tag = cipher.encrypt_and_digest(data)
file_out = open(text_path, "wb")
[ file_out.write(x) for x in
上一篇: springboot 自定义效验规则