Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bpawnzZ committed Dec 5, 2023
0 parents commit de51066
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
51 changes: 51 additions & 0 deletions decrypt.py
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)
55 changes: 55 additions & 0 deletions encrypt.py
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)
1 change: 1 addition & 0 deletions ferret.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ynjuXt6yjwspTgNy7VmYfi2nMAGaVS8TiH4YBbFFPYU=
11 changes: 11 additions & 0 deletions genKey.py
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)

0 comments on commit de51066

Please sign in to comment.