-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit de51066
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import os | ||
import time | ||
from cryptography.fernet import Fernet | ||
from watchdog.observers import Observer | ||
from watchdog.events import FileSystemEventHandler | ||
|
||
class EncryptHandler(FileSystemEventHandler): | ||
def __init__(self, key_path, watch_path, output_path, delete_unencrypted): | ||
self.key_path = key_path | ||
self.watch_path = watch_path | ||
self.output_path = output_path | ||
self.delete_unencrypted = delete_unencrypted | ||
|
||
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: | ||
original = file.read() | ||
|
||
encrypted = fernet.encrypt(original) | ||
|
||
output_file_path = os.path.join(self.output_path, 'enc.' + os.path.basename(event.src_path)) | ||
with open(output_file_path, 'wb') as output_file: | ||
output_file.write(encrypted) | ||
|
||
if self.delete_unencrypted: | ||
os.remove(event.src_path) | ||
|
||
def main(key_path, watch_path, output_path, delete_unencrypted): | ||
event_handler = EncryptHandler(key_path, watch_path, output_path, delete_unencrypted) | ||
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='Encrypt files in a directory.') | ||
parser.add_argument('-k', '--key_path', required=True, help='Path to the encryption 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.') | ||
parser.add_argument('-r', '--delete_unencrypted', action='store_true', help='Delete the original unencrypted file.') | ||
args = parser.parse_args() | ||
|
||
main(args.key_path, args.watch_path, args.output_path, args.delete_unencrypted) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ynjuXt6yjwspTgNy7VmYfi2nMAGaVS8TiH4YBbFFPYU= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from cryptography.fernet import Fernet | ||
|
||
# Generate a key | ||
key = Fernet.generate_key() | ||
|
||
# Use the key to create a Fernet object | ||
cipher_suite = Fernet(key) | ||
|
||
# Write the key to a file | ||
with open('ferret.key', 'wb') as key_file: | ||
key_file.write(key) |