gCryptor Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
import cryptography.fernet
import os

class GCryptorApp:
    def __init__(self, master):
        self.master = master
        master.title("gCryptor: Secure File Encryption")

        self.key = None

        # GUI Elements
        self.file_path_label = ttk.Label(master, text="File Path:")
        self.file_path_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")

        self.file_path_entry = ttk.Entry(master, width=50)
        self.file_path_entry.grid(row=0, column=1, padx=5, pady=5, sticky="we")

        self.browse_button = ttk.Button(master, text="Browse", command=self.browse_file)
        self.browse_button.grid(row=0, column=2, padx=5, pady=5)

        self.encrypt_button = ttk.Button(master, text="Encrypt", command=self.encrypt_file)
        self.encrypt_button.grid(row=1, column=1, padx=5, pady=5)

        self.decrypt_button = ttk.Button(master, text="Decrypt", command=self.decrypt_file)
        self.decrypt_button.grid(row=2, column=1, padx=5, pady=5)

        self.details_button = ttk.Button(master, text="Details", command=self.show_details)
        self.details_button.grid(row=3, column=0, columnspan=3, padx=5, pady=5)

        # Configure grid weights for resizing
        master.columnconfigure(1, weight=1)

    def browse_file(self):
        file_path = filedialog.askopenfilename()
        self.file_path_entry.delete(0, tk.END)
        self.file_path_entry.insert(0, file_path)

    def generate_key(self):
        self.key = cryptography.fernet.Fernet.generate_key()
        return self.key

    def load_key(self): #look for pre-existing key file
        return open("secret.key", "rb").read()

    def write_key(self): #if no key exists, write a new one
        key = cryptography.fernet.Fernet.generate_key()
        with open("secret.key", "wb") as key_file:
            key_file.write(key)

    def encrypt_file(self):
        file_path = self.file_path_entry.get()
        if not file_path:
            self.show_message("Error", "Please select a file.")
            return

        try:
            # Load the key if it exists, otherwise generate and save it
            if os.path.exists("secret.key"):
                key = self.load_key()
            else:
                self.write_key()
                key = self.load_key()
                
            f = cryptography.fernet.Fernet(key)
            with open(file_path, "rb") as file:
                file_data = file.read()

            encrypted_data = f.encrypt(file_data)

            with open(file_path + ".enc", "wb") as file:
                file.write(encrypted_data)

            self.show_message("Success", "File encrypted successfully!")

        except Exception as e:
            self.show_message("Error", f"Encryption failed: {e}")

    def decrypt_file(self):
        file_path = self.file_path_entry.get()
        if not file_path:
            self.show_message("Error", "Please select a file.")
            return

        if not file_path.endswith(".enc"):
            self.show_message("Error", "Please select an encrypted file (.enc).")
            return

        try:
            # Load the key if it exists
            if os.path.exists("secret.key"):
                key = self.load_key()
            else:
                self.show_message("Error", "Encryption key missing. Decryption impossible without the key.")
                return
                
            f = cryptography.fernet.Fernet(key)
            with open(file_path, "rb") as file:
                encrypted_data = file.read()

            decrypted_data = f.decrypt(encrypted_data)

            with open(file_path[:-4], "wb") as file:
                file.write(decrypted_data)

            self.show_message("Success", "File decrypted successfully!")

        except Exception as e:
            self.show_message("Error", f"Decryption failed: {e}")

    def show_message(self, title, message):
        tk.messagebox.showinfo(title, message)

    def show_details(self):
         details_window = tk.Toplevel(self.master)
         details_window.title("gCryptor Details")

         details_text = tk.Text(details_window, wrap=tk.WORD)
         details_text.insert(tk.END, "gCryptor is a file encryption tool designed for ease of use and security.  It uses the Fernet library for symmetric encryption, generating a unique key for each encryption session. The key is stored in a file named 'secret.key' in the same directory as the script.

         Key Features:
         - **File Encryption:** Encrypts any file type using a secure key.
         - **File Decryption:** Decrypts files previously encrypted by gCryptor, provided the 'secret.key' file is available.
         - **User-Friendly Interface:** Simple and intuitive GUI for easy file selection and encryption/decryption operations.
         - **Key Management:** Automatically generates and stores encryption keys.  Ensures key re-use for consistency in encryption and decryption processes.

         Important Notes:
         - The 'secret.key' file is crucial for decryption.  Losing this file will result in permanent data loss.
         - gCryptor encrypts the file in place and appends '.enc' to the filename.  Decryption removes the '.enc' extension.
         - This tool is provided as-is, and the user assumes all responsibility for data security.")
         details_text.config(state=tk.DISABLED) # Make the text read-only
         details_text.pack(padx=10, pady=10)

root = tk.Tk()
app = GCryptorApp(root)
root.mainloop()
👁️ Viewed: 5

Comments