1.创作需求
日常生活中我们有很多文件想要保密。这个脚本可以方便大家对所有的文件类型进行加密,解密。最大程度保护我们的隐私。
2.话不多说,直接上代码
from cryptography.fernet import Fernet import docx # 加密 def encrypt_file(filename): # 生成密钥 key = Fernet.generate_key() # print("密钥:", key.decode()) # 加密 fernet = Fernet(key) with open(filename, 'rb') as file: original = file.read() encrypted = fernet.encrypt(original) with open(filename, 'wb') as enc_file: enc_file.write(encrypted) # 密码写入文件,每次都会重写 doc = docx.Document() doc.add_paragraph(str(key)) doc.save('密码.docx') # 解密 # 与加密采用的key值一样 # b''类型 def decrypt_file(filename,key): # 读取密码 fernet = Fernet(key) with open(filename, 'rb') as enc_file: encrypted = enc_file.read() decrypted = fernet.decrypt(encrypted) with open(filename, 'wb') as dec_file: dec_file.write(decrypted)标签:加密,file,Python,encrypted,解密,filename,key,fernet From: https://blog.csdn.net/weixin_55144746/article/details/140816958