cryptography库

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("解密 完成")

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容