From 70312a73ff2c8472db002f115cf3aab0877839d3 Mon Sep 17 00:00:00 2001 From: P7MJ Date: Wed, 6 May 2026 09:39:32 -0400 Subject: [PATCH] Experimental p7c, file finding errors. Starting point for testing Full Disk Encryption Ant --- bugpy-mos-0.py | 5 +- config/pointerfile.txt | 4 + encrypted.p7c_enc | 1 + scripts/help/help.py | 1 + scripts/p7c/__init__.py | 0 scripts/p7c/p7c.py | 172 ++++++++++++++++++++++++++++++++++++++++ what_next?.txt | 2 + 7 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 encrypted.p7c_enc create mode 100644 scripts/p7c/__init__.py create mode 100644 scripts/p7c/p7c.py create mode 100644 what_next?.txt diff --git a/bugpy-mos-0.py b/bugpy-mos-0.py index f9a37c5..4f38ad5 100755 --- a/bugpy-mos-0.py +++ b/bugpy-mos-0.py @@ -8,12 +8,13 @@ from scripts.help import help from scripts.exit import exit from scripts.make_directory import make_directory from scripts.color_print import color_print +from scripts.p7c import p7c from pathlib import Path # Shitty test # list_files.main() -if_verbose = 0 +if_verbose = 1 def verbose(strings): if if_verbose == 1: @@ -59,6 +60,8 @@ def match_command(command, args_list): exit.main(args_list) elif command == "make_directory": make_directory.main(args_list) + elif command == "p7c": + p7c.main(args_list) # use if else if else if else if to match the output def cmdrun(keyword): diff --git a/config/pointerfile.txt b/config/pointerfile.txt index 5b4c908..422a643 100755 --- a/config/pointerfile.txt +++ b/config/pointerfile.txt @@ -29,3 +29,7 @@ shutdown: exit mkdir: make_directory make_directory: make_directory + +p7c: p7c +pcs: p7c +encrypt: p7c diff --git a/encrypted.p7c_enc b/encrypted.p7c_enc new file mode 100644 index 0000000..ae1d244 --- /dev/null +++ b/encrypted.p7c_enc @@ -0,0 +1 @@ + ee±©Ö&ÉêÒŠX çgAAAAABp-fYQKtq4Qba0XU_5lFPXCzQ47spDAsRrWs83RZHHnGev4rWUuwKgbHDO07G3vCGBI6IoPNQ4Zc4zAyXJh0qerGq8wyt_qW88bnB_aGGXGExJdpNE6vZByGpo3oeKshvwaopBF9OaiYH_4Z-2h2pxFHGf-rEY0IhVOoV2lb1hgJwHhHsZve6m_goBAQwLexH1g-K0MSzSK1t7pbhgNKMxfMPhNfmyrwKtUFvQEX5jCGyKgj6MJU6C0pFAIs6Ks4Txdn_uyCRDTZufxGzfUseo2AS9hEbqt94nNaCsxaqnh850PEo5DWLqmwiE-uUylwjvb5WjUVPVdoQSTxSb0x4_JUFkLPVSc7yIcd5okSIbJotMFToTmo1ItD-OhxI-F9W9jAHXs4MJmjyrW13MeY5ywzLLCw== \ No newline at end of file diff --git a/scripts/help/help.py b/scripts/help/help.py index 0468cec..e4ffed5 100755 --- a/scripts/help/help.py +++ b/scripts/help/help.py @@ -6,6 +6,7 @@ def main(args): # Entries color_print.cprint("pointerfile.txt", "GREEN", sameline=True); print(": command aliases") + color_print.cprint("p7c", "DARKRED", sameline=True); print(": experimental encryption program. Will not work.") # Newline diff --git a/scripts/p7c/__init__.py b/scripts/p7c/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/scripts/p7c/p7c.py b/scripts/p7c/p7c.py new file mode 100644 index 0000000..5ddcb71 --- /dev/null +++ b/scripts/p7c/p7c.py @@ -0,0 +1,172 @@ +import os +import getpass +import sys +from cryptography.fernet import Fernet +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC +from pathlib import Path +import base64 + +# EXPERIMENTAL + +def main(args): + # TODO Add option to write and read keyfile, where there is an option to store the salt! + + # Encrypting data + def encrypt_data(): + + # Pesterish file asker + while True: + file_name = input("\nEnter the name of a zip file, without the extension that is in the same directory as this script. \nE.g. for encrypt.zip you would type \"encrypt\". Name > ") + ".zip" + if Path(file_name).is_file(): + break + else: + print("This zip file does not exist!") + + file_chosen_name = input("\nMake a name for your encrypted file, e.g. encrypted (press enter for default)\nName > ") + if file_chosen_name == "": + file_chosen_name = "encrypted" + + keyfile = input("\nDo you want enable keyfile encryption?\nIf enabled, decryption is only possible with the file.[y]/[N] ") + if keyfile == "y" or keyfile == "Y": + print("Keyfile enabled.") + enable_keyfile = True + else: + print("Keyfile disabled.") + enable_keyfile = False + + # Pestering password asker until you confirm it + while True: + password = getpass.getpass("\nCreating password > ") + confirm = input(f"Did you type the correct password, and remember it? [Y]/[n] ") + if confirm == "Y" or confirm == "y" or confirm == "": + password = password.encode() + break + + # 1. Setup Salt + salt = os.urandom(16) + + # 2. Derive Key + kdf = PBKDF2HMAC( + algorithm=hashes.SHA256(), + length=32, + salt=salt, + iterations=480000, + ) + key = base64.urlsafe_b64encode(kdf.derive(password)) + f = Fernet(key) + + print("Encrypting...", end = " ") + try: + # 3. Encrypt and save SALT + DATA together + with open(file_name, 'rb') as file: + original_data = file.read() + + encrypted_data = f.encrypt(original_data) + + if not enable_keyfile: + with open(f"{file_chosen_name}.p7c_enc", 'wb') as file: + # Write the 16-byte salt first, then the encrypted data + file.write(salt) + file.write(encrypted_data) + + elif enable_keyfile: + with open(f"key.p7c_key", 'wb') as key: + key.write(salt) + + with open(f"{file_chosen_name}.p7c_enc", 'wb') as file: + file.write(encrypted_data) + else: + print("What the f** did you do something to enable_keyfile?") + + print("Encrypted! ") + delete_original = input("Delete the original zip file for security? [Y]/[n] ") + if delete_original == "Y" or delete_original == "y" or delete_original == "": + os.remove(f"{file_name}") + print("Original file removed.") + except Exception as e: + print(f"Error! {e}") + + # Decrypting data + def decrypt_data(): + # 1. Get the password from the user + while True: + choose_file = input("\nName of the p7c_enc file without extension, e.g. \"encrypted\"\nName > ") + ".p7c_enc" + if Path(choose_file).is_file(): + break + else: + print("This p7c_enc file does not exist!") + + key_exists = input("Do you have a p7c_key keyfile? [y]/[N] ") + if key_exists == "y" or key_exists == "Y": + while True: + input("Double check if your key is in the same direcory as this code and your encrypted file.\nIt must be named key.p7c_key. Press ENTER to check.") + if Path("key.p7c_key").is_file(): + break + else: + print("Does not exist!") + there_is_a_key = True + else: + there_is_a_key = False + + password = getpass.getpass("Password > ").encode() + + # 2. Open the encrypted file and extract the salt + data + if there_is_a_key: + with open("key.p7c_key", 'rb') as file: + file_salt = file.read(16) + with open(choose_file, 'rb') as file: + encrypted_data = file.read() + + elif not there_is_a_key: + with open(choose_file, 'rb') as file: + # Read exactly 16 bytes for the salt + file_salt = file.read(16) + # Read everything else as the encrypted data + encrypted_data = file.read() + else: + print("WHAT DID YOU DO TO THERE_IS_A_KEY?!") + + # 3. Re-derive the EXACT same key using that salt + kdf = PBKDF2HMAC( + algorithm=hashes.SHA256(), + length=32, + salt=file_salt, + iterations=480000, + ) + key = base64.urlsafe_b64encode(kdf.derive(password)) + f = Fernet(key) + + # 4. Decrypt and save the original file + print("Decrypting...", end = " ") + try: + decrypted_data = f.decrypt(encrypted_data) + + with open('unencrypted.zip', 'wb') as file: + file.write(decrypted_data) + print("Success.") + delete_encrypted = input("\nDelete the encrypted file (and key, if exists)? [Y]/[n] ") + if delete_encrypted == "Y" or delete_encrypted == "y" or delete_encrypted == "": + os.remove(choose_file) + if there_is_a_key: + os.remove("key.p7c_key") + print("Key removed.") + print("Encrypted file removed.") + + + except Exception as e: + print(f"Could not decrypt!.\nThis might be due to a wrong file or corrupted data?\nDetailed info:\n{e}.") + + # Main function + + while True: + print("\nPCS - P7MJ's enCryption System | V A-2-i Dividend | P7MJ") + choice = input("[1] Encrypt [2] Decrypt [x] Exit > ") + if choice == "1": + encrypt_data() + elif choice == "2": + decrypt_data() + elif choice == "x": + break + else: + print("Not an option!!!") diff --git a/what_next?.txt b/what_next?.txt new file mode 100644 index 0000000..4eab119 --- /dev/null +++ b/what_next?.txt @@ -0,0 +1,2 @@ +Make a master encryption program that handles the launch process, disk encryption, and others +Support partial encryption (everything in the home folder) and full encryption (everything) \ No newline at end of file