Brute Force Caesar Cipher (Python)

Brute Force Caesar Cipher

“The Caesar Cipher (Caesar Shift, Caesar’s Code) is a simple, easy to implement substitution cipher. The cipher is named after Julius Caesar. The cipher works by taking the letters of the message and then shifts the letter a defined space along the alphabet.

For example a shift of 1 character would make a=b, b=c, c=d etc.”

I have looked at creating a few ways to use the Caesar Cipher, however I’ve never included a brute force method which is something I am going to correct now.

geektechstuff_brute_force_caesar
Brute Force Caesar Cipher

letters = “abcdefghijklmnopqrstuvwxyz”
enc_string = “hpwwozypjzfnclnvpoespnzopjzflcpyzhlyzqqtntlwnzopmcplvpcszhhzfwojzftx aczgpespnlpdlcntaspcezxlvpteslcopcezmcplvcplozyezespypiedepaezqtyozfe”
x = 0
while x < 26:
x = x + 1
stringtodecrypt=enc_string
stringtodecrypt=stringtodecrypt.lower()
ciphershift=int(x)
stringdecrypted=””
for character in stringtodecrypt:
position = letters.find(character)
newposition = position-ciphershift
if character in letters:
stringdecrypted = stringdecrypted + letters[newposition]
else:
stringdecrypted = stringdecrypted + character
ciphershift=str(ciphershift)
print(“You used a cipher shift of “+ciphershift)
print(“Your decrypted message reads:”)
print(stringdecrypted)
print(“\n”)

 

The Python program takes an encrypted string and then outputs all possibilities of the string by cycling through each possible Caesar Cipher. For small strings this is quite quick, however for larger strings it will take longer. Possible future improvements would be outputting all possibilities to different text files, and/or using character frequency to pick out the possible vowels.

This blog post was inspired by the Future Learn course, “Introduction to Encryption and Cryptography” which I am currently undertaking. The GitHub for my Caesar Cipher Python files is available at: https://github.com/geektechdude/Python_Caesar_Cipher

My previous posts on the Caesar Cipher can be found at:

Python:

https://geektechstuff.com/2018/02/25/caesar-cipher-python/

Python (with a graphical user interface):

https://geektechstuff.com/2018/12/10/caesar-cipher-with-gui-python/

Java:

https://geektechstuff.com/2019/06/11/caesar-cipher-java/