Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #has user enter keyword and message, checks to see if input has two strings
  2. def checkString ():
  3. usermsg = input ("Enter keyword and message to be encrypted, separated by a space: ")
  4. string = usermsg.split()
  5. messageList = []
  6. #each object converted to uppercase, puts into empty list
  7. for Object in string:
  8. hello = Object.upper ()
  9. messageList.append (hello)
  10.  
  11. twostring = len(messageList)
  12. if twostring != 2:
  13. print ("ERROR")
  14. else:
  15. duplicateLetter(messageList)
  16.  
  17.  
  18. #checks and removes duplicate letters and non-alphabet characters, converts X to K & S
  19. def duplicateLetter (messageList):
  20. alphabet = "ABCDEFGHIJKSLMNOPQRTUVWXYZ"
  21. newInput = []
  22. newStr = ""
  23. keyword = messageList
  24. for obj in keyword:
  25. for ch in obj:
  26. if ch == "X":
  27. ch = "KS"
  28. if ch in alphabet:
  29. if ch not in newStr:
  30. newStr = newStr + ch
  31. newInput.append (newStr)
  32. newStr = ""
  33. createAlphaTable (newInput)
  34. #EvenOdd (newInput,table)
  35.  
  36. #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
  37. def createAlphaTable (newInput):
  38. alphabet = "ABCDEFGHIJKLMNOPQRSTUVWYZ"
  39. alphaKey = ""
  40. keyword = newInput [0]
  41. for ch in alphabet:
  42. if ch not in keyword:
  43. alphaKey = alphaKey + ch
  44. cipherKey = keyword + alphaKey
  45. print (cipherKey)
  46. #createTable (cipherKey)
  47.  
  48. #creates table in the format of playfair cipher
  49. table = [cipherKey[i:i+5] for i in range(0, len(cipherKey), 5)]
  50. print (table)
  51. #return table
  52. EvenOdd (newInput,table)
  53.  
  54. # 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
  55. def EvenOdd (newInput,table):
  56. message = newInput [1]
  57.  
  58. if (len(message) % 2 == 0):
  59. tobeEncrypt (message,table)
  60. else:
  61. message = message + 'Z'
  62. tobeEncrypt (message,table)
  63.  
  64. #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
  65. def tobeEncrypt(message,table):
  66. pairList = []
  67. letterPair = ""
  68. for ch in message:
  69. if len(letterPair) != 2:
  70. letterPair = letterPair + ch
  71. if len(letterPair) == 2:
  72. pairList.append (letterPair)
  73. letterPair = ""
  74. print (pairList)
  75. encryptPair(pairList,table)
  76.  
  77. def encryptPair(pairList,table):
  78. rowPos = []
  79. colPos = []
  80.  
  81. row = 0
  82. col = 0
  83. i = 0
  84. q = 0
  85. for eachPair in pairList:
  86. for letter in eachPair:
  87. while i <= 4:
  88. if letter in table[i] :
  89. row = i
  90. break
  91. else:
  92. i = i + 1
  93. while q <= 4:
  94. if letter in table [row][q]:
  95. col = q
  96. break
  97. else:
  98. q = q + 1
  99. rowPos.append(row)
  100. colPos.append(col)
  101. i = 0
  102. q = 0
  103. #print (rowPos, colPos)
  104.  
  105. posList = []
  106. posPair = ""
  107. for num in rowPos:
  108. if len(posPair) != 2:
  109. posPair = str(num) + str(colPos[i])
  110. i = i + 1
  111. if len(posPair) == 2:
  112. posList.append (posPair)
  113. posPair = ""
  114. print (posList)
  115. convLetter(posList,table)
  116.  
  117. def convLetter (posList,table):
  118. encryptPair = []
  119. onePair = ""
  120. twoPair = ""
  121. d = 0
  122. w = 0
  123. q = 1
  124. for pair in posList:
  125. pair1 = posList [d][w]
  126. pair2 = posList [q][w]
  127. onechar = pair [0]
  128. twochar = pair [1]
  129. if pair1 == pair2:
  130. if int(twochar) == 4:
  131. twochar = 0
  132. onePair = onechar + str(twochar)
  133. twoPair = onechar + str(int(posList [d+1][w+1]) + 1)
  134. #d = d + 1
  135. else:
  136. twochar = int(twochar) + 1
  137. onePair = onechar + str(twochar)
  138. twoPair = onechar + str(int(posList [d+1][w+1]) + 1)
  139. encryptPair.append (onePair)
  140. encryptPair.append (twoPair)
  141. onePair = ""
  142. print(twoPair)
  143. twoPair = ""
  144. if q != (len(posList)-2):
  145. d = d + 1
  146. q = q + 1
  147. #print (type(posList [d+1][w+1]))
  148. ##w =
  149. ##elif posList [d][w] == posList [d][w]:
  150. ##for item in pair:
  151. #break
  152. print ((len(posList)-2))
  153. print (encryptPair)
  154.  
  155. checkString ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement