-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdfgh.py
65 lines (65 loc) · 2.89 KB
/
sdfgh.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
import tkinter, tkinter.messagebox, tkinter.simpledialog, tkinter.scrolledtext, Crypto.Random, Crypto.Protocol.KDF, Crypto.Cipher.AES, os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
def cipherAES_GCM(pwd, nonce):
key = Crypto.Protocol.KDF.PBKDF2(pwd, nonce, count=100000)
return Crypto.Cipher.AES.new(key, Crypto.Cipher.AES.MODE_GCM, nonce=nonce, mac_len=16)
def save(e):
global pwd
if 'pwd' not in globals():
pwd = tkinter.simpledialog.askstring('sdfgh', 'Password:', show="*").encode()
nonce = Crypto.Random.new().read(16)
ciphertext = nonce + b''.join(cipherAES_GCM(pwd, nonce).encrypt_and_digest(text.get("1.0", 'end-1c').encode()))
with open('sdfgh.dat', 'wb') as f:
f.write(ciphertext)
text.edit_modified(False)
def load(root):
global pwd
try:
with open('sdfgh.dat', 'rb') as f:
s = f.read()
nonce, ciphertext, tag = s[:16], s[16:len(s)-16], s[-16:]
while True:
try:
root.update_idletasks()
pwd = tkinter.simpledialog.askstring('sdfgh', 'Password:', show="*").encode()
s = cipherAES_GCM(pwd, nonce).decrypt_and_verify(ciphertext, tag).decode()
break
except AttributeError:
exit()
except ValueError: # wrong password, let's try again
pass
except FileNotFoundError: # new file
s = "Welcome! This is a new file. Basic documentation:\n* Save with CTRL+S\n* Quit with CTRL+W or ALT+F4\n* Find a pattern with CTRL+F or F3"
return s
def close(e=None):
if not text.edit_modified() or tkinter.messagebox.askokcancel('sdfgh', "Some modifications have not been saved, do you really want to quit?", default=tkinter.messagebox.CANCEL):
root.destroy()
def find(e, findnext=False):
global query
if not findnext or 'query' not in globals():
query = tkinter.simpledialog.askstring('sdfgh', 'Query:')
start = "1.0"
else:
start = text.index(tkinter.INSERT) + "+1c"
pos = text.search(query, start, stopindex="end", nocase=True, regexp=True)
if pos != '':
text.mark_set("insert", pos)
text.see("insert")
text.focus()
if __name__ == '__main__':
root = tkinter.Tk('sdfgh')
root.title('sdfgh')
root.state('zoomed')
text = tkinter.scrolledtext.ScrolledText(master=root, wrap='none', bg='#272822', fg="#f8f8f2", insertbackground='#f8f8f2', font=("Consolas", 10))
text.pack(side="top", fill="both", expand=True, padx=0, pady=0)
root.bind('<Control-s>', save) # CTRL+S too?
root.bind('<Control-w>', close)
root.bind('<Control-f>', find)
root.bind('<F3>', lambda e: find(e, True))
root.protocol("WM_DELETE_WINDOW", close)
s = load(root)
text.insert(tkinter.END, s)
text.mark_set("insert", "1.0") # cursor position at the start
text.edit_modified(False)
text.focus()
root.mainloop()