-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencNotepad.py
254 lines (224 loc) · 9.63 KB
/
encNotepad.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import tkinter as tk
from tkinter import filedialog, simpledialog, messagebox, font
from cryptography.fernet import Fernet, InvalidToken
import base64
import os
import json
import subprocess
import platform
import rethyxyz.rethyxyz
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
PROGRAM_TITLE = "encNotepad"
LAST_FILE_PATH = "last_file.json"
IMAGE_EXTENSIONS = (".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico")
VIDEO_EXTENSIONS = (".mp4", ".mkv", ".avi", ".mov", ".flv", ".wmv", ".mpeg")
def generate_key(password: str):
password_bytes = password.encode('utf-8')
salt = b'\xa3\xb8\xce\xd1\x7f\x3e\x9e\x1c\x8e\xd2\xcb\xac\x2d\x8c\x5f\xbc'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
key = base64.urlsafe_b64encode(kdf.derive(password_bytes))
return key
def update_status_bar(filename="", content=""):
if filename:
base_filename = os.path.basename(filename)
else:
base_filename = "New File"
words = len(content.split())
characters = len(content)
status_bar.config(text=f"{base_filename} - Words: {words}, Characters: {characters}")
def encrypt_text(text: str, password: str):
key = generate_key(password)
cipher = Fernet(key)
encrypted_text = cipher.encrypt(text.encode('utf-8'))
return encrypted_text
def decrypt_text(encrypted_text: bytes, password: str):
try:
key = generate_key(password)
cipher = Fernet(key)
decrypted_text = cipher.decrypt(encrypted_text).decode('utf-8')
return decrypted_text
except InvalidToken:
return None
def save_last_file_path(filename):
with open(LAST_FILE_PATH, 'w') as file:
json.dump({'last_file': filename}, file)
def get_last_file_path():
if os.path.exists(LAST_FILE_PATH):
with open(LAST_FILE_PATH, 'r') as file:
data = json.load(file)
return data.get('last_file')
return None
def open_last_file():
last_file = get_last_file_path()
if last_file and os.path.exists(last_file):
open_file(root.password, last_file)
def save_file(content: str, password: str, filename=None):
if not filename:
filename = filedialog.asksaveasfilename(defaultextension=".encNotepad", filetypes=[("encNotepad files", "*.encNotepad"), ("All files", "*.*")])
if not filename:
return # Ensure that we exit if no filename is selected
encrypted_content = encrypt_text(content, password)
with open(filename, 'wb') as file:
file.write(encrypted_content)
update_title(filename)
save_last_file_path(filename)
messagebox.showinfo("Success", "Your note was saved successfully.")
root.filename = filename # Set the filename on the root object
root.password = password # Set the password on the root object
return filename
def open_file(password: str, filename=""):
if not filename:
filename = filedialog.askopenfilename(filetypes=[("encNotepad files", "*.encNotepad"), ("All files", "*.*")])
if not filename:
return None, None
with open(filename, 'rb') as file:
content = file.read()
# Check if the file is encrypted
decrypted_content = decrypt_text(content, password)
if decrypted_content is not None:
text_area.delete("1.0", "end")
text_area.insert("1.0", decrypted_content)
text_area.edit_reset() # Reset the undo history
update_title(filename)
save_last_file_path(filename)
root.filename = filename # Ensure the filename is set on the root object
root.password = password # Ensure the password is set on the root object
return filename, password
else:
# If decryption fails, check if it's plain text
try:
content = content.decode('utf-8')
convert = messagebox.askyesno("Plain Text File Detected", "The selected file is plain text. Do you want to encrypt it?")
if convert:
text_area.delete("1.0", "end")
text_area.insert("1.0", content)
text_area.edit_reset() # Reset the undo history
new_password = get_password(False)
if new_password:
save_file(text_area.get("1.0", "end-1c"), new_password, filename)
root.filename = filename
root.password = new_password
return filename, new_password
except UnicodeDecodeError:
messagebox.showerror("Error", "Invalid password or corrupted file.")
return None, None
def update_title(filename):
if filename:
root.title(f"{os.path.basename(filename)} - {PROGRAM_TITLE}")
else:
root.title(f"New File - {PROGRAM_TITLE}")
def get_password(reuse_password=True):
if reuse_password and hasattr(root, 'password') and root.password:
use_old = messagebox.askyesno("Password", "Do you want to use the last used password?")
if use_old:
return root.password
return simpledialog.askstring("Password", "Enter a password for encryption:", show='*')
def new_note():
password = get_password(False)
if password:
filename = save_file(text_area.get("1.0", "end-1c"), password)
if filename:
root.filename = filename
root.password = password
def open_note():
password = get_password()
if password:
filename, password = open_file(password)
if filename:
root.filename = filename
root.password = password
def save_current_note():
if hasattr(root, 'filename') and root.filename and hasattr(root, 'password') and root.password:
save_file(text_area.get("1.0", "end-1c"), root.password, root.filename)
else:
messagebox.showerror("Error", "No file is currently open or password is not set.")
def adjust_font_size(event):
current_size = text_font.actual("size")
if event.delta > 0 or event.num == 4:
new_size = current_size + 1
else:
new_size = current_size - 1 if current_size > 1 else 1
text_font.configure(size=new_size)
def normalize_smb_path(path):
if path.startswith("//"):
return "\\" + path[1:].replace("/", "\\")
return path
def open_local_path(event):
start, end = text_area.tag_prevrange("highlight", "insert")
path = text_area.get(start, end).strip()
path = normalize_smb_path(path) # Normalize SMB path
if os.path.exists(path):
if os.path.isdir(path):
open_path(path)
elif os.path.isfile(path) and (path.lower().endswith(IMAGE_EXTENSIONS) or path.lower().endswith(VIDEO_EXTENSIONS)):
open_path(path)
def open_path(path):
if platform.system() == "Windows":
os.startfile(path)
elif platform.system() == "Darwin": # macOS
subprocess.call(["open", path])
else: # Linux
subprocess.call(["xdg-open", path])
def create_gui():
global text_area, root, text_font, status_bar
rethyxyz.rethyxyz.show_intro(PROGRAM_TITLE)
root = tk.Tk()
root.title(PROGRAM_TITLE)
root.geometry("800x600")
root.filename = None
root.password = None
# Resolve the icon path
icon_path = os.path.abspath('encNotepad.ico')
if os.path.isfile(icon_path):
try:
root.iconbitmap(icon_path)
except Exception as e:
print(f"Failed to set icon: {e}")
else:
print(f"Icon file not found at path: {icon_path}")
text_font = font.Font(family="Lucida Console", size=10)
background_color = "#333333"
text_color = "#FFFFFF"
menu_color = "#555555"
menu_text_color = "#FFFFFF"
text_area = tk.Text(root, font=text_font, bg=background_color, fg=text_color, insertbackground=text_color, undo=True, maxundo=100) # Enable undo and set maxundo
text_area.pack(fill=tk.BOTH, expand=True, side=tk.TOP)
text_area.edit_reset() # Initialize undo history
text_area.bind("<Control-s>", lambda event: save_current_note())
text_area.bind("<Control-o>", lambda event: open_note())
text_area.bind("<<Modified>>", lambda event: update_status_bar(root.filename, text_area.get("1.0", "end-1c")))
text_area.bind("<Control-z>", lambda event: text_area.edit_undo()) # Bind Ctrl+Z to undo
text_area.bind("<Control-y>", lambda event: text_area.edit_redo()) # Bind Ctrl+Y to redo
root.bind("<Control-MouseWheel>", adjust_font_size)
root.bind("<Control-Button-1>", adjust_font_size) # For Linux
root.bind("<Control-Button-4>", adjust_font_size)
root.bind("<Control-Button-5>", adjust_font_size)
menu_bar = tk.Menu(root, bg=menu_color, fg=menu_text_color)
file_menu = tk.Menu(menu_bar, tearoff=0, bg=menu_color, fg=menu_text_color)
file_menu.add_command(label="New", command=new_note)
file_menu.add_command(label="Open", command=open_note)
file_menu.add_command(label="Save", command=save_current_note)
menu_bar.add_cascade(label="File", menu=file_menu)
root.config(menu=menu_bar)
status_bar = tk.Label(root, text="Ready", bd=1, relief=tk.SUNKEN, anchor=tk.W, bg=menu_color, fg=menu_text_color)
status_bar.pack(fill=tk.X, side=tk.BOTTOM)
# Check for last file and prompt
last_file = get_last_file_path()
if last_file and os.path.exists(last_file):
if messagebox.askyesno("Open Last File", f"Do you want to open the last edited file?\n{last_file}"):
root.filename = last_file
password = get_password()
if password:
open_file(password, last_file)
root.config(bg=background_color)
root.mainloop()
if __name__ == "__main__":
create_gui()