Click to download code
#Zach Krasne
#Purpose: Executes a Vigenere cipher
#Input: Word to be coded and keyword from user
#Output: Encoded word to Graphics window text box
#Requires a Python Graphics Library
from graphics import *
def code(message, keyword):
#Create a reference list for the cipher
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
encodedWord = ""
#Find the ciphered letter and add it into the empty string
for i in range(len(message)):
shiftNum = alphabet.find(keyword[i % len(keyword)]) + 1
indexOfLetterToShift = alphabet.find(message[i])
encodedWord += alphabet[(indexOfLetterToShift + shiftNum) % 26]
return encodedWord
def wordPrep(word):
#Get rid of the spaces and capitalize all the words
wordList = word.split()
newWord = ""
for item in wordList:
capWord = item.upper()
newWord += capWord
return newWord
def main():
#Initiate the Graphics Window and set it up
win = GraphWin("Vigenere Cipher", 400, 400)
win.setBackground("lightblue")
messageText = Text(Point(120, 175), "Message to be Encoded:")
keyText = Text(Point(120, 200), "Key word for encryption:")
messageText.draw(win)
keyText.draw(win)
#Initialize the entry objects to get user input
userWord = Entry(Point(250, 175), 15)
userKey = Entry(Point(250, 200), 15)
userWord.draw(win)
userKey.draw(win)
win.getMouse()
#Get the strings formatted, then encrypt them
userWordCap = wordPrep(userWord.getText())
userKeyCap = wordPrep(userKey.getText())
encoded = code(userWordCap, userKeyCap)
#Reset the window to display the final encryption
userWord.undraw()
userKey.undraw()
messageText.undraw()
keyText.undraw()
result = Text(Point(200, 200), encoded)
result.setSize(25)
result.draw(win)
instructions = Text(Point(200, 350), "Click anywhere to close window.")
instructions.draw(win)
win.getMouse()
win.close()
main()