title: Brute forcing a Caesar Cipher in Python
body: <p>I am trying to figure out an assignment which asks us to write a program to brute force an encrypted Caesar cipher message. I can do this to the point where my program prints all of the possible decrypted messages, but I am stuck on the next part that states:</p>
  <blockquote>
  <p>For each of the 26 possibly-decrypted messages, our program needs to figure out whether it &quot;looks like English&quot; instead of encrypted gibberish.</p>
  <p>Here's how: We'll take each word in the possibly-decrypted message and look it up in a dictionary (a list of English words). If the word is in the dictionary, then it's an English word; if there are a lot of English words in this possibly-decrypted message, it's likely that this message is the correct decrypted plaintext. (If very few words in the message are in the dictionary, then this message isn't the English plaintext.) So we need to count up how many of the words in each possibly-decrypted message we find in the dictionary, saving that total along with the message that produced it.</p>
  <p>Once we're done with all 26 possible decryptions, we should expect that the possibly-decrypted message that had the most &quot;hits&quot; in the dictionary is in fact the correctly decrypted plaintext, and that's the message we return.</p>
  </blockquote>
  <p>Here is the code I have so far:</p>
  <pre><code>alphabet = 'abcdefghijklmopqrstuvwxyz'
  infile = open('wordlist.txt', 'r')
  
  def Rotate(key: int) -&gt; str:
      rotate = &quot;&quot;
      for l in alphabet:
          if l in alphabet:
               rotate += alphabet[(alphabet.index(l) + key) % (len(alphabet))]
      return rotate
  
  def Caesar_break(sentence:str)-&gt; str:
      &quot;&quot;&quot;Takes a str encrypted by a caesar cipher and returns the original string
      without using the cipher key&quot;&quot;&quot;
      infile = open('wordlist.txt', 'r')
      possible = []
      for key in range(25):
          Rotate(key)
          table = str.maketrans(Rotate(key), alphabet)
          possible.append(sentence.translate(table)) 
  </code></pre>
  <p>The end result should be that</p>
  <pre><code>Caesar_break(&quot;I qwxm bqra lwma nwb bitm bw uwnp jmkicam I qidm wbqmz abcoo bw lw&quot;)
  </code></pre>
  <p>prints</p>
  <blockquote>
  <p>I hope this does not take to long because I have other stuff to do</p>
  </blockquote>