Download Caesar Cipher Project: Encrypting and Decrypting Messages and more Study notes Printing in PDF only on Docsity!
Girls’ Programming Network
Cryptography
Create a Caesar Cipher encryptor and decryptor!
This project was created by GPN Australia for GPN sites all around Australia!
This workbook and related materials were created by tutors at:
Sydney, Canberra and Perth Girls’ Programming Network
If you see any of the following tutors don’t forget to thank them!!
Writers Testers
Renee Noble Sarah Mac Alex McCulloch Jess Austin Bhavna Yadav Lyndsey Thomas Courtney Ross Maddie Jones Jess Austin Maggie Liuzzi
Cipher Wheels
Sometimes we want to use different keys. So we want a different amount of rotation. We’ve given you a cipher wheel that lets you do this rotation! Grab your cipher wheel! You can rotate the inner wheel to try different shifts. Let’s give it a try!
Task 0.2: Encrypt with a key of 10!
Let’s try using a different key. Let’s try 10. Rotate your cipher wheel so the green A lines up with the purple K. Encrypt this message:
MYSTERY → __ __ __ __ __ __ __
Task 0.3: Encrypt with a key of 24!
Let’s try a key of 24. Rotate your wheel 24 spots and encrypt this message. (You can ignore spaces)
GPN IS GREAT → __ __ __ __ __ __ __ __ __ __
Task 0.4: Decrypt with a key of 7!
Let’s try a key of 7. Rotate your wheel 7 spots and decrypt this message:
JYFWAVNYHWOF → __ __ __ __ __ __ __ __ __ __ __ __
Task 0.5: Decrypt with a key of 11!
Let’s try a key of 11. Rotate your wheel 11 spots and decrypt this message:
DATY ESP HSPPW → __ __ __ __ __ __ __ __ __ __ __ __
CHECKPOINT If you can tick all of these off you can go to Part 1: ☐ You used your cipher wheel! ☐ You encrypted all the message!
☐ You decrypted all the secret messages! Part 1: Setting up
Task 1.1: Making a python file
Open the start menu, and type 'IDLE’. Select IDLE Python 3.
- Go to the file menu and select 'New File’. This opens a new window.
- Go to the file menu, select ’Save As’
- Choose Desktop and save the file as ‘caesar_cipher.py’
Task 1.2: You’ve got a blank space, so write your name!
At the top of the file use a comment to write your name! Any line starting with # is a comment.
This is a comment
CHECKPOINT If you can tick all of these off you can go to Part 2: ☐ You should have a file called caesar_cipher.py ☐ Your file has your name at the top in a comment ☐ Run your file with F5 key and it does nothing!! Intro to Pytho n
Task 2.5: Print your secret message
1. Let’s print our secret message that we got from the user earlier.
CHECKPOINT If you can tick all of these off you can go to Part 3:
☐ Print a welcome message
☐ Ask for secret message
☐ Ask for a key to encrypt the the secret message with
☐ Convert key to integer format
☐ Print your secret message
☐ Run your code
★ BONUS 2.6: What’s the name of your secret language? Waiting for the next lecture? Try adding this bonus feature!!
Let’s come up with the name of our secret language and print the language of our
secret message. ★ BONUS 2.7: Customize welcome message Waiting for the next lecture? Try adding this bonus feature!! Let’s customise the welcome message for the user.
For example, if the user typed in their name was Lyndsey, then print the welcome
message for Lyndsey.
Welcome to my amazing cipher, Lyndsey
Part 3: Getting a secret letter
Task 3.1 How do we get a secret letter?
Let’s try doing this with pen and paper!
- Get the first letter of the word and write it in the second column below.
- Count how far through the alphabet that letter is. You may want to use your cipher wheels to count with. Put this in the third column. Remember that computers start counting at 0, so you should count like a computer does!
- Using 5 as the key , add 5 to the index of that first letter to get our secret letter index. Put that in the fourth column.
- Now count through the alphabet to find what letter is at the secret letter index. Write it in the last column. Remember again that computers start counting from 0!
- In the next steps, we’ll write code to do this for us! Message First letter Letter Index Secret Letter Index Secret Letter apple a 0 5 f banana carrot grapes
Hint
Counting the wheel can take a long time. You can use this table to look up the indexes of the letters: 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 0 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
Task 3.2: Let’s store the alphabet!
- Write a variable that stores the alphabet in a string like this: alphabet = 'abcdefghijklmnopqrstuvwxyz' String s and Ints
Task 3.5: Turn the key!
So we’ve got the position of the original letter, now we have to use the key to shift it to get a new index (later we’ll use this to get the secret letter). To do that we need to work out the index of the secret letter which will be the current_index plus the key.
- Make a variable called new_index, set this using the formula above to get the the location of the secret letter.
- Test your code using 5 as the key, print out the new_index and write what number is printed for each of these examples to make sure it’s right! Remember that it should be what you wrote down for the last task plus your key! Message current_letter current_index new_index gpn abracadabra pizza zoink
Hint
For example if we wanted to take the message “cipher” the table would look like this: Message current_letter current_index new_index cipher c 2? Since our key is 5 and our current_index is 2, we can work out that new_index should be the current_index plus the key, so it’s 5+2 which is 7!
Task 3.6: Secret Letter!
Next let’s use the new index to get the secret letter from the alphabet.
- Make a variable called new_letter to store the letter in the alphabet that is at the new_index position.
- Print the new letter!
- Fill out the table below for each of the examples using 5 as the key. Message current_letter current_index new_index new_letter gpn abracadabra pizza zoink Did it work with zoink? Don’t worry, we’re going to fix it in the next task!
Hint
Remember that you can get a letter out of a string like this: name = "Alex" index = 2 letter = name[index]
Task 3.7: Zoink!
- What happens when you put in “zoink” and a key of 5? You get and error! This is because when you try to go 5 letters past ‘z’ in the alphabet, there isn’t a letter there! To fix this we need to make the index wrap around back to 0 when it gets to the end of the alphabet. We can use modulo to make our code do this!
- Make the new_index be the new_index modulo 26
- Overwrite new_index with the modulo version of itself
Hint
To modulo something by 5 you do this: number = 21 number = number % 5
Part 4: What about words?
Task 4.1: Not just one letter
- On your cipher wheel, try and encrypt ‘gpn’ with a key of 4 , what letters do you get?
gpn = ________
- Now try putting “gpn” with a key of 4 into your program and seeing if they match.
Did it match what you did by hand?
Right now your code only gets the first letter of the message variable. The computer doesn’t know that there are more letters in message, we need to tell it!
Task 4.2: Not just one letter
- First we can remove the code that sets the current_letter variable to the first letter of your message. You can do this by making the whole line into a comment.
Hint
Remember:
this is a comment
Task 4.3: Working letter by letter
Now we want to add a for loop so that the computer looks at every letter in the message and turns it into a secret letter.
- Add the for loop right under the comment you just made.
- Your for loop should loop through the message, this will give us the current_letter one at a time.
Hint
Here’s an example of a for loop. Don’t forget that when you code a for loop you need to use indents. for letter in word: print(letter) Loops
Task 4.4: Check the whole secret word
- Use your cipher wheel to encrypt a 3 letter word, like ‘gpn’, rotated by 6.
__ __ __ → __ __ __
- Now type that word and key into your encryptor and see if they match.
- If it’s working you can move onto the next step.
Task 4.5: Everybody get in line!
- Try your program with a long word like ‘zooperdooper’. The secret word is way too L O N G ! Let’s print it on one line so that it is easy to read.
Hint
Use the print(variable, end='') that you learned in lecture slides to print your secret word on one line. CHECKPOINT If you can tick all of these off you can go to Part 4:
☐ Used a comment to remove a line of code
☐ Added a for loop
☐ Check your code with a 3 letter word
☐ Print the word on one line
Part 5 : Dealing With Spaces
Task 5.1: Testing With A Space!
- Test your code with 2 words now instead on one, what happens? You should get a ValueError, this is because the program doesn’t know what a space is!
Task 5.2: What If?
To fix this issue, we are going to add some if statements to check if the character is in the alphabet variable.
- In your loop add a line to check if the current letter is in the alphabet
- If the character is in the alphabet, we want to make it secret with our code - indent your code into the if statement
Task 5.3: Ignore the spaces
- Create an else statement to handle when characters are not in the alphabet.
- Inside the else statement, print the character you are on, but don’t encrypt it. Make sure they stay on the same line too.
Hint
An else always needs to be attached to an if statement if raining == True: print ('oh no!') else: print ('Yay!') CHECKPOINT If you can tick all of these off you can go to Part 6: ☐ You have an if and else statement ☐ If your code can make more than one word secret ☐ Print out a secret sentence If Stateme nts
Part 6: Let’s Get Cracking!
Task 6.1: Make a Secret Code!
Before we can crack some codes, we need a message to decrypt. Use your program to get a secret message that we can decrypt (write down your key!!) Fill in the table below so that we can decrypt it later! Original Message Key Encrypted Message
Task 6.2: To encrypt or decrypt?
At the moment your program can make a secret code, but if we gave that code to someone else, they won’t be able to read it! We will need to add a mode where you can turn the secret message back into normal words using the same key that we used to make the secret message. We can call the modes ‘e’ for encrypt and ‘d’ for decrypt.
- Use an input() statement before the for loop to ask the user which mode they would like to use.
Task 6.3: Not So Secret Anymore!
If the user chooses mode ‘d’ we will need to change the key value to become the opposite of the encryption key value
- Go to just after where you get your integer version of your key.
- On the next line add an if statement that checks what mode the user entered. We want to check if we are in decrypting mode.
- If we are in decrypting mode we want to multiply the key by - 1. Overwrite the current value with this new value. For example: If the letters in the word were changed by 3 letters to encrypt the secret word, the letters need to be changed back by 3 letters.
7. Extension: Random Keys? So far we have had to choose a number to shift the letters by. Now, we want to let the computer choose what number to use by typing ‘random’ when you ask how many letters to rotate by.
Task 7.0: Let’s get random!
First we need to import random at the top of our program!
Task 7.1: Letters or numbers?
Now, when we ask the user for a key they can either type a number (the key) or the word “random”.
- Add an if statement to check if the key they entered is the word "random" before the rest of your program.
- Also add an else so that if they put in a number, the key is set to that number!
Hint
Don’t forget to use int() when you set the key to be the number
Task 7.2: So random
If the input is ‘"random" the computer should pick a random number and store it in the key variable. Write code that chooses a random number between 1 and 25 and stores it in the key variable.
Hint
To get a random number between 1 and 10 you would write it like this: number = random.randrange(1, 11)
Task 7.1: Write it down!
We need to know what the random number is so that we can give it to our friends to decode the message later. Add a print statement that prints the key before the secret message.
8. Extension: While loop What if we have heaps of messages to encrypt and decrypt? Let’s loop our whole program so that we can use it for heaps of messages!
Task 8.1: Whiling away the while
To make the code run over and over and over again so that we can keep encrypting all of our secret messages, we want to add a while loop near the top of the program.
1. Add a while True loop to your program!
Hint
Remember that you only want to put things you want to do EVERY TIME inside the while loop, so printing out the welcome statement and making your alphabet variable should be before the loop. Remember while loops look like this:
while :
statement
For our loop to run continuously, we can use while True:
Task 8.2: Add new line
Did you notice that Do you want to encrypt or decrypt doesn’t appear on a
new line when it runs in a loop? Look at the text in red in the below box.
Welcome to the Caesar Cipher!
Do you want to encrypt or decrypt (e/d): e
What is your message: Hello
How many letters to rotate by: 4
ippso Do you want to encrypt or decrypt (e/d):
- Use and empty print statement to print a blank line at the end of each loop