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: https://geektechstuff.com/2019/12/22/brute-force-caesar-cipher-python/). 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 https://github.com/geektechdude/Python_Caesar_Cipher

—-


#!/usr/bin/python3
# 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”
return(letters)

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

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.”
return(result)

answer = brute_force(enc_string)
print(answer)

geektechstuff_brute_caesar_1
Python Brute Force Caesar

geektechstuff_brute_caesar_2