【问题标题】:Decrypting multiple files using cryptography.fernet使用 cryptography.fernet 解密多个文件
【发布时间】: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


【解决方案1】:

您的问题似乎是您的循环有错误。我对 glob 没有太多经验,但您可能想尝试这样的事情:

from cryptography.fernet import Fernet
import os
import glob

mainfolderlocation = os.path.dirname(os.path.abspath(__file__))
keyfolderlocation = mainfolderlocation+'\Keys'
messagefolderlocation = mainfolderlocation+'\Messages' 

files = []

folder = input('Choose folder:\n')
os.chdir(keyfolderlocation+'\{}'.format(folder))    
for file in glob.glob(file+'.key'):
    filek = open(file, 'rb')
    key = filek.read()
    filek.close()

if os.path.exists(folder):
    for r, d, f in os.walk(folder):
        for file in f:
            if '.key' in file:
                files.append(os.path.join(r, file))
else:
    print('\nThis folder does not exist!')

for i in files:
    try:
        filem = open(i, 'rb')
        encrypted = filem.read()
        filem.close()
        filename= os.path.splitext(i)[0]



        f = Fernet(key)
        decrypted = f.decrypt(encrypted)
        decode=decrypted.decode('utf-8')           
        print(decode)

    except:
        print('\nThis folder does not exist!') 

所以基本上发生的事情是你抓住你的密钥(请注意,当前如果你有多个 .key 文件,你会遇到问题),列出文件夹目录中的所有文件(如果存在),然后对于您打开的每个文件,读取内容,解密并打印。我没有机会测试它。

您可能还想考虑将解密放在一个函数中并在每个文件上调用它,而不是让它成为一个整体,但这只是一种风格选择。此外,我将密钥抓取放在 try 块上方,因为您只想抓取一次密钥,否则您将浪费性能将变量设置为相同的值。除非您为每个文件使用不同的密钥文件,在这种情况下,您可能希望为每一对制作字典,因此“bananasandwich.txt”对应于“bananasandwich.key”。祝你好运!

【讨论】:

  • 我认为这可能有效。唯一的问题是未定义变量“文件”。
  • 它在第 13 行之前没有定义,你需要创建一个方法来定义 'file' 在调用它之前作为一个变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 2021-10-26
  • 2016-07-23
相关资源
最近更新 更多