Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Given a character and an affine cipher key, this function returns the decoded version of such character.
- #the decoded version of a character shall be one of the following:
- #(1) if such character is alphabetic, then:
- #(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.
- #(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
- #(2) otherwise, the decoded version of such character shall be equal to such character itself.
- def decode_character(character,a,b):
- if character in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
- if character.isupper():
- return chr((a*(ord(character)-65)+b)%26+65)
- else:
- return chr((a*(ord(character)-97)+b)%26+97)
- else:
- return character
- #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.
- def decode_word(word,a,b):
- result = ""
- for char in word:
- result += decode_character(char,a,b)
- return result
- #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.
- def strip_non_alphabetic(word):
- result = ""
- for char in word:
- if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
- result += char
- return result
- #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.
- def score_solution(solution,word_list):
- solution_words = solution.split()
- count = 0
- for word in solution_words:
- if strip_non_alphabetic(word).lower() in word_list:
- count += 1
- return (count,solution)
- #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.
- def read_ciphertext():
- result = ""
- done = False
- print("Enter some ciphertext. Enter a blank line when done:")
- while not done:
- cur_line = input()
- if cur_line == "":
- done = True
- else:
- result += cur_line+"\n"
- return result
- enable = open("enable1.txt","r")
- word_list = enable.read().splitlines()
- enable.close()
- word_list += ["a","i"]
- ciphertext = read_ciphertext()
- solutions = []
- for a in range(26):
- if (a%2 == 0) or (a%13 == 0):
- continue
- for b in range(26):
- decoded_text = decode_word(ciphertext,a,b)
- solutions.append(score_solution(decoded_text,word_list))
- solutions.sort()
- solutions.reverse()
- print("The following are possible solutions for this ciphertext:")
- i = 0
- highest_score = solutions[0][0]
- while solutions[i][0] == highest_score:
- print(solutions[i][1])
- i = i+1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement