Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #Given a character and an affine cipher key, this function returns the decoded version of such character.
  2. #the decoded version of a character shall be one of the following:
  3. #(1) if such character is alphabetic, then:
  4. #(a) if such character is upper case, then if such character has ascii value x, then the decoded version of such character shall have ascii value equal to (a*(x-65))%26+65.
  5. #(b) if such character is lower case, then if such character has ascii value x, then the decoded version of such character shall have ascii value equal to (a*(x-97))%26+97
  6. #(2) otherwise, the decoded version of such character shall be equal to such character itself.
  7. def decode_character(character,a,b):
  8. if character in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
  9. if character.isupper():
  10. return chr((a*(ord(character)-65)+b)%26+65)
  11. else:
  12. return chr((a*(ord(character)-97)+b)%26+97)
  13. else:
  14. return character
  15. #given a word and an affine cipher key, this function decodes such word using such key by decoding each of the characters of which such word consists.
  16. def decode_word(word,a,b):
  17. result = ""
  18. for char in word:
  19. result += decode_character(char,a,b)
  20. return result
  21. #Given a word, this function discards all non-alphabetic characters of such word. This fixes a bug where the correct cipher solution was not being computed when the ciphertext included non-alphabetic characters.
  22. def strip_non_alphabetic(word):
  23. result = ""
  24. for char in word:
  25. if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
  26. result += char
  27. return result
  28. #Given a string that is the solution to an affine cipher and a list of valid words (such as can be found in the enable1.txt file), this function determines the score of such solution. The score of a solution shall be equal to the number of words in such solution which are found in the list of valid words.
  29. def score_solution(solution,word_list):
  30. solution_words = solution.split()
  31. count = 0
  32. for word in solution_words:
  33. if strip_non_alphabetic(word).lower() in word_list:
  34. count += 1
  35. return (count,solution)
  36. #This function reads the ciphertext from the user. Since the ciphertext may consist of multiple lines, the convension shall be that the ciphertext ends with the first blank line read.
  37. def read_ciphertext():
  38. result = ""
  39. done = False
  40. print("Enter some ciphertext. Enter a blank line when done:")
  41. while not done:
  42. cur_line = input()
  43. if cur_line == "":
  44. done = True
  45. else:
  46. result += cur_line+"\n"
  47. return result
  48. enable = open("enable1.txt","r")
  49. word_list = enable.read().splitlines()
  50. enable.close()
  51. word_list += ["a","i"]
  52. ciphertext = read_ciphertext()
  53. solutions = []
  54. for a in range(26):
  55. if (a%2 == 0) or (a%13 == 0):
  56. continue
  57. for b in range(26):
  58. decoded_text = decode_word(ciphertext,a,b)
  59. solutions.append(score_solution(decoded_text,word_list))
  60. solutions.sort()
  61. solutions.reverse()
  62. print("The following are possible solutions for this ciphertext:")
  63. i = 0
  64. highest_score = solutions[0][0]
  65. while solutions[i][0] == highest_score:
  66. print(solutions[i][1])
  67. i = i+1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement