Python learning – Caesar Cipher

Get all the information about the old, famous and interesing Caesar Cipher (http://en.wikipedia.org/wiki/Caeser_cipher). In this post we will implement the Caeser Cipher Encoder (cce.py), Caeser Cipher Decoder (ccd.py) and Caeser Cipher Brute Force Decipher (ccbfd.py). Have FUN:) (BTW, I have suddenly realized that I have some typos in source code – Caeser should be Caesar…my bad:(

[daveti@aimlab python]$ cat cce.py
#!/bin/env python
# Caeser Cipher Ecoder – cce
# Input: original text and shift code
# Output: ciphered text
# 24SEP2012
# daveti

print (“Input the original text:”)
originalText = input()
print (“Input the shift code:”)
shiftCode = int(input())

originalText = originalText.upper()
cipheredText = ”
for i in range(len(originalText)):
    if originalText[ i].isalpha() == True:
        ordNum = ord(originalText[ i])+shiftCode
        if ordNum > ord(‘Z’):
            ordNum = ordNum – ord(‘Z’) + ord(‘A’) – 1
        cipheredText += chr(ordNum)
    else:
        # Do nothing
        cipheredText += originalText[ i]

print (“Output the ciphered text: ” + cipheredText)
[daveti@aimlab python]$ ./cce.py
Input the original text:
‘what is the hell?’
Input the shift code:
7
Output the ciphered text: DOHA PZ AOL OLSS?
[daveti@aimlab python]$ cat ccd.py
#!/bin/env python
# Caeser Cipher Decoder – ccd
# Input: ciphered text and shift code
# Output: original text
# 24SEP2012
# daveti

print (“Input the ciphered text:”)
cipheredText = input()
print (“Input the shift code:”)
shiftCode = int(input())

cipheredText = cipheredText.upper()
originalText = ”
for i in range(len(cipheredText)):
    if cipheredText[ i].isalpha() == True:
        ordNum = ord(cipheredText[ i])-shiftCode
        if ordNum < ord(‘A’):
            ordNum = ord(‘Z’) + 1 – (ord(‘A’) – ordNum)
        originalText += chr(ordNum)
    else:
        # Do nothing
        originalText += cipheredText[ i]

print (“Output the original text: ” + originalText)
[daveti@aimlab python]$ ./ccd.py
Input the ciphered text:
‘DOHA PZ AOL OLSS?’
Input the shift code:
7
Output the original text: WHAT IS THE HELL?
[daveti@aimlab python]$ cat ccbfd.py
#!/bin/env python
# Caeser Cipher Brute Force Decipher – ccbfd
# Input: ciphered text
# Output: original text with maximal possibility
# 25SEP2012
# daveti

# Letter frequency list (from A to Z)
letterGoodness = [ .0817, .0149, .0278, .0425, .1270, .0223, .0202, .0609, .0697,
                    .0015, .0077, .0402, .0241, .0675, .0751, .0913, .0009, .0599,
                    .0633, .0906, .0276, .0098, .0236, .0015, .0197, .0007 ]

# Assume the right input without error checking…
def computeGoodness(text):
    goodness = .0
    for i in range(len(text)):
        if text[i].isalpha() == True:
            goodness += letterGoodness[ord(text[i])-ord(‘A’)]
    return goodness

# Save all the possible decodings based on shiftCode
originalTextList = []
originalText = ”
# Save all the goodness for all the possible shiftCode
goodnessList = []
goodness = .0

print (“Input the ciphered text:”)
cipheredText = input()
cipheredText = cipheredText.upper()
lenOfCipheredText = len(cipheredText)

# i is shiftCode
for i in range(26):
    # Init the originalText each time
    originalText = ”
    for j in range(lenOfCipheredText):
        if cipheredText[ j].isalpha() == True:
            ordNum = ord(cipheredText[ j])-i
            if ordNum < ord(‘A’):
                ordNum = ord(‘Z’) + 1 – (ord(‘A’) – ordNum)
            originalText += chr(ordNum)
        else:
            # Do nothing
            originalText += cipheredText[ j]
    # Add this text into the list
    originalTextList.append(originalText)
    # Comput the goodness for this text and add into the list
    goodness = computeGoodness(originalText)
    goodnessList.append(goodness)
    # Debug options
    #print (‘DEBUG: shiftCode: ‘, i)
    #print (‘DEBUG: originalText: ‘, originalText)
    #print (‘DEBUG: goodness: ‘, goodness)

# Debug option
#print (‘DEBUG: goodnessList: ‘, goodnessList)
# Get the index of maximal value
maxIndex = 0
maxGoodness = .0
for k in range(len(goodnessList)):
    if goodnessList[ k] >= maxGoodness:
        maxGoodness = goodnessList[ k]
        maxIndex = k
# Debug option
#print (‘DEBUG: maxIndex: ‘, maxIndex)
#print (‘DEBUG: maxGoodness: ‘, maxGoodness)
# Find the text with maximal goodness
decipheredText = originalTextList[maxIndex]

print (“Output the most possible original text: ” + decipheredText)
[daveti@aimlab python]$ ./ccbfd.py
Input the ciphered text:
‘DOHA PZ AOL OLSS?’
Output the most possible original text: WHAT IS THE HELL?
[daveti@aimlab python]$

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s