Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Caesar Cipher Decoder

In my lesson I was tasked with creating a Caesar Cipher decoder that takes a string of input and finds the best possible string using a letter frequencies. If not sure how much sense that made but let post the question:

Write a program which does the following. First, it should read one line of input, which is the encoded message, and will consist of capital letters and spaces. Your program must try decoding the message with all 26 possible values of the shift S; out of these 26 possible original messages, print the one which has the highest goodness. For your convenience, we will pre-define the variable letterGoodness for you, a list of length 26 which equals the values in the frequency table above

Letter Frequencies

I have this code so far:

x = input()
NUM_LETTERS = 26 #Can't import modules I'm using a web based grader/compiler
def SpyCoder(S, N):
    y = ""
    for i in S:
        x = ord(i)
        x += N
        if x > ord('Z'):
            x -= NUM_LETTERS
        elif x < ord('A'):
            x += NUM_LETTERS
        y += chr(x)
    return y  

def GoodnessFinder(S):
    y = 0
    for i in S:
        if x != 32:
            x = ord(i)
            x -= ord('A')
            y += letterGoodness[x]
    return y 

def GoodnessComparer(S):
    goodnesstocompare = GoodnessFinder(S)
    goodness = 0
    v = ''
    for i in range(0, 26):
        v = SpyCoder(S, i)
        goodness = GoodnessFinder(v)
        if goodness > goodnesstocompare:
            goodnesstocompare = goodness
    return v

y = x.split()
z = ''
for i in range(0, len(y)):
    if i == len(y) - 1:
        z += GoodnessComparer(y[i])
print(z)

EDIT: Made changes suggested by Cristian Ciupitu Please ignore indentation errors, they probably arose when I copied my code over.

The program works like this:

  • Take the input and split it into a list
  • For every list value I feed it to a goodness finder.
  • It takes the goodness of the string and compares everything else against and when there's a higher goodness it makes the higher one the goodness to compare.
  • It then shifts that string of text by i amount to see if the goodness is higher or lower

I'm not quite sure where the problem is, the first test: LQKP OG CV GKIJV DA VJG BQQ
Prints the correct message: JOIN ME AT AT BY THE ZOO

However the next test: UIJT JT B TBNQMF MJOF PG UFYU GPS EFDSZQUJOH
Gives the a junk string of: SGHR HR Z RZLOKD KHMD NE SDWS ENQ CDBQXOSHMF
When it's supposed to be: THIS IS A SAMPLE LINE OF TEXT FOR DECRYPTING

I know I have to:
Try every shift value
Get the 'goodness' of the word
Return the string with the highest goodness.

I hope my explanation made sense as I am quite confused at the moment.

like image 950
Thegluestickman Avatar asked May 29 '12 01:05

Thegluestickman


1 Answers

Here is my implementation which works fine.

You should print the goodness of each possible message and see why your program output it.

letterGoodness = dict(zip(string.ascii_uppercase,
                        [.0817,.0149,.0278,.0425,.1270,.0223,.0202,
                         .0609,.0697,.0015,.0077,.0402,.0241,.0675,
                         .0751,.0193,.0009,.0599,.0633,.0906,.0276,
                         .0098,.0236,.0015,.0197,.0007]))

trans_tables = [ str.maketrans(string.ascii_uppercase,
                 string.ascii_uppercase[i:]+string.ascii_uppercase[:i])
                 for i in range(26)]

def goodness(msg):
    return sum(letterGoodness.get(char, 0) for char in msg)

def all_shifts(msg):
    msg = msg.upper()
    for trans_table in trans_tables:
        txt = msg.translate(trans_table)
        yield goodness(txt), txt

print(max(all_shifts(input())))
like image 194
Kabie Avatar answered Oct 11 '22 13:10

Kabie