First Solution

  • Lookup character in alphabet
  • Apply cipher key
  • Find new character
In [ ]:
alphabet = "abcdefghijklmnopqrstuvwxyz"
input_text = "and the mome raths outgrabe"
cipher_key = 3

character = input_text[0]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
print(new_letter)

character = input_text[1]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
print(new_letter)

character = input_text[2]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
print(new_letter)

character = input_text[3]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
print(new_letter)

Building Up the Cipher Text

In [ ]:
alphabet = "abcdefghijklmnopqrstuvwxyz"
input_text = "twas brillig in the slithy tove"
cipher_key = 3

cipher_text = ""

character = input_text[0]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
cipher_text = cipher_text + new_letter
print(cipher_text)

character = input_text[1]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
cipher_text = cipher_text + new_letter
print(cipher_text)

character = input_text[2]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
cipher_text = cipher_text + new_letter
print(cipher_text)

character = input_text[3]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
new_letter = alphabet[new_position]
cipher_text = cipher_text + new_letter
print(cipher_text)

Wrapping the Alphabet Search

In [4]:
alphabet = "abcdefghijklmnopqrstuvwxyz"
input_text = "twas"
cipher_key = 15

cipher_text = ""

character = input_text[0]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
if new_position >= len(alphabet):
    new_position = new_position - len(alphabet)
new_letter = alphabet[new_position]
cipher_text = cipher_text + new_letter
print(cipher_text)

character = input_text[1]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
if new_position >= len(alphabet):
    new_position = new_position - len(alphabet)
new_letter = alphabet[new_position]
cipher_text = cipher_text + new_letter
print(cipher_text)

character = input_text[2]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
if new_position >= len(alphabet):
    new_position = new_position - len(alphabet)
new_letter = alphabet[new_position]
cipher_text = cipher_text + new_letter
print(cipher_text)

character = input_text[3]
character_position = alphabet.find(character)
new_position = character_position + cipher_key
if new_position >= len(alphabet):
    new_position = new_position - len(alphabet)
new_letter = alphabet[new_position]
cipher_text = cipher_text + new_letter
print(cipher_text)
i
il
ilp
ilph

Dealing with Arbitrary-Length Input

In [ ]:
alphabet = "abcdefghijklmnopqrstuvwxyz"
input_text = "twas brillig in the slithy tove"
cipher_key = 15

cipher_text = ""

for character in input_text:
    character_position = alphabet.find(character)
    new_position = character_position + cipher_key
    if new_position >= len(alphabet):
        new_position = new_position - len(alphabet)
    new_letter = alphabet[new_position]
    cipher_text = cipher_text + new_letter
    print(cipher_text)

Expanding the Alphabet

In [ ]:
alphabet = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
input_text = "$@Twas brillig in the slithy tove"
cipher_key = 1

cipher_text = ""

for character in input_text:
    if character in alphabet:
        character_position = alphabet.find(character)
        new_position = character_position + cipher_key
        if new_position >= len(alphabet):
            new_position = new_position - len(alphabet)
        new_letter = alphabet[new_position]
        cipher_text = cipher_text + new_letter
    else:
        cipher_text = cipher_text + character
    
print(cipher_text)

Getting Input

In [ ]:
alphabet = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
input_text = raw_input("Enter your input text: ")
cipher_key = raw_input("Enter a number between 0 and " + str(len(alphabet)) + ": ")
cipher_key = int(cipher_key)

cipher_text = ""

for character in input_text:
    if character in alphabet:
        character_position = alphabet.find(character)
        new_position = character_position + cipher_key
        if new_position >= len(alphabet):
            new_position = new_position - len(alphabet)
        new_letter = alphabet[new_position]
        cipher_text = cipher_text + new_letter
    else:
        cipher_text = cipher_text + character
    
print(cipher_text)

Decrypting

In [ ]:
alphabet = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
direction = raw_input("Encrypt or decrypt? (d/e): ")
input_text = raw_input("Enter your input text: ")
cipher_key = raw_input("Enter a number between 0 and " + str(len(alphabet)) + ": ")
cipher_key = int(cipher_key)

if direction == "d":
    cipher_key = -cipher_key

cipher_text = ""

for character in input_text:
    if character in alphabet:
        character_position = alphabet.find(character)
        new_position = character_position + cipher_key
        if new_position >= len(alphabet):
            new_position = new_position - len(alphabet)
        new_letter = alphabet[new_position]
        cipher_text = cipher_text + new_letter
    else:
        cipher_text = cipher_text + character
    
print(cipher_text)

Breaking

In [25]:
alphabet = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

def caesar_cipher(input_text, cipher_key):
    cipher_text = ""
    
    for character in input_text:
        if character in alphabet:
            character_position = alphabet.find(character)
            new_position = character_position + cipher_key
            if new_position >= len(alphabet):
                new_position = new_position - len(alphabet)
            new_letter = alphabet[new_position]
            cipher_text = cipher_text + new_letter
        else:
            cipher_text = cipher_text + character
    
    return cipher_text

direction = raw_input("Encrypt, decrypt, or break? (d/e/b): ")
user_input_string = raw_input("Enter your input text: ")
cipher_text = ""

if direction == "b":
    for count in range(len(alphabet)):
        output_cipher_text = caesar_cipher(user_input_text, -count)
        print(output_cipher_text)
else:
    cipher_key = raw_input("Enter a number between 0 and " + str(len(alphabet)) + ": ")
    cipher_key = int(cipher_key)

    if direction == "d":
        cipher_text = caesar_cipher(user_input_text, -cipher_key)
    elif direction == "e":
        cipher_text = caesar_cipher(user_input_text, cipher_key)

    print(cipher_text)
Encrypt, decrypt, or break? (d/e/b): e
Enter your input text: something
Enter a number between 0 and 63: 1
tpnfuijoh

Input Checking

In [ ]:
alphabet = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

def caesar_cipher(input_text, cipher_key):
    cipher_text = ""
    
    for character in input_text:
        if character in alphabet:
            character_position = alphabet.find(character)
            new_position = character_position + cipher_key
            if new_position >= len(alphabet):
                new_position = new_position - len(alphabet)
            new_letter = alphabet[new_position]
            cipher_text = cipher_text + new_letter
        else:
            cipher_text = cipher_text + character
    
    return cipher_text

            
direction = ""
valid_option = False
while valid_option == False:
    direction = raw_input("Encrypt, decrypt, or break? (d/e/b): ")
    if direction in "bed":
        valid_option = True
    else:
        print("Option not recognized. Please try again.")

    
user_input_text = raw_input("Enter your input text: ")
output_cipher_text = ""



if direction == "b":
    for count in range(1, len(alphabet)):
        output_cipher_text = caesar_cipher(user_input_text, -count)
        print(output_cipher_text)
else:
    cipher_key = raw_input("Enter a number between 0 and " + str(len(alphabet)) + ": ")
    cipher_key = int(cipher_key)

    if direction == "d":
        output_cipher_text = caesar_cipher(user_input_text, -cipher_key)
    elif direction == "e":
        output_cipher_text = caesar_cipher(user_input_text, cipher_key)

    print(output_cipher_text)