from cryptography.fernet import Fernet
(1)生成的秘钥,注意保存,第一次
key = Fernet.generate_key()
print(key)
file = open('./key.data', 'wb')
file.write(key)
file.close()
(2)读取秘钥
fr = open("./key.data", 'rb')
chenkey = fr.read()
print(chenkey)
(3)加密文件
fpwd = Fernet(chenkey)
file_orign = open("Win64OpenSSL-3_0_7.msi", 'rb')
file_content = file_orign.read()
print("chenread")
encrypted_content = fpwd.encrypt(file_content)
fsave = open("Win64OpenSSL-3_0_7.msi", 'wb')
fsave.write(encrypted_content)
print("加密 完成")
(4)解密文件
fpwd = Fernet(chenkey)
file_jiemi_orign = open("Win64OpenSSL-3_0_7.msi", 'rb')
file_jiemi_content = bytearray(fpwd.decrypt(file_jiemi_orign.read()))
fsave_jiemi = open("Win64OpenSSL-3_0_7.msi", 'wb')
fsave_jiemi.write(file_jiemi_content)
print("解密 完成")
网友评论