-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
225 lines (178 loc) · 8.83 KB
/
main.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
# import classes
import random
from tkinter import *
def clearEncryptField():
preText_field.delete(0, END)
encryptText_field.delete(0, END)
encryptKeyCode_field.delete(0, END)
def clearDecryptField():
postText_field.delete(0, END)
decryptText_field.delete(0, END)
decryptKeyCode_field.delete(0, END)
# Function to convert the normal text into encrypted text
def encryptTexttoCode():
encryptText_field.delete(0, END)
encryptKeyCode_field.delete(0, END)
shiftPosition = random.randint(1, 26)
encryptText = preText_field.get()
unrecognizeLetterCount = 0
for string in encryptText:
if not string.isalpha():
unrecognizeLetterCount = unrecognizeLetterCount + 1
if not 0 == unrecognizeLetterCount:
encryptText_field.insert(0, "Character Error!")
encryptKeyCode_field.insert(0, "Character Error!")
else:
encryptList = []
for letter in encryptText:
if letter.isupper():
updateNumericValue = (ord(letter) - 64) + shiftPosition
if updateNumericValue > 26:
updateNumericValue = updateNumericValue % 26
convertValue = updateNumericValue + 64
encryptList.append(chr(convertValue))
else:
convertValue = updateNumericValue + 64
encryptList.append(chr(convertValue))
elif letter.islower():
updateNumericValue = (ord(letter) - 96) + shiftPosition
if updateNumericValue > 26:
updateNumericValue = updateNumericValue % 26
convertValue = updateNumericValue + 96
encryptList.append(chr(convertValue))
else:
convertValue = updateNumericValue + 96
encryptList.append(chr(convertValue))
encryptText = ''.join(encryptList)
encryptText_field.insert(0, encryptText)
if len(encryptList) != 0:
encryptKeyCode_field.delete(0, END)
encryptKeyCode_field.insert(0, str(shiftPosition))
def decryptTexttoCode():
decryptText_field.delete(0, END)
decryptText = postText_field.get()
keycodeStr = decryptKeyCode_field.get()
numbers = []
for number in keycodeStr:
numbers.insert(0, number)
if len(numbers) != 0:
keycode = int(''.join(numbers))
else:
keycode = 0
unrecognizeLetterCount = 0
for string in decryptText:
if not string.isalpha():
unrecognizeLetterCount = unrecognizeLetterCount + 1
if unrecognizeLetterCount != 0:
decryptText_field.insert(0, "Character Error!")
else:
decryptList = []
for letter in decryptText:
if letter.isupper():
updateNumericValue = (ord(letter) - 64) - keycode
if updateNumericValue < 1:
updateNumericValue = updateNumericValue + 26
convertValue = updateNumericValue + 64
decryptList.append(chr(convertValue))
else:
convertValue = updateNumericValue + 64
decryptList.append(chr(convertValue))
elif letter.islower():
updateNumericValue = (ord(letter) - 96) - keycode
if updateNumericValue < 1:
updateNumericValue = updateNumericValue + 26
convertValue = updateNumericValue + 96
decryptList.append(chr(convertValue))
else:
convertValue = updateNumericValue + 96
decryptList.append(chr(convertValue))
decryptText = ''.join(decryptList)
decryptText_field.insert(0, decryptText)
if __name__ == "__main__":
# Create a GUI window
root = Tk()
w = 500 # width
h = 670 # height
# Get screen width and height
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
# Calculate x and y coordinates
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
# set the dimensions of the screen and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
# set the name of tkinter GUI window
root.title("CryptGuard")
# CryptGuard Logo Card
logo_card = Frame(root, bg='lightgray', padx=10, pady=10)
logo_card.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
# CryptGuard Logo
originalPhoto = PhotoImage(file=r"C:\Users\ASUS\Desktop\CryptGuard\key.png")
resized_photo = originalPhoto.subsample(15, 15) # You can adjust the subsample factor as needed
cryptguard = Label(root, text="CryptGuard", image=resized_photo, compound="top", fg='black', font=("Lato", 15, "bold"), justify="center")
cryptguard.grid(row=1, column=0, columnspan=3, padx=10, pady=10)
# Create a frame for the encrypt fields
encryptCard = Frame(root, bd=1, relief="solid", padx=10, pady=10)
encryptCard.grid(row=2, column=0, columnspan=3, padx=10, pady=10, sticky="ew")
# Encryption Title
encryptHeader = Label(encryptCard, text="Encryption", fg='gray', font=("Lato", 12, "italic"))
encryptHeader.grid(row=0, column=0, padx=10, pady=10)
# Text to Encrypt section
preText = Label(encryptCard, text="Text to Encrypt", fg='black', font=("Lato", 12, "bold"))
preText.grid(row=1, column=0, padx=10, pady=10)
preText_field = Entry(encryptCard, font=("Lato", 12))
preText_field.grid(row=1, column=1, padx=10, pady=10)
# Encrypted Text
encryptText = Label(encryptCard, text="Encrypted Text", fg='black', font=("Lato", 12, "bold"))
encryptText.grid(row=2, column=0, padx=10, pady=10)
encryptText_field = Entry(encryptCard, font=("Lato", 12))
encryptText_field.insert(0, 'Automatically Generated')
encryptText_field.grid(row=2, column=1, padx=10, pady=10)
# Key Code
encryptKeyCode = Label(encryptCard, text="Key Code", fg='black', font=("Lato", 12, "bold"))
encryptKeyCode.grid(row=3, column=0, padx=10, pady=10)
encryptKeyCode_field = Entry(encryptCard, font=("Lato", 12))
encryptKeyCode_field.insert(0, 'Automatically Generated')
encryptKeyCode_field.grid(row=3, column=1, padx=10, pady=10)
# Create a submit button to encrypt the text
encryptButton = Button(encryptCard, height=4, width=8, text="Encrypt", fg="black", font=("Lato", 12, "bold"), command=encryptTexttoCode)
encryptButton.grid(row=1, rowspan=2, column=2, pady=10, padx=10)
# Create a submit button to clear the encrypt fields
clearEncrypt = Button(encryptCard, height=1, width=8, text="Clear", fg="black", font=("Lato", 12, "bold"), command=clearEncryptField)
clearEncrypt.grid(row=3, column=2, pady=10, padx=10)
# Create a frame for the decrypt fields
decryptCard = Frame(root, bd=1, relief="solid", padx=10, pady=10)
decryptCard.grid(row=3, column=0, columnspan=3, padx=10, pady=10, sticky="ew")
# Decryption Title
decryptHeader = Label(decryptCard, text="Decryption", fg='gray', font=("Lato", 12, "italic"))
decryptHeader.grid(row=0, column=0, padx=10, pady=10)
# Text to Decrypt section
postText = Label(decryptCard, text="Text to Decrypt", fg='black', font=("Lato", 12, "bold"))
postText.grid(row=1, column=0, padx=10, pady=10)
postText_field = Entry(decryptCard, font=("Lato", 12))
postText_field.grid(row=1, column=1, padx=10, pady=10)
# Key Code
decryptKeyCode = Label(decryptCard, text="Key Code", fg='black', font=("Lato", 12, "bold"))
decryptKeyCode.grid(row=2, column=0, padx=10, pady=10)
decryptKeyCode_field = Entry(decryptCard, font=("Lato", 12))
decryptKeyCode_field.grid(row=2, column=1, padx=10, pady=10)
# Decrypted Text
decryptText = Label(decryptCard, text="Decrypted Text", fg='black', font=("Lato", 12, "bold"))
decryptText.grid(row=3, column=0, padx=10, pady=10)
decryptText_field = Entry(decryptCard, font=("Lato", 12))
decryptText_field.insert(0, "Automatically Generated")
decryptText_field.grid(row=3, column=1, padx=10, pady=10)
# Create a submit button to decrypt the text
decryptButton = Button(decryptCard, height=4, width=8, text="Decrypt", fg="black", font=("Lato", 12, "bold"), command=decryptTexttoCode)
decryptButton.grid(row=1, rowspan=2, column=2, pady=10, padx=10)
# Create a submit button to clear the decrypt fields
clearDecrypt = Button(decryptCard, height=1, width=8, text="Clear", fg="black", font=("Lato", 12, "bold"), command=clearDecryptField)
clearDecrypt.grid(row=3, column=2, pady=10, padx=10)
# Create a frame ownership
ownership = Frame(root, padx=10, pady=10)
ownership.grid(row=4, column=0, columnspan=3)
# ownership with your name
ownershipFooter = Label(ownership, text="© 2024 Sachindu Malshan | All rights reserved.", fg='black', font=("Lato", 12), justify="center")
ownershipFooter.grid(row=1, column=0, columnspan=3)
# Start the GUI
root.mainloop()