美文网首页
py加密小工具

py加密小工具

作者: lk_erzanml | 来源:发表于2021-03-18 16:01 被阅读0次
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