Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Caesar Cypher Decoding
- # Decode some text by finding out what Caesar offset produces the best looking result
- intext = "evmvizekyvwzvcufwyldretfewcztknrjjfdltyfnvuspjfdrepkfjfwvn"
- # Read a sample file of valid English words
- # This wordlist can be found here:- https://pastebin.com/VtvPbcAd
- fin = open("mywords.txt")
- alltext = fin.read() # Read the whole file in one go
- wordlist = alltext.split() # Split the total text into words
- # Try decoding the input text using all possible offsets, 1 to 25, in turn
- # We will keep scores of good words found, for each offset (each score will be appended to this list)
- countlist = [0]
- # Each good word that is found in the decoded text, will add a score equal to the length of the word
- # For example: it is considered much more significant to find the word "health" than to find "he"
- for off in range(1, 26): # Try each offset (1 to 25) in turn
- otext = ""
- for ch in intext:
- x = ord(ch) - ord('a') # compute the distance of a letter from 'a'
- x += off # add the current offset
- x %= 26 # wrap around 26 letters
- otext += chr(x + ord('a')) # build the decoded output text
- # Now find all the valid English words that exist in the decoded text
- score = 0
- for w in wordlist: # try each word in our sample English words list
- if w in otext: # does this word exist in the decoded text?
- score += len(w) # - yes - the score for this word is the length of the word
- countlist.append(score) # append the total matching words score to our list of scores per offset
- # Find the best scoring offset by examining our list of scores
- best = max(countlist)
- # Find the offset that made that biggest score
- off = countlist.index(best)
- # Now decode the encypted text once more, this time using the offset that gave the best results
- otext = ""
- for ch in intext:
- x = ord(ch) - ord('a') # compute the distance of a letter from 'a'
- x += off # add the current offset
- x %= 26 # wrap around 26 letters
- otext += chr(x + ord('a')) # build the decoded output text
- print(otext) # Print out our best attempt at the decoded text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement