-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
143 lines (114 loc) · 5.35 KB
/
Copy pathgui.py
File metadata and controls
143 lines (114 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""Simple desktop GUI for the ciphertext toolkit."""
from __future__ import annotations
import tkinter as tk
from tkinter import filedialog, messagebox, scrolledtext, ttk
from ciphertext.crypto import CryptoError, decrypt, decrypt_file, encrypt, encrypt_file
class CiphertextApp(tk.Tk):
def __init__(self) -> None:
super().__init__()
self.title("Ciphertext")
self.geometry("760x560")
self.minsize(640, 480)
self._build_ui()
def _build_ui(self) -> None:
container = ttk.Frame(self, padding=16)
container.pack(fill=tk.BOTH, expand=True)
ttk.Label(container, text="Password").pack(anchor=tk.W)
self.password_entry = ttk.Entry(container, show="*")
self.password_entry.pack(fill=tk.X, pady=(4, 12))
notebook = ttk.Notebook(container)
notebook.pack(fill=tk.BOTH, expand=True)
text_tab = ttk.Frame(notebook, padding=8)
file_tab = ttk.Frame(notebook, padding=8)
notebook.add(text_tab, text="Text")
notebook.add(file_tab, text="File")
self._build_text_tab(text_tab)
self._build_file_tab(file_tab)
def _build_text_tab(self, parent: ttk.Frame) -> None:
ttk.Label(parent, text="Input").pack(anchor=tk.W)
self.input_text = scrolledtext.ScrolledText(parent, height=10, wrap=tk.WORD)
self.input_text.pack(fill=tk.BOTH, expand=True, pady=(4, 12))
ttk.Label(parent, text="Output").pack(anchor=tk.W)
self.output_text = scrolledtext.ScrolledText(parent, height=10, wrap=tk.WORD, state=tk.DISABLED)
self.output_text.pack(fill=tk.BOTH, expand=True, pady=(4, 12))
buttons = ttk.Frame(parent)
buttons.pack(fill=tk.X)
ttk.Button(buttons, text="Encrypt", command=self._encrypt_text).pack(side=tk.LEFT)
ttk.Button(buttons, text="Decrypt", command=self._decrypt_text).pack(side=tk.LEFT, padx=8)
ttk.Button(buttons, text="Copy Output", command=self._copy_output).pack(side=tk.LEFT)
def _build_file_tab(self, parent: ttk.Frame) -> None:
path_row = ttk.Frame(parent)
path_row.pack(fill=tk.X, pady=(0, 12))
ttk.Label(path_row, text="Selected file").pack(anchor=tk.W)
self.file_path = tk.StringVar()
ttk.Entry(path_row, textvariable=self.file_path).pack(fill=tk.X, pady=(4, 8), side=tk.LEFT, expand=True)
ttk.Button(path_row, text="Browse", command=self._browse_file).pack(side=tk.LEFT, padx=(8, 0))
buttons = ttk.Frame(parent)
buttons.pack(fill=tk.X)
ttk.Button(buttons, text="Encrypt File", command=self._encrypt_file).pack(side=tk.LEFT)
ttk.Button(buttons, text="Decrypt File", command=self._decrypt_file).pack(side=tk.LEFT, padx=8)
ttk.Label(parent, text="Result").pack(anchor=tk.W, pady=(16, 4))
self.file_result = scrolledtext.ScrolledText(parent, height=12, wrap=tk.WORD, state=tk.DISABLED)
self.file_result.pack(fill=tk.BOTH, expand=True)
def _password(self) -> str:
password = self.password_entry.get()
if not password:
raise CryptoError("Password cannot be empty.")
return password
def _set_output(self, widget: scrolledtext.ScrolledText, value: str) -> None:
widget.configure(state=tk.NORMAL)
widget.delete("1.0", tk.END)
widget.insert(tk.END, value)
widget.configure(state=tk.DISABLED)
def _encrypt_text(self) -> None:
try:
result = encrypt(self.input_text.get("1.0", tk.END).strip(), self._password())
except CryptoError as exc:
messagebox.showerror("Encryption failed", str(exc))
return
self._set_output(self.output_text, result)
def _decrypt_text(self) -> None:
try:
result = decrypt(self.input_text.get("1.0", tk.END).strip(), self._password())
except CryptoError as exc:
messagebox.showerror("Decryption failed", str(exc))
return
self._set_output(self.output_text, result)
def _copy_output(self) -> None:
value = self.output_text.get("1.0", tk.END).strip()
if not value:
return
self.clipboard_clear()
self.clipboard_append(value)
messagebox.showinfo("Copied", "Output copied to clipboard.")
def _browse_file(self) -> None:
path = filedialog.askopenfilename()
if path:
self.file_path.set(path)
def _encrypt_file(self) -> None:
path = self.file_path.get().strip()
if not path:
messagebox.showerror("Missing file", "Choose a file first.")
return
try:
output = encrypt_file(path, self._password())
except CryptoError as exc:
messagebox.showerror("Encryption failed", str(exc))
return
self._set_output(self.file_result, f"Encrypted file created:\n{output}")
def _decrypt_file(self) -> None:
path = self.file_path.get().strip()
if not path:
messagebox.showerror("Missing file", "Choose a file first.")
return
try:
output = decrypt_file(path, self._password())
except CryptoError as exc:
messagebox.showerror("Decryption failed", str(exc))
return
self._set_output(self.file_result, f"Decrypted file created:\n{output}")
def main() -> None:
app = CiphertextApp()
app.mainloop()
if __name__ == "__main__":
main()