Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Caesar Cipher Project: Encrypting and Decrypting Messages, Study notes of Printing

Instructions for creating a Caesar Cipher project using Python. It covers the basics of Caesar Ciphers, encrypting and decrypting messages, setting up the Python file, and getting a secret letter. It also includes a bonus task to handle words instead of just letters.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

mathieu
mathieu šŸ‡®šŸ‡¹

4.2

(11)

235 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Girls’ Programming Network
Cryptography
Create a Caesar Cipher encryptor and decryptor!
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

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.

  1. Go to the file menu and select 'New File’. This opens a new window.
  2. Go to the file menu, select ’Save As’
  3. 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!

  1. Get the first letter of the word and write it in the second column below.
  2. 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!
  3. 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.
  4. 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!
  5. 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!

  1. 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.

  1. Make a variable called new_index, set this using the formula above to get the the location of the secret letter.
  2. 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.

  1. Make a variable called new_letter to store the letter in the alphabet that is at the new_index position.
  2. Print the new letter!
  3. 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!

  1. 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!
  2. Make the new_index be the new_index modulo 26
  3. 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

  1. On your cipher wheel, try and encrypt ā€˜gpn’ with a key of 4 , what letters do you get?

gpn = ________

  1. 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

  1. 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.

  1. Add the for loop right under the comment you just made.
  2. 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

  1. Use your cipher wheel to encrypt a 3 letter word, like ā€˜gpn’, rotated by 6.

__ __ __ → __ __ __

  1. Now type that word and key into your encryptor and see if they match.
  2. If it’s working you can move onto the next step.

Task 4.5: Everybody get in line!

  1. 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!

  1. 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.

  1. In your loop add a line to check if the current letter is in the alphabet
  2. 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

  1. Create an else statement to handle when characters are not in the alphabet.
  2. 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.

  1. 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

  1. Go to just after where you get your integer version of your key.
  2. 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.
  3. 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ā€.

  1. Add an if statement to check if the key they entered is the word "random" before the rest of your program.
  2. 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):

  1. Use and empty print statement to print a blank line at the end of each loop