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)
Here is a variable that contains a string.
jabberwocky = "Did gyre and gimble in the wabe:"
print(jabberwocky)
input_text = "all mimsy were the borogoves"
input_text[0] # This should be a
cipher_key = 15
alphabet = "abcdefghijklmnopqrstuvwxyz"
print(alphabet[0 + cipher_key])
print(alphabet[1 + cipher_key])
print(alphabet[2 + cipher_key])
print(alphabet[3 + cipher_key])
.find(letter)
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)
print("Beware the Jabberwock," + " " + "my son!")
spam = "email"
eggs = "bacon"
print (spam + eggs)
So far, we've been writing programs that do the same thing repeatedly. If order for a program to be more reactive, we need a way for it to make decisions. Depending on whether something is the case, it takes one action or another.
temperature = 10
if (temperature < 5):
print("Wear a coat.")
We can use decision structures to adjust string indexes that are too big or too small.
if the index is too big then ...
alphabet = "abcdefghijklmnopqrstuvwxyz "
index = 25
print(len(alphabet))
print(str(index) + ": " + str(alphabet[index]))
index = index + 1
print(str(index) + ": " + str(alphabet[index]))
index = index + 1
# print(alphabet[27])
if index >= len(alphabet):
index = index - len(alphabet)
print(str(index) + ": " + str(alphabet[index]))
index = index + 1
if index >= len(alphabet):
index = index - len(alphabet)
print(str(index) + ": " + str(alphabet[index]))
So far, we have been repeating actions by repeating lines of code.
If we are going to encrypt longer strings, we need to make this easier.
This is where repetition structures come in.
for number in [1, 2, 3, 4]:
print(number)
for character in "The jaws that bite, the claws that catch!":
print(character)
So far, the program can only handle lowercase characters. We can expand the set of characters to include:
But we'll probably run into something that's not yet in our alphabet.
How can we fix this?
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)
Let's change this.
Let's start with the last one.
sentence = raw_input("Type in a sentence: ")
number = int(raw_input("Enter a number between 0 and 25: "))
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 the Caesar cipher is the same as the encryption process but in reverse.
Let's try doing this on paper.
Exchange a cipher text and cipher key with someone else. Decrypt the cipher text.
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)
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)
Modern methods of breaking an encryption are based on statistical techniques.
But the Caesar cipher is a pretty simple linear cipher. We can try using "brute force"
Let's write a breaking program using the existing code as a starting point.
def function_name():
statement
statement
statement
# This program demonstrates a function
# First we define a function named "message"
def message():
print("I am Arthur")
print ("King of the Britons")
# Call the message function
message()
def holy_hand_grenade(tick):
output = ""
if tick == 1:
output = "Keepest going"
elif tick == 2:
output = "Nor either count thou two, excepting that thou then proceed to three"
elif tick == 3:
output = "Lobbest thou thy Holy Hand Grenade of Antioch towards thou foe"
elif tick == 4:
output = "Four shalt thou not count"
elif tick == 5:
output = "Five is right out"
return output
holy_hand_grenade(3)
# Turn this code into a function
# Input? temperature
# Processing? decision
# Output? string
input_temp = 10
def what_to_wear(temperature):
output = ""
if (temperature < 15):
output = "Wear a coat."
return output
attire = what_to_wear(input_temp)
print(attire)
alphabet = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
input_string = raw_input("Enter your input text: ")
def decrypt(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
for key in range(len(alphabet)):
possible_string = decrypt(input_string, key)
print(possible_string)
for num in range(10):
print(num)
The ability to say "I can write a computer program to solve that problem."