0
votes

I am attempting to use encryption key from my USB stick to decrypt a file on my computer. The output I get from my current script (based on source: https://medium.com/codex/encrypting-your-files-using-a-usb-stick-as-the-key-python-e04b26657357) keeps throwing path errors. It can load the key but somehow doesnt decrypt the file on my computer. Can someone see where this error is coming from or how it can be solved?

I get the following output:

C:\Users\Asus\PycharmProjects\pythonProject220728usbkey\venv\Scripts\python.exe C:/Users/Asus/PycharmProjects/pythonProject220728usbkey/main.py
Trying to find key...
Key Found
Traceback (most recent call last):
  File "C:\Users\Asus\PycharmProjects\pythonProject220728usbkey\main.py", line 68, in <module>
    encryptFiles(key,files)
  File "C:\Users\Asus\PycharmProjects\pythonProject220728usbkey\main.py", line 30, in encryptFiles
    files = os.listdir(directory)
NotADirectoryError: [WinError 267] El nombre del directorio no es vĂ¡lido: 'C:\\Users\\Asus\\PycharmProjects\\pythonProject220728usbkey\\enc_levensonderhoud.xlsx'

Process finished with exit code 1

-------------------------------------------------------------------------------------#

#See my current script below:

import os
import wmi
from cryptography.fernet import Fernet

my_key = 'TICOZM'
files = r'C:\Users\Asus\PycharmProjects\pythonProject220728usbkey\enc_levensonderhoud.xlsx'

c = wmi.WMI()

def check_for_key():
   for disk in c.Win32_LogicalDisk():
      if disk.VolumeName==my_key:
         return disk

def load_key(usbDisk):
   port = usbDisk.DeviceID
   try:
      print('Trying to find key...')
      with open(f'{port}\\encryptionKey.key','rb') as encryptKey:
         key = encryptKey.read()
         print('Key Found')
   except:
      print('Key not found... Creating a new key')
      key = Fernet.generate_key()
      with open(f'{port}\\encryptionKey.key','wb') as encryptKey:
         encryptKey.write(key)
   return key

def encryptFiles(key,directory):
   files = os.listdir(directory)
   cipher = Fernet(key)
   global state
   state = 'encrypted'
   for file in files:
      with open(f'{directory}\{file}','rb') as old:
         original = old.read()
      encrypted = cipher.encrypt(original)
      with open(f'{directory}\{file}','wb') as old:
         old.write(encrypted)

   def decryptFiles(key, directory):
      files = os.listdir(directory)
      cipher = Fernet(key)
      global state
      state = 'decrypted'
      for file in files:
         with open(f'{directory}\{file}', 'rb') as old:
            encrypted = old.read()
         decrypted = cipher.decrypt(encrypted)
         with open(f'{directory}\{file}', 'wb') as old:
            old.write(decrypted)

state = 'decrypted'
if __name__=='__main__':
   while True:
      disk = check_for_key()
      try:
         key = load_key(disk)
      except:
         print('No Key Available')
      if disk!=None:
         current_state = 'decrypted'
         if current_state!=state:
            decryptFiles(key,files)
         else:
            current_state = 'encrypted'
            if current_state!=state:
               encryptFiles(key,files)