# -*- coding: utf-8 -*-标签:加密,encrypted,密码,cipher,key,print,message,password From: https://www.cnblogs.com/mayueyue/p/17720023.html
"""
@Time : 9/13/23 3:37 PM
@Author :
"""
import hashlib
"""def hash_password(password):
# 创建md5对象
hasher = hashlib.md5()
# 更新md5对象,需要使用字节类型
hasher.update(password.encode())
# 获取十六进制哈希值
hex_dig = hasher.hexdigest()
return hex_dig
password = "my_password"
hashed_password = hash_password(password)
print(f"Hashed password: {hashed_password}")"""
from cryptography.fernet import Fernet
# 首先,生成一个密钥
key = Fernet.generate_key()
# 创建一个cipher对象
cipher = Fernet(key)
# 原始消息
message = b"this is a secret message"
# 加密消息
encrypted_message = cipher.encrypt(message)
print(f"Encrypted: {encrypted_message}")
# 解密消息
decrypted_message = cipher.decrypt(encrypted_message)
print(f"Decrypted: {decrypted_message}")