Brute Force Caesar Cipher V2 (Python)

Python Brute Force Caesar

Around a month ago I used Python to brute force the Caesar Cipher (I blogged about it here: My original solution outputted a solution for each possible cipher shift, which worked ok for small text samples but for larger texts started to take up a large portion of the terminal and my time scrolling.

So, version 2 of the Brute Force Caesar Cipher was needed. Version 2 outputs one solution, which the program comes to by looping through all the possible outcomes and choosing the one with the most recognised words. Version 2 uses an English dictionary file (currently in plain text) that I found online.

I have also started placing various parts of the program into functions to make it easier to edit / update. One possible change for the future may be to amend it into a Flask website.

I’ve placed the Brute Force Caesar Cipher and the dictionary file onto my GitHub at


# geektechstuff
# brute force Caesar

# encrypted string to brute force
enc_string = ‘qgmj skkayfewfl, addmkljagmk ugvw tjwscwj, ak lg jwkwsjuz s uahzwj lzsl zskh32!n3adu73y2xlk2k37i3-k839xxe4052p56jko5\zaep525mmg4a50s$3qj4m-065?r5h/o5cz.?u’

def alphabet():
# letters of alphabet (English)
letters = “abcdefghijklmnopqrstuvwxyz”

def dictionary():
# dictonary
dictionary = open(‘/Users/gary/Documents/caesar_brute/dictionary.txt’)
#list_of_words = {}
list_of_words = []
for word in‘\n’):
# close dictionary

def brute_force(enc_string):
best_match = 0
best_match_text = “”
best_cipher = 0
# loop to brute
x = 0
while x < 26: x = x + 1 stringtodecrypt=enc_string stringtodecrypt=stringtodecrypt.lower() ciphershift=int(x) stringdecrypted=”” letters = alphabet() for character in stringtodecrypt: position = letters.find(character) newposition = position-ciphershift if character in letters: stringdecrypted = stringdecrypted + letters[newposition] else: stringdecrypted = stringdecrypted + character detected_words = 0 for word in stringdecrypted.split(‘ ‘): word = word.strip(” “) word = word.strip(“,”) if word in dictionary(): detected_words = detected_words + 1 if detected_words > best_match:
best_match_text = stringdecrypted
best_cipher = str(ciphershift)
number_of_detected_words = detected_words

result = “Best matched text:”,best_match_text,”With Ciphershift of:”,best_cipher,” And”,number_of_detected_words,”detected words.”

answer = brute_force(enc_string)

Python Brute Force Caesar