Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from tkinter import *
from tkinter import ttk
#intializing variables
alphaKey = "` 1234567890-=\qwertyuiop[]asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+|QWERTYUIOP{}ASDFGHJKL:ZXCVBNM<>?"
#setting the keys. Keys were randomly generated using 3 seperate methods are are different lengths.
#a complete loop of all the keys occurs every 8 million iterations.
#recommended changing the keys if you intend to use the program
myKey = [99, 7, 116, 246, 344, 41, 561, 18, 779, 616, 450, 16, 510, 504, 540, 870, 1716, 1394, 861, 387, 8, 54, 16, 36, 48, 68, 100, 60, 133, 160, 36, 50, 45, 64, 6, 256, 1640, 990, 42, 820, 280, 270, 4, 30, 2240, 2187, 2291, 2457, 2624, 1302, 9, 520, 201, 30, 88, 48, 68, 5, 192, 133, 72, 90, 150, 15, 64, 120, 144, 41, 924, 27, 1189, 840, 1290, 41, 1200, 1092, 270, 261, 1482, 820, 21, 144, 344, 90, 1, 56, 81, 116, 160, 60, 63, 24, 9, 280, 87, 132, 246, 368, 1394, 33, 23, 164, 140, 960, 9, 300, 252, 108, 145, 312, 41, 735, 396, 360, 3, 1, 66, 213, 4, 340, 414, 476, 672, 819, 920, 270, 4, 6, 320, 1394, 1089, 65, 3239, 896, 870, 34, 990, 448, 27, 145, 351, 2583, 1344, 36, 8, 204, 87, 2, 267, 348, 445, 384, 476, 520, 621, 620, 231, 316, 30, 24, 164, 2541, 89, 41, 2520, 1350, 75, 2370, 28, 1998, 116, 2418, 205, 1407, 594, 616, 237, 87, 162, 186, 256, 405, 462, 469, 600, 684, 740, 30, 308, 18, 552, 246, 2244, 12, 164, 224, 180, 4, 150, 28, 1512,]
messageKey = [3, 74, 52, 58, 2, 34, 27, 10, 82, 73, 60, 59, 12, 74, 93, 40, 8, 11, 83, 68, 83, 46, 42, 61, 61, 4, 27, 71, 88, 49, 83, 49, 16, 49, 33, 76, 42, 63, 7, 16, 40, 15, 5, 51, 79, 75, 18, 29, 21, 23, 43, 63, 76, 46, 87, 56, 65, 58, 82, 33, 78, 2, 17, 17, 93, 59, 77, 45, 69, 60, 42, 66, 65, 23, 82, 53, 63, 48, 81, 31, 3, 75, 44, 62, 24, 29, 70, 61, 63, 58, 14, 6, 32, 65, 85, 38, 15, 82, 67, 49, 10, 82, 10, 62, 84, 2, 10, 90, 23, 94, 35, 13, 89, 58, 53, 86, 79, 65, 10, 16, 7, 12, 20, 19, 30, 71, 27, 11, 3, 36, 43, 29, 62, 13, 73, 91, 80, 42, 76, 4, 84, 87, 8, 15, 30, 4, 88, 74, 44, 22, 16, 9, 44, 61, 58, 34, 2, 75, 16, 75, 16, 62, 92, 60, 58, 80, 13, 27, 27, 78, 54, 85, 72, 82, 29, 21, 59, 33, 82, 76, 13, 91, 17, 1, 10, 3, 23, 68, 14, 49, 2, 89, 3, 84, 42, 34, 70, 90, 70]
passKey = [4, 5, 6, 8, 10, 11, 12, 15, 16, 17, 19, 20, 21, 23, 24, 28, 29, 32, 33, 36, 39, 40, 42, 44, 47, 48, 50, 53, 60, 62, 63, 70, 72, 78, 79, 80, 81, 84, 90, 94, 95, 96, 97, 99, 100, 101, 102, 104, 110, 114, 116, 117, 120, 122, 123, 124, 127, 128, 129, 131, 133, 134, 135, 136, 140, 142, 145, 147, 148, 151, 152, 154, 156, 159, 161, 162, 165, 167, 169, 170, 171, 174, 176, 178, 180, 181, 182, 185, 186, 189, 190, 194, 196, 197, 198, 200, 201, 204, 207, 210, 213, 223, 224, 225, 227, 228, 229, 230, 231, 232, 233, 234, 237, 240, 242, 245, 246, 248, 250, 252, 255, 258, 259, 260, 262, 268, 270, 272, 276, 279, 280, 286, 288, 290, 294, 297, 298, 299, 300, 302, 304, 306, 308, 309, 310, 311, 312, 314, 315, 316, 318, 322, 324, 326, 328, 330, 334, 338, 340, 342, 345, 346, 348, 350, 354, 356, 358, 360, 363, 365, 366, 368, 369, 371, 375, 376, 377, 378, 381, 386, 388, 390, 392, 393, 395, 399, 400, 402, 403, 404, 407, 408, 410, 411, 414, 416, 417, 420, 426, 429, 432, 435, 436]
#converters letters in a string to a numerical value
def alpha2Num(letter):
if letter is '"':
return 0
index = 0
for l in alphaKey:
if letter is l:
return index
index += 1
return 1
#converts numerical value to a letter
def num2Alpha(number):
return alphaKey[number % len(alphaKey)]
#changes the password to a int array
def setPassword():
passwordNumberArray = []
rawPassword = str(tkRawPassword.get())
for letter in rawPassword:
passwordNumberArray.append(alpha2Num(letter))
return passwordNumberArray
#sets a starting key based numberic representation of the password.
#This will be used to choose the starting location with the keys
def setStartingKey(password):
startingKey = 0
index = 0
for number in password:
if index is 0:
startingKey += number
index = 100
else:
startingKey += (number * index)
index = index * 100
return startingKey
#selects the correct keys based on starting position and password then returns thier effect on encryption
def findKeysum(password, index):
passNumber = password[index % len(password)]
myKeyNumber = myKey[index % len(myKey)]
messageKeyNumber = messageKey[index % len(messageKey)]
passKeyNumber = passKey[index % len(passKey)]
return(messageKeyNumber + (passNumber * passKeyNumber * myKeyNumber) % len(alphaKey))
#encrypts the message based on keys and password
def encryption(password, message, index):
alphaMessage = []
for letter in message:
keySum = findKeysum(password, index)
#selects a number based on the message and keySum, converts it into a letter, and saves it to alphamMssage array
alphaMessage.append(num2Alpha((alpha2Num(letter) + keySum)))
index += 1
output = ''.join(alphaMessage)
return output
#decrypts the message based on keys and password
def decryption(password, message, index):
decoded = []
for letter in message:
letterNumber = alpha2Num(letter)
keySum = findKeysum(password, index)
#finds a number with the same remander as numeric message, changes it to a letter, and saves it to decoded array
decoded.append(num2Alpha(letterNumber + len(alphaKey) - keySum))
index += 1
output =''.join(decoded)
return output
#the main function
def main(*args):
mode = tkMode.get()
password = setPassword()
message = inputTextbox.get('1.0', 'end -1 chars')
#makes decryption much harder with access to source code by changing starting location of decryption
startingKey = setStartingKey(password)
if mode is 2:
codedMessage = decryption(password, message, startingKey)
else:
codedMessage = encryption(password, message, startingKey)
outputTextbox.delete('1.0', END)
outputTextbox.insert(END, codedMessage)
#initialize tkInter
root = Tk()
root.title("Encryptor")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column =0, row = 0, sticky = (N, W, E, S))
mainframe.columnconfigure (0, weight = 1)
mainframe.rowconfigure (0, weight = 1)
#set variables
tkMessage = StringVar()
tkRawPassword = StringVar()
tkMode = IntVar()
#the GUI
feet_entry = ttk.Entry(mainframe, width=20, textvariable = tkRawPassword)
feet_entry.grid (column = 2, row = 1, columnspan = 2, sticky = (W, E))
ttk.Label(mainframe, text="Password:").grid(column=1, row=1, sticky = E)
inputTextbox = Text(mainframe, width = 100, height = 10, state = 'normal')
inputTextbox.grid(column=2, row=2, columnspan = 2, sticky=(E))
ttk.Label(mainframe, text="Input:").grid(column=1, row=2, sticky = E)
ttk.Label (mainframe, text='Output:').grid(column=1, row=3, sticky=(E))
ttk.Button(mainframe, text="Go", command = main).grid(column=3, row=4, sticky = E)
enButton = ttk.Radiobutton(mainframe, text='Encrypt', variable=tkMode, value=1)
enButton.grid(column=1, row=4, sticky = (W))
deButton = ttk.Radiobutton(mainframe, text='Decrypt', variable=tkMode, value=2)
deButton.grid(column=2, row=4, sticky = (W))
outputTextbox = Text(mainframe, width = 100, height = 10, state = 'normal', wrap = 'word')
outputTextbox.grid(column=2, row=3, columnspan = 2, sticky=(E))
for child in mainframe.winfo_children():
child.grid_configure(padx =5, pady=5)
feet_entry.focus()
#makes return run the main function
root.bind('<Return>', main)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.