Advertisement
acclivity

pyBestGuessCaesarDecryption

Nov 21st, 2021
1,934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. # Caesar Cypher Decoding
  2. # Decode some text by finding out what Caesar offset produces the best looking result
  3.  
  4. intext = "evmvizekyvwzvcufwyldretfewcztknrjjfdltyfnvuspjfdrepkfjfwvn"
  5.  
  6. # Read a sample file of valid English words
  7. # This wordlist can be found here:-   https://pastebin.com/VtvPbcAd
  8. fin = open("mywords.txt")
  9. alltext = fin.read()            # Read the whole file in one go
  10. wordlist = alltext.split()      # Split the total text into words
  11.  
  12. # Try decoding the input text using all possible offsets, 1 to 25, in turn
  13. # We will keep scores of good words found, for each offset (each score will be appended to this list)
  14. countlist = [0]
  15.  
  16. # Each good word that is found in the decoded text, will add a score equal to the length of the word
  17. # For example: it is considered much more significant to find the word "health" than to find "he"
  18.  
  19. for off in range(1, 26):            # Try each offset (1 to 25) in turn
  20.     otext = ""
  21.     for ch in intext:
  22.         x = ord(ch) - ord('a')      # compute the distance of a letter from 'a'
  23.         x += off                    # add the current offset
  24.         x %= 26                     # wrap around 26 letters
  25.         otext += chr(x + ord('a'))  # build the decoded output text
  26.  
  27.     # Now find all the valid English words that exist in the decoded text
  28.     score = 0
  29.     for w in wordlist:          # try each word in our sample English words list
  30.         if w in otext:          # does this word exist in the decoded text?
  31.             score += len(w)     # - yes - the score for this word is the length of the word
  32.  
  33.     countlist.append(score)     # append the total matching words score to our list of scores per offset
  34.  
  35. # Find the best scoring offset by examining our list of scores
  36. best = max(countlist)
  37.  
  38. # Find the offset that made that biggest score
  39. off = countlist.index(best)
  40.  
  41. # Now decode the encypted text once more, this time using the offset that gave the best results
  42. otext = ""
  43. for ch in intext:
  44.     x = ord(ch) - ord('a')      # compute the distance of a letter from 'a'
  45.     x += off                    # add the current offset
  46.     x %= 26                     # wrap around 26 letters
  47.     otext += chr(x + ord('a'))  # build the decoded output text
  48.  
  49. print(otext)                    # Print out our best attempt at the decoded text
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement