-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.py
51 lines (41 loc) · 1.76 KB
/
decrypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# decrypt.py
import os
import time
from cryptography.fernet import Fernet
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class DecryptHandler(FileSystemEventHandler):
def __init__(self, key_path, watch_path, output_path):
self.key_path = key_path
self.watch_path = watch_path
self.output_path = output_path
def on_created(self, event):
if not event.is_directory:
with open(self.key_path, 'rb') as key_file:
key = key_file.read()
fernet = Fernet(key)
with open(event.src_path, 'rb') as file:
encrypted = file.read()
decrypted = fernet.decrypt(encrypted)
output_file_path = os.path.join(self.output_path, os.path.basename(event.src_path)[4:])
with open(output_file_path, 'wb') as output_file:
output_file.write(decrypted)
def main(key_path, watch_path, output_path):
event_handler = DecryptHandler(key_path, watch_path, output_path)
observer = Observer()
observer.schedule(event_handler, watch_path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Decrypt files in a directory.')
parser.add_argument('-k', '--key_path', required=True, help='Path to the decryption key.')
parser.add_argument('-d', '--watch_path', required=True, help='Path to the directory to watch.')
parser.add_argument('-o', '--output_path', required=True, help='Path to the output directory.')
args = parser.parse_args()
main(args.key_path, args.watch_path, args.output_path)