Untitled

mail@pastecode.io avatar
unknown
python
a year ago
5.6 kB
2
Indexable
Never
import tkinter as tk
from tkinter import filedialog
from cryptography.fernet import Fernet


class EncryptPad(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.master.title("EncryptPad")
        self.pack(fill=tk.BOTH, expand=1)

        main_frame = tk.Frame(self)
        main_frame.pack(fill=tk.BOTH, expand=1)

        self.textarea = tk.Text(main_frame, undo=True, bg='black', fg='#00ff00', font=('Courier', 14))
        self.textarea.pack(fill=tk.BOTH, expand=1)

        button_frame = tk.Frame(main_frame)
        button_frame.pack(side=tk.TOP, fill=tk.X)

        save_button = tk.Button(button_frame, text="Save", command=self.save_file)
        save_button.pack(side=tk.LEFT, padx=10, pady=5)

        load_button = tk.Button(button_frame, text="Load", command=self.open_file)
        load_button.pack(side=tk.LEFT, padx=10, pady=5)

        key_frame = tk.Frame(main_frame)
        key_frame.pack(side=tk.TOP, fill=tk.X)

        self.key_var = tk.StringVar()
        self.key_var.set("vyJpTGa-ebn9GJdKvzY1I_RQ2n0xMxWQf_wLPBkC-zc=")
        key_label = tk.Label(key_frame, text="Key:")

        key_button_frame = tk.Frame(key_frame)
        key_button_frame.pack(side=tk.RIGHT, padx=5, pady=5)

        new_key_frame = tk.Frame(key_frame)
        new_key_frame.pack(side=tk.RIGHT, padx=2, pady=5)

        key_label_frame = tk.Frame(main_frame)
        key_label_frame.pack(side=tk.BOTTOM, fill=tk.X)

        self.key_label_var = tk.StringVar()
        self.key_label_var.set("Encryption Key: " + self.key_var.get())
        self.key_label = tk.Label(key_label_frame, textvariable=self.key_label_var)
        self.key_label.pack(side=tk.LEFT, padx=5)

        encrypt_button = tk.Button(button_frame, text="Encrypt", command=self.encrypt_file)
        encrypt_button.pack(side=tk.LEFT, padx=10, pady=5)

        decrypt_button = tk.Button(button_frame, text="Decrypt", command=self.decrypt_file)
        decrypt_button.pack(side=tk.LEFT, padx=10, pady=5)

        buttonx = tk.Button(self, text="Advanced", command=self.advanced_settings)
        buttonx.pack(side=tk.RIGHT, padx=5, pady=5)

    def save_file(self):
        file_path = filedialog.asksaveasfilename(defaultextension=".txt",
                                                 filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
        if file_path:
            with open(file_path, "w") as file:
                text = self.textarea.get(1.0, tk.END)
                file.write(text)

    def open_file(self):
        file_path = filedialog.askopenfilename(defaultextension=".txt",
                                               filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
        if file_path:
            with open(file_path, "r") as file:
                text = file.read()
                self.textarea.delete(1.0, tk.END)
                self.textarea.insert(tk.END, text)

    def advanced_settings(self):
        Popup()
        print('New PopUp')

    def encrypt_file(self):
        key = self.key_var.get().encode()
        f = Fernet(key)
        encrypted_text = f.encrypt(self.textarea.get(1.0, tk.END).encode())
        self.textarea.delete(1.0, tk.END)
        self.textarea.insert(tk.END, encrypted_text.decode())

    def decrypt_file(self):
        key = self.key_var.get().encode()
        f = Fernet(key)
        decrypted_text = f.decrypt(self.textarea.get(1.0, tk.END).encode())
        self.textarea.delete(1.0, tk.END)
        self.textarea.insert(tk.END, decrypted_text.decode())


class Popup(tk.Toplevel):
    def __init__(self, master=None):
        super().__init__(master)
        self.title("Advanced Settings")

        # Create a label with instructions
        label = tk.Label(self, text='''These settings are for advanced users, 
                typically there is a secure key that you do not need to reset, but that is less secure.
                If you do decide to create a custom key with generate, it is imperative for you to write it down 
                or put it somewhere secure.''')
        label.pack(side=tk.TOP, padx=5, pady=5)

        key_frame2 = tk.Frame(self)
        key_frame2.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)

        self.key_var = tk.StringVar()
        self.key_var.set("vyJpTGa-ebn9GJdKvzY1I_RQ2n0xMxWQf_wLPBkC-zc=")
        key_label = tk.Label(key_frame2, text="Key:")
        key_label.pack(side=tk.LEFT, padx=5)
        self.key_entry = tk.Entry(key_frame2, textvariable=self.key_var, width=50)
        self.key_entry.pack(side=tk.LEFT, padx=5)

        button = tk.Button(key_frame2, text="Generate Key", command=self.generate_key)
        button.pack(side=tk.RIGHT, padx=5, pady=5)

        button = tk.Button(key_frame2, text="EXIT WINDOW", command=self.ok_button)
        button.pack(side=tk.RIGHT, padx=5, pady=5)

    def ok_button(self):
        self.key = self.key_entry.get()
        self.destroy()

        # def generate_key(self):
        #    key = Fernet.generate_key()
        #    self.key_var.set(key.decode())
        #     self.key_label_var.set("Encryption Key: " + self.key_var.get())

    def generate_key(self):
        key = Fernet.generate_key()
        self.key_var = tk.StringVar()
        self.key_var.set("vyJpTGa-ebn9GJdKvzY1I_RQ2n0xMxWQf_wLPBkC-zc=")
        self.key_var.set(key.decode())
        self.key_label_var = tk.StringVar()
        self.key_label_var.set("Encryption Key: " + self.key_var.get())


if __name__ == '__main__':
    root = tk.Tk()
    app = EncryptPad(master=root)
    root.geometry("700x600")
    root.mainloop()