-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.py
167 lines (126 loc) · 4.96 KB
/
core.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
#!/usr/bin/env python3
#
# Main Code For ceaserSalad
#
import tkinter as tk
from tkinter import scrolledtext, N, E, S, W
from tkinter import filedialog, simpledialog
from pathlib import Path
import ctypes
class engine:
soup = '''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~0123456789 \n\t'''
def __init__(self, blocking=True):
self.map = {}
self.block = blocking
for char in self.soup:
self.map[char] = self.soup.index(char)
def encrypt(self, plainText, secret):
ind, cypher, l, s = 0, str(), len(secret), len(self.soup)
for char in plainText:
cypher += self.shift(char, secret, ind, l, s)
ind += 1
return cypher
def decrypt(self, cipher, secret):
ind, plain, l, s = 0, str(), len(secret), len(self.soup)
for letter in cipher:
plain += self.shift(letter, secret, ind, l, s, -1)
ind += 1
return plain
def shift(self, letter, secret, ind, l, s, sign=1):
try:
return self.soup[(self.map[letter] + sign * self.map[secret[ind % l]]) % s]
except:
if self.block:
raise ValueError(f'String includes invalid characters, allowed are \n{self.soup}')
else:
return letter
class editor (tk.Tk):
def __init__ (self):
tk.Tk.__init__(self)
self.title("ceaserSalad")
# load the encryption engine
self.engine = engine(blocking=False)
# adjust resolution
ctypes.windll.shcore.SetProcessDpiAwareness(1)
self.tk.call('tk', 'scaling', 2.0)
# window geometry
self.size = (500, 600)
self.geometry("{}x{}".format(*self.size))
self.navHeight = 40
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
# types for explorer
self.fileTypes = (("Text files", "*.txt"), ("Data files", "*.dat"), ("all files", "*.*"))
self.savePath = None
# build UI
self.build()
self.mainloop()
def about (self):
pass
def build (self):
self.navBar = tk.Frame(self, height=self.navHeight, bg="#ddd")
self.navBar.grid(row=0, columnspan=20, rowspan=1, sticky=N+S+E+W)
self.navBar.grid_propagate(0)
self.fileDropDown = tk.Menubutton(self.navBar, text="File")
self.fileDropDown.menu = tk.Menu( self.fileDropDown, tearoff = 0 )
self.fileDropDown["menu"] = self.fileDropDown.menu
self.fileDropDown.menu.add_command(label='Open', command=self.open)
self.fileDropDown.menu.add_command(label='Save', command=self.save)
self.fileDropDown.menu.add_command(label='Save as', command=self.save_as)
self.fileDropDown.menu.add_command(label='About', command=self.save_as)
self.fileDropDown.grid(row=0, column=0)
self.encryptButton = tk.Button(self.navBar, text="Encrypt", command=self.encryptAction)
self.encryptButton.grid(row=0, column=1)
self.decryptButton = tk.Button(self.navBar, text="Decrypt", command=self.decryptAction)
self.decryptButton.grid(row=0, column=2)
self.textField = scrolledtext.ScrolledText(self)
self.textField.grid_propagate(0)
self.textField.grid(row=1, column=0, rowspan=19, columnspan=20, sticky=N+S+E+W)
def decryptAction (self):
password = simpledialog.askstring("Decrypt", "Enter password:")
cipher = self.get()
plaintext = self.engine.decrypt(cipher, password)
self.textField.delete(1.0, tk.END)
self.textField.insert(1.0, plaintext)
def encryptAction (self):
password = simpledialog.askstring("Encrypt", "Enter password:")
plaintext = self.get()
print(plaintext)
cipher = self.engine.encrypt(plaintext, password)
self.textField.delete(1.0, tk.END)
self.textField.insert(1.0, cipher)
def get(self):
'''
Get text from text field.
'''
return self.textField.get(1.0, tk.END)[:-1]
def open (self):
'''
Open file path routine.
'''
# override path
self.savePath = Path(filedialog.askopenfilename(initialdir="~", filetypes=self.fileTypes))
# drop file content into text field
with open(self.savePath) as f:
self.textField.insert(tk.INSERT, f.read())
def save (self):
'''
Quick save routine.
'''
if not self.savePath:
self.save_as()
return
# drop file content into text field
with open(self.savePath, 'w+') as f:
f.write(self.get())
def save_as (self):
'''
Save as routine.
'''
# override the savepath
f = filedialog.asksaveasfile(initialdir="~", defaultextension=self.fileTypes[0][1])
self.savePath = Path(f.name)
f.write('*')
f.close()
# quick save
self.save()