From 8c815a10b1b9a9451e9aef8a9050813dadf035ce Mon Sep 17 00:00:00 2001 From: x011 Date: Mon, 15 Jan 2024 14:58:00 +0000 Subject: [PATCH] Update file_hider.py --- file_hider.py | 138 ++++++++++++++++++++++---------------------------- 1 file changed, 61 insertions(+), 77 deletions(-) diff --git a/file_hider.py b/file_hider.py index e320905..0978390 100644 --- a/file_hider.py +++ b/file_hider.py @@ -10,50 +10,55 @@ import argparse import sys import io +import base64 +import zlib -# Dummy stream to handle write calls when sys.stderr is None class DummyStream(io.StringIO): def write(self, txt): pass -# Check if stderr is None and replace it with the dummy stream if sys.stderr is None: sys.stderr = DummyStream() -# Constants SALT_SIZE = 16 NUM_ITERATIONS = 100000 -KEY_SIZE = 32 # 256 bits for AES-256 +KEY_SIZE = 32 IV_SIZE = 16 NUM_LAYERS = 7 -FILENAME_SIZE = 255 # Maximum filename length +FILENAME_SIZE = 255 -# Function to encrypt data with AES and then encrypt AES key with RSA def encrypt_data(data, public_key_path): with open(public_key_path, 'rb') as f: public_key = RSA.import_key(f.read()) cipher_rsa = PKCS1_OAEP.new(public_key) - session_key = get_random_bytes(16) # AES key length (128 bits) + session_key = get_random_bytes(16) salt = get_random_bytes(SALT_SIZE) key = PBKDF2(session_key, salt, dkLen=KEY_SIZE, count=NUM_ITERATIONS) + data = zlib.compress(data) + for _ in range(NUM_LAYERS): iv = get_random_bytes(IV_SIZE) cipher_aes = AES.new(key, AES.MODE_CBC, iv) data = cipher_aes.encrypt(pad(data, AES.block_size)) - data = iv + data # Prepend IV to the ciphertext for each layer + data = iv + data enc_session_key = cipher_rsa.encrypt(session_key) - return enc_session_key, salt, data + enc_session_key_b64 = base64.b64encode(enc_session_key).decode('utf-8') + salt_b64 = base64.b64encode(salt).decode('utf-8') + data_b64 = base64.b64encode(data).decode('utf-8') + return enc_session_key_b64, salt_b64, data_b64 -# Function to decrypt AES key with RSA and then decrypt data with AES -def decrypt_data(enc_session_key, salt, data, private_key_path, passphrase): +def decrypt_data(enc_session_key_b64, salt_b64, data_b64, private_key_path, passphrase): with open(private_key_path, 'rb') as f: private_key = RSA.import_key(f.read(), passphrase=passphrase) cipher_rsa = PKCS1_OAEP.new(private_key) - session_key = cipher_rsa.decrypt(enc_session_key) + session_key = cipher_rsa.decrypt(base64.b64decode(enc_session_key_b64)) + salt = base64.b64decode(salt_b64) + data = base64.b64decode(data_b64) + key = PBKDF2(session_key, salt, dkLen=KEY_SIZE, count=NUM_ITERATIONS) for _ in range(NUM_LAYERS): @@ -62,21 +67,16 @@ def decrypt_data(enc_session_key, salt, data, private_key_path, passphrase): cipher_aes = AES.new(key, AES.MODE_CBC, iv) data = unpad(cipher_aes.decrypt(data), AES.block_size) + # Decompress the data after decrypting + data = zlib.decompress(data) + return data - - - if __name__ == '__main__': - root = tk.Tk() - root.withdraw() # Hide the root window + root.withdraw() - # Check if any arguments were provided if len(sys.argv) > 1: - - - # Add command line argument parsing parser = argparse.ArgumentParser(description="File Hider") parser.add_argument('--hide', action='store_true', help='Hide and encrypt a file') parser.add_argument('--unhide', action='store_true', help='Decrypt and unhide a file') @@ -86,13 +86,7 @@ def decrypt_data(enc_session_key, salt, data, private_key_path, passphrase): parser.add_argument('--private-key', type=str, help='Path to the private key file') parser.add_argument('--output', type=str, help='Path to save the modified host file or extracted file') parser.add_argument('--passphrase', type=str, help='Passphrase for the private key', default='') - try: - args = parser.parse_args() - except SystemExit: - # When bad arguments are provided, show the help message in a messagebox - error_message = parser.format_help() - messagebox.showerror("Argument Error", error_message) - sys.exit(1) + args = parser.parse_args() if args.hide and args.host and args.file and args.public_key and args.output: try: @@ -102,23 +96,24 @@ def decrypt_data(enc_session_key, salt, data, private_key_path, passphrase): with open(args.file, 'rb') as hidden_file: hidden_data = hidden_file.read() - enc_session_key, salt, encrypted_hidden_data = encrypt_data(hidden_data, args.public_key) + enc_session_key_b64, salt_b64, encrypted_hidden_data_b64 = encrypt_data(hidden_data, args.public_key) full_filename = os.path.basename(args.file).encode('utf-8') - full_filename += b' ' * (FILENAME_SIZE - len(full_filename)) + full_filename_b64 = base64.b64encode(full_filename).decode('utf-8') + full_filename_b64 += ' ' * (FILENAME_SIZE - len(full_filename_b64)) with open(args.output, 'wb') as output_file: output_file.write(host_data) - output_file.write(enc_session_key) - output_file.write(salt) - output_file.write(full_filename) - output_file.write(encrypted_hidden_data) - output_file.write(struct.pack('