py加密小工具
from cryptography.fernet import Fernet
def write_key () :
"""
Generates a key and save it into a file
"""
key = Fernet.generate_key()
with open( "key.key" , "wb" ) as key_file:
key_file.write(key)
def load_key () :
"""
Loads the key from the current directory named `key.key`
"""
return open( "key.key" , "rb" ).read()
#初始化密钥
# write_key()
def jiami(mes):
message=mes.encode()
key = load_key()
f = Fernet(key)
encrypted = f.encrypt(message)
return encrypted.decode()
def jiemi(encrypted):
encrypted=encrypted.encode()
# 解密
key = load_key()
f = Fernet(key)
decrypted_encrypted = f.decrypt(encrypted)
return decrypted_encrypted.decode()
if __name__=="__main__":
mes="xixi"
print(jiami(mes))
本文标题:py加密小工具
本文链接:https://www.haomeiwen.com/subject/cnqpcltx.html
网友评论