Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A polyalphabetic (Vigenere) cipher encryption and decryption program
- # Based on a single keyword and a modified alphabet
- # messages to be encrypted must be lower case letters only and not contain spaces,
- # numbers, punctuation, or symbols
- # variables
- alphabet0 = "kryptosabcdefghijlmnquvwxz"# The alphabet variant
- keyWord = ""
- plainText = ""
- alphaLength = len(alphabet0)
- # functions
- # Set up the alphabet grid
- def matAlpha(m, n):
- global gridAlpha
- gridAlpha = [] # define empty matrix
- # n rows
- # m cols
- for i in range(n): # total row is n
- row = []
- for j in range(m): # total column is m
- row.append("") # adding "" null string for each column for this row
- gridAlpha.append(row) # add fully defined column into the row
- # Fill alphabet grid with 26 shifted alphabets
- def FillAlpha(aL):
- global gridAlpha
- stoLine = 0
- for alphab in range(aL):
- for letter in range(aL):
- gridAlpha[alphab][letter] = alphabet0[((letter + alphab)%aL)]
- def Encrypt():
- global gridAlpha, plainText, keyWord, alphabet0, cryptOut
- plainText = input('Enter the message: ')
- keyWord = input('Enter the keyword: ')
- # variables for Encyption
- cryptOut = ""
- crypText = []
- textLength = len(plainText)
- keyLength = len(keyWord)
- # loop through the plain text and encrypt
- for x in range(textLength):
- letterX = plainText[x]
- keyNum = x%keyLength
- keyLetter = keyWord[keyNum]
- alphaNum = alphabet0.find(keyLetter)
- letterNum = alphabet0.find(letterX)
- cryptLetter = gridAlpha[alphaNum][letterNum]
- crypText.append(cryptLetter)
- cryptLength = len(crypText)
- for y in range(cryptLength):
- cryptOut += crypText[y]
- print(cryptOut) #print the ciphertext
- # save as a text file
- text_file = open("cryptOut.txt", "w")
- n = text_file.write(cryptOut)
- text_file.close()
- def Decrypt():
- global gridAlpha, keyWord, alphabet0, cryptIn, plainOut
- cryptIn = input('Enter the ciphertext: ')
- keyWord = input('Enter the keyword: ')
- # variables for decryption
- plainOut = ""
- plainText = []
- cryptLength = len(cryptIn)
- keyLength = len(keyWord)
- # loop through the plain text and encrypt
- for x in range(cryptLength):
- letterX = cryptIn[x]
- keyNum = x%keyLength
- keyLetter = keyWord[keyNum]
- alphaNum = alphabet0.find(keyLetter)
- letterNum = gridAlpha[alphaNum].index(letterX)
- plainLetter = alphabet0[letterNum]
- plainText.append(plainLetter)
- plainLength = len(plainText)
- for y in range(plainLength):
- plainOut += plainText[y]
- print(plainOut) #print the plaintext
- def DecryptFile():
- global gridAlpha, keyWord, alphabet0, cryptIn, plainOut
- getCryptext()
- keyWord = input('Enter the keyword: ')
- # variables for decryption
- plainOut = ""
- plainText = []
- cryptLength = len(cryptIn)
- keyLength = len(keyWord)
- # loop through the plain text and encrypt
- for x in range(cryptLength):
- letterX = cryptIn[x]
- keyNum = x%keyLength
- keyLetter = keyWord[keyNum]
- alphaNum = alphabet0.find(keyLetter)
- letterNum = gridAlpha[alphaNum].index(letterX)
- plainLetter = alphabet0[letterNum]
- plainText.append(plainLetter)
- plainLength = len(plainText)
- for y in range(plainLength):
- plainOut += plainText[y]
- print(plainOut) #print the plaintext
- def getCryptext():
- global cryptIn
- with open('cryptOut.txt', 'r') as file:
- crypt = file.read().rstrip()
- cryptIn = crypt.casefold()
- #example using alphabet0 = kryptosabcdefghijlmnquvwxz and keyword = palimpsest
- # message
- #betweensubtleshadingandtheabsenceoflightliesthenuanceofiqlusion
- # resulting ciphertext
- #emufphzlrfaxyusdjkzldkrnshgnfivjyqtquxqbqvyuvlltrevjyqtmkyrdmfd
- #main section
- matAlpha(alphaLength, alphaLength)
- FillAlpha(alphaLength)
- #inputs
- action = input("Enter e to encrypt a message, d to decode a ciphertext, f to decode a .txt file: ")
- if action == "e":
- Encrypt()
- elif action == "d":
- Decrypt()
- elif action == "f":
- DecryptFile()
- else:
- print("Action invalid")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement