【发布时间】:2020-04-10 13:53:05
【问题描述】:
我正在尝试编写一个代码,我可以在其中一次从子目录中解密多条消息。
我创建了一个密钥目录和一个消息目录。密钥目录包含带有包含解密密钥的 .key 文件的子目录。
消息目录包含带有 .key 文件的子目录,其中包含加密的消息。
当我选择一个子目录时,我希望代码能够解密该子目录中的所有消息。目前它只解密所选子目录的一个文件。密钥和加密文件具有相同的名称。
谁能帮我解决这个问题? - 初学者新手程序员
from cryptography.fernet import Fernet
import os
import glob
mainfolderlocation = os.path.dirname(os.path.abspath(__file__))
keyfolderlocation = mainfolderlocation+'\Keys'
messagefolderlocation = mainfolderlocation+'\Messages'
folder = input('Choose folder:\n')
try:
os.chdir(messagefolderlocation+'\{}'.format(folder))
for file in glob.glob('*.key'):
filem = open(file, 'rb')
encrypted = filem.read()
filem.close()
filename= os.path.splitext(file)[0]
os.chdir(keyfolderlocation+'\{}'.format(folder))
for file in glob.glob(filename+'.key'):
filek = open(file, 'rb')
key = filek.read()
filek.close()
f = Fernet(key)
decrypted = f.decrypt(encrypted)
decode=decrypted.decode('utf-8')
print(decode)
except:
print('\nThis folder does not exist!')
【问题讨论】:
-
...谁能帮我解决这个问题?...有什么问题?
-
我希望脚本解密子目录中的所有文件。但目前只选择了子目录中的一个文件,然后它就停止了。
标签: python python-3.x encryption cryptography fernet