from cryptography.fernet import Fernet import os # 生成一个新的Fernet密钥 key = Fernet.generate_key() # 指定密钥文件的路径 key_path = 'secret.key' # 将密钥保存到文件中 with open(key_path, 'wb') as key_file: key_file.write(key) # 打印密钥以便查看 # print(f"Generated key: {key.decode()}") # 确保密钥文件的权限设置为只读 os.chmod(key_path, 0o400) print(f"Key has been saved to {key_path}")