-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecNotes.py
185 lines (160 loc) · 7.63 KB
/
secNotes.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
'''
@author Mqtth3w https://github.com/mqtth3w
@license GPL-3.0
'''
from Crypto.Cipher import AES
from hashlib import sha3_512
import ctypes
import os
import tkinter as tk
from tkinter import scrolledtext
from tkinter import filedialog, messagebox
key_len = 32
iv_len = 16
mic_len = 128
def pad(data: bytes, blockSize: int) -> bytes:
padLen = blockSize - (len(data) % blockSize)
return data + (bytes([padLen]) * padLen)
def unpad(data: bytes, blockSize: int) -> bytes:
return data[:-ord(data[-1:])]
def encrypt_AES256(KEY: bytes, IV: bytes, plaintext: str) -> bytes:
plaintext = plaintext.encode()
cipher = AES.new(KEY, AES.MODE_CBC, IV)
return cipher.encrypt(pad(plaintext, AES.block_size))
def decrypt_AES256(KEY: bytes, IV: bytes, ciphertext: bytes) -> str:
cipher = AES.new(KEY, AES.MODE_CBC, IV)
plaintext = cipher.decrypt(ciphertext)
return unpad(plaintext, AES.block_size).decode()
def calculate_sha3_512(input_string: str) -> str:
sha3_512_hash = sha3_512()
sha3_512_hash.update(input_string.encode('utf-8'))
return sha3_512_hash.hexdigest()
def clear_data(data: bytearray):
ctypes.memset(ctypes.addressof(ctypes.c_char.from_buffer(data)), 0, len(data))
data.clear()
def encrypt(aesKey_entry, iv_entry, hashKey_entry, file, textbox, checklab):
aesKey = bytearray(aesKey_entry.get(), 'utf-8')
iv = bytearray(iv_entry.get(), 'utf-8')
hashKey = bytearray(hashKey_entry.get(), 'utf-8')
text = textbox.get(1.0, tk.END)
if len(aesKey) != key_len:
messagebox.showerror("Error", "AES-256 key must be 32 bytes long.")
elif len(iv) != iv_len:
messagebox.showerror("Error", "IV (CBC) must be 16 bytes long.")
elif not hashKey:
messagebox.showerror("Error", "Checksum key textbox cannot be empty.")
elif not text.strip():
messagebox.showerror("Error", "Text textbox cannot be empty.")
elif not file:
messagebox.showerror("Error", "File textbox cannot be empty.")
elif not os.path.isfile(file):
messagebox.showerror("Error", "File must exists.")
else:
aesKey_entry.delete(0, tk.END)
iv_entry.delete(0, tk.END)
hashKey_entry.delete(0, tk.END)
checklab.config(text="")
mic = calculate_sha3_512(text + hashKey.decode('utf-8'))
clear_data(hashKey)
textbox.delete(1.0, tk.END)
textbox.insert(1.0, "Encrypting...")
chipertext = encrypt_AES256(aesKey, iv, mic + text)
clear_data(aesKey)
clear_data(iv)
try:
with open(file, 'wb') as f:
f.write(chipertext)
textbox.delete(1.0, tk.END)
textbox.insert(1.0, f"Content encrypted in {file}.")
except Exception as e:
textbox.delete(1.0, tk.END)
messagebox.showerror("Error", f"An error occured writing the chipertext in {file}: {e}")
def decrypt(aesKey_entry, iv_entry, hashKey_entry, file, textbox, checklab):
aesKey = bytearray(aesKey_entry.get(), 'utf-8')
iv = bytearray(iv_entry.get(), 'utf-8')
hashKey = bytearray(hashKey_entry.get(), 'utf-8')
if len(aesKey) != key_len:
messagebox.showerror("Error", "AES-256 key must be 32 bytes long.")
elif len(iv) != iv_len:
messagebox.showerror("Error", "IV (CBC) must be 16 bytes long.")
elif not hashKey:
messagebox.showerror("Error", "Checksum key textbox cannot be empty.")
elif not file:
messagebox.showerror("Error", "File textbox cannot be empty.")
elif not os.path.isfile(file):
messagebox.showerror("Error", "File must exists.")
else:
aesKey_entry.delete(0, tk.END)
iv_entry.delete(0, tk.END)
hashKey_entry.delete(0, tk.END)
textbox.delete(1.0, tk.END)
textbox.insert(1.0, "Decrypting...")
try:
with open(file, 'rb') as f:
chipertext = f.read()
plaintext = decrypt_AES256(aesKey, iv, chipertext)
clear_data(aesKey)
clear_data(iv)
mic = plaintext[:mic_len]
text = plaintext[mic_len:]
valid = mic == calculate_sha3_512(text + hashKey.decode('utf-8'))
clear_data(hashKey)
checklab.config(text=f"Integrity check passed: {valid}")
textbox.delete(1.0, tk.END)
textbox.insert(tk.END, text)
except Exception as e:
textbox.delete(1.0, tk.END)
messagebox.showerror("Error", f"An error occured reading the chipertext in {file}: {e}")
def select_file(entry):
selected_file = filedialog.askopenfilename()
if selected_file:
entry.delete(0, 'end')
entry.insert(0, selected_file)
def update_length_label(entry, label):
data = bytearray(entry.get(), 'utf-8')
length = len(data)
clear_data(data)
label.config(text=f"Length: {length}")
def secNotes_gui():
root = tk.Tk()
root.title("secNotes by Mqtth3w")
root.resizable(False, False)
tk.Label(root, text="Symmetric key AES-256 (32 bytes):").grid(row=0, column=0)
aesKey_entry = tk.Entry(root, width=50, show='*')
aesKey_entry.grid(row=0, column=1)
aesKey_length_label = tk.Label(root, text="Length: 0")
aesKey_length_label.grid(row=0, column=2)
aesKey_entry.bind('<KeyRelease>', lambda event: update_length_label(aesKey_entry, aesKey_length_label))
tk.Label(root, text="IV (CBC mode, 16 bytes):").grid(row=1, column=0)
iv_entry = tk.Entry(root, width=50, show='*')
iv_entry.grid(row=1, column=1)
iv_length_label = tk.Label(root, text="Length: 0")
iv_length_label.grid(row=1, column=2)
iv_entry.bind('<KeyRelease>', lambda event: update_length_label(iv_entry, iv_length_label))
tk.Label(root, text="Key for the checksum (at least one character):").grid(row=2, column=0)
hashKey_entry = tk.Entry(root, width=50, show='*')
hashKey_entry.grid(row=2, column=1)
hashKey_length_label = tk.Label(root, text="Length: 0")
hashKey_length_label.grid(row=2, column=2)
hashKey_entry.bind('<KeyRelease>', lambda event: update_length_label(hashKey_entry, hashKey_length_label))
tk.Label(root, text="Path to the file ex: the/file/name.txt \n(in Windows, the script can write only in the same position as the script or lower in the hierarchy):").grid(row=3, column=0)
file_entry = tk.Entry(root, width=50)
file_entry.grid(row=3, column=1)
tk.Button(root, text="Browse", command=lambda: select_file(file_entry)).grid(row=3, column=2)
tk.Label(root, text="Encypt: The contents of the following textbox will be encrypted in the specified file with the given keys.\n"
"To decrypt the specified file, you need to use the same encryption keys.\n"
"Decrypt: The contents of the encrypted file will be shown in the following textbox.\nPlease wait.\n").grid(row=5, columnspan=3)
tk.Button(root, text="Encrypt", command=lambda: encrypt(aesKey_entry,
iv_entry, hashKey_entry, file_entry.get(), textbox, checklab
)).grid(row=6, column=0)
tk.Button(root, text="Decrypt and check integrity", command=lambda: decrypt(aesKey_entry,
iv_entry, hashKey_entry, file_entry.get(), textbox, checklab
)).grid(row=6, column=1)
checklab = tk.Label(root, text="")
checklab.grid(row=7, column=0)
textbox = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=100, height=25)
#textbox = tk.Text(root, width=100, height=30)
textbox.grid(row=8, column=0, columnspan=3)
root.mainloop()
if __name__ == "__main__":
secNotes_gui()