Home Cracking the Caesar Shift Cipher with Python
Post
Cancel

Cracking the Caesar Shift Cipher with Python

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

I recently started learning Python and have created a small script which can encrypt or decrypt plaintext or cipher text from a Caesar Shift Cipher. It’s nothing special but I’m curious as to how much easier it can be accomplished. I’m sure my method is very long winded and substantially more complicated than it needs to be.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#Creates the base Alphabet which is used for finding preceeding characters from the ciphertext.
baseAlphabet = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
print ("Welcome to a python Caesar shift cipher analyser. You will first be asked to enter the cipher text to be decrypted and then the amount of shifts you want to perform. Entering ALL for the shift amount will iterate through all 26 combinations.")
cipherText = raw_input("Please enter the Cipher text")
shiftAmount = raw_input("Please enter the shift amount. Enter ALL for brute force shifting.")
if shiftAmount == "all":
    shiftAmount = str(shiftAmount)
else:
    shiftAmount = int(shiftAmount)
baseLetterIndex = 0
completePlainText = [] #The variable each processed letter is appended to
def shiftAndStore(shift):
    for increment in cipherText:
        for value in baseAlphabet:
                    if value == increment:
                            while shift + baseAlphabet.index(value) >= 26: #Checks if the shift will be higher more than the 26 letters of the alphabet.
                                shift = shift - 26 #If shifts are higher 26 is subtracted till the shift and base alphabet value is lower than 26.
                            baseLetterIndex = baseAlphabet.index(value) + shift #assigns the index value of the processed letter to baseLetterIndex variable.
                            completePlainText.append(baseAlphabet[baseLetterIndex]) # appends the processed letter to the completePlainText variable.
                    if increment == (" "): #Handles the spaces Temporarily makes the value of increment X to prevent it looping 26 times.
                        completePlainText.append(" ")
                        increment = ("X")


if shiftAmount == "all": #Checks weather user selected brute force method or specific shift value.
    shiftAmount = int(0)
    while shiftAmount < 25: #Iterates through all 26 combinations producing a processed value each time.
        shiftAndStore(shiftAmount)
        shiftAmount = shiftAmount + 1
        print "The Encoded / Decoded text on shift  " + str(shiftAmount - 1) + " is " + (''.join(completePlainText)) #Prints the shift amount and processed text.
        completePlainText = []
else: #Executed if specific shift value is chosen and not brute force.
    shiftAndStore(shiftAmount)
    print "The Shift Amount is " + str(shiftAmount)
    print "The Encoded / Decoded text is " + (''.join(completePlainText))
This post is licensed under CC BY 4.0 by the author.