Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #has user enter keyword and message, checks to see if input has two strings
- def checkString ():
- usermsg = input ("Enter keyword and message to be encrypted, separated by a space: ")
- string = usermsg.split()
- messageList = []
- #each object converted to uppercase, puts into empty list
- for Object in string:
- hello = Object.upper ()
- messageList.append (hello)
- twostring = len(messageList)
- if twostring != 2:
- print ("ERROR")
- else:
- duplicateLetter(messageList)
- #checks and removes duplicate letters and non-alphabet characters, converts X to K & S
- def duplicateLetter (messageList):
- alphabet = "ABCDEFGHIJKSLMNOPQRTUVWXYZ"
- newInput = []
- newStr = ""
- keyword = messageList
- for obj in keyword:
- for ch in obj:
- if ch == "X":
- ch = "KS"
- if ch in alphabet:
- if ch not in newStr:
- newStr = newStr + ch
- newInput.append (newStr)
- newStr = ""
- createAlphaTable (newInput)
- #EvenOdd (newInput,table)
- #takes result of duplicateLetter and puts it into a new string with the rest of the alphabet #creates table in the format of playfair cipher
- def createAlphaTable (newInput):
- alphabet = "ABCDEFGHIJKLMNOPQRSTUVWYZ"
- alphaKey = ""
- keyword = newInput [0]
- for ch in alphabet:
- if ch not in keyword:
- alphaKey = alphaKey + ch
- cipherKey = keyword + alphaKey
- print (cipherKey)
- #createTable (cipherKey)
- #creates table in the format of playfair cipher
- table = [cipherKey[i:i+5] for i in range(0, len(cipherKey), 5)]
- print (table)
- #return table
- EvenOdd (newInput,table)
- # determines the length of the message to be encrypted. if odd, adds 'Z' to the end of the string. if even, continues to next function without change
- def EvenOdd (newInput,table):
- message = newInput [1]
- if (len(message) % 2 == 0):
- tobeEncrypt (message,table)
- else:
- message = message + 'Z'
- tobeEncrypt (message,table)
- #splits the message (2nd inputted word) into pairs and adds a 'Z' at the end of the pair if only containing 1 character, moves pairs into empty list
- def tobeEncrypt(message,table):
- pairList = []
- letterPair = ""
- for ch in message:
- if len(letterPair) != 2:
- letterPair = letterPair + ch
- if len(letterPair) == 2:
- pairList.append (letterPair)
- letterPair = ""
- print (pairList)
- encryptPair(pairList,table)
- def encryptPair(pairList,table):
- rowPos = []
- colPos = []
- row = 0
- col = 0
- i = 0
- q = 0
- for eachPair in pairList:
- for letter in eachPair:
- while i <= 4:
- if letter in table[i] :
- row = i
- break
- else:
- i = i + 1
- while q <= 4:
- if letter in table [row][q]:
- col = q
- break
- else:
- q = q + 1
- rowPos.append(row)
- colPos.append(col)
- i = 0
- q = 0
- #print (rowPos, colPos)
- posList = []
- posPair = ""
- for num in rowPos:
- if len(posPair) != 2:
- posPair = str(num) + str(colPos[i])
- i = i + 1
- if len(posPair) == 2:
- posList.append (posPair)
- posPair = ""
- print (posList)
- convLetter(posList,table)
- def convLetter (posList,table):
- encryptPair = []
- onePair = ""
- twoPair = ""
- d = 0
- w = 0
- q = 1
- for pair in posList:
- pair1 = posList [d][w]
- pair2 = posList [q][w]
- onechar = pair [0]
- twochar = pair [1]
- if pair1 == pair2:
- if int(twochar) == 4:
- twochar = 0
- onePair = onechar + str(twochar)
- twoPair = onechar + str(int(posList [d+1][w+1]) + 1)
- #d = d + 1
- else:
- twochar = int(twochar) + 1
- onePair = onechar + str(twochar)
- twoPair = onechar + str(int(posList [d+1][w+1]) + 1)
- encryptPair.append (onePair)
- encryptPair.append (twoPair)
- onePair = ""
- print(twoPair)
- twoPair = ""
- if q != (len(posList)-2):
- d = d + 1
- q = q + 1
- #print (type(posList [d+1][w+1]))
- ##w =
- ##elif posList [d][w] == posList [d][w]:
- ##for item in pair:
- #break
- print ((len(posList)-2))
- print (encryptPair)
- checkString ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement