Introduction:

On 15 December 2015, British astronaut Tim Peake set off on the Principia mission to the International Space Station which is in orbit around Earth. This project ties in with the The Principia Space Diary, a free resource for UK schools, produced as part of European Space Agency Astronaut Tim Peake’s Principia Mission. For more information about the Space Diary, visit http://principiaspacediary.org.

Space Diary
Space Diary

In this project, you’ll learn how to make your own encryption program, to send and receive secret messages with a friend. This project ties in with the “Earth to Principa” activity on page 16 of the Space Diary.

Step 1: The Caesar cipher

A cipher is a type of secret code, where you swap the letters around so that no-one can read your message.

You’ll be using one of the oldest and most famous ciphers, the Caesar cipher, which is named after Julius Caesar.

Before we start coding, let’s try using the Caesar cipher to hide a word.

Activity Checklist

  • Hiding a word is called encryption.

    Let’s start by encrypting the letter ‘a’. To do this, we can draw the alphabet in a circle, like this:

    screenshot
    screenshot
  • To make a secret encrypted letter from a normal one, you need to have a secret key. Let’s use the number 3 as the key (but you can use any number you like).

    To encrypt the letter ‘a’, you just move 3 letters clockwise, which will give you the letter ‘d’:

    screenshot
    screenshot
  • You can use what you’ve learnt to encrypt an entire word. For example, ‘hello’ encrypted is ‘khoor’. Try it yourself.

    • h + 3 = k
    • e + 3 = h
    • l + 3 = o
    • l + 3 = o
    • o + 3 = r
  • Getting text back to normal is called decryption. To decrypt a word, just subtract the key instead of adding it:

    • k - 3 = h
    • h - 3 = e
    • o - 3 = l
    • o - 3 = l
    • r - 3 = o

Challenge: Use a Caesar cipher

Can you send a secret word to a friend? You’ll both need to agree on a secret key before you start.

You could even send entire sentences to each other!

Step 2: Encrypting letters

Let’s write a Python program to encrypt a single character.

Activity Checklist

  • Open the blank Python template Trinket: jumpto.cc/python-new.

  • Instead of drawing the alphabet in a circle, let’s write it out as an alphabet variable.

    screenshot
    screenshot
  • Each letter of the alphabet has a position, starting at position 0. So the letter ‘a’ is at position 0 of the alphabet, and ‘c’ is at position 2.

    screenshot
    screenshot
  • You can get a letter from your alphabet variable by writing the position in square brackets.

    screenshot
    screenshot

    You can delete the print ststements once you’ve tried this out.

  • Next, you’ll need to store the secret key in a variable.

    screenshot
    screenshot
  • Next, ask the user for a single letter (called a character) to encrypt.

    screenshot
    screenshot
  • Find the position of the character.

    screenshot
    screenshot
  • You can test the stored position by printing it. For example, that character ‘e’ is at position 4 in the alphabet.

    screenshot
    screenshot
  • To encrypt the character, you should add the key to the position.

    screenshot
    screenshot
  • Test out your new code. As your key is 3, it should add 3 to the position and store it in your newPosition variable.

    For example, letter ‘e’ is at position 4. To encrypt, you add the key (3), giving 7.

    screenshot
    screenshot
  • What happens when you try and encrypt the letter ‘y’?

    screenshot
    screenshot

    Notice how the newPosition is 27, and there aren’t 27 letters in the alphabet!

  • You can use a % to tell the new position to go back to position 0 once it gets to position 26.

    screenshot
    screenshot
  • Finally, you want to print the letter at the new position.

    For example, adding the key to the letter ‘e’ gives 7, and the letter at position 7 of the alphabet is ‘h’.

    screenshot
    screenshot
  • Try out your code. You can also remove some of your print statements, just printing the new character at the end.

    screenshot
    screenshot

Save Your Project

Challenge: Variable keys

Modify your program, so that the user can enter their own key to use. You’ll need to get the user’s input, and store it in the key variable.

Remember to use the int() function to convert the input to a whole number.

You can then use a negative key to decrypt messages!

Save Your Project

Step 3: Encrypting entire messages

Instead of just encrypting and decrypting messages one character at a time, let’s change the program to encrypt entire messages!

Activity Checklist

  • Firstly, check that your code looks like this:

    screenshot
    screenshot
  • Create a variable to store the new encrypted message.

    screenshot
    screenshot
  • Change your code to store the user’s message and not just one character.

    screenshot
    screenshot
  • Add a for loop to your code, and indent the rest of the code so that it is repeated for each character in the message.

    screenshot
    screenshot
  • Test your code. You should see that each character in the message is encrypted and printed one at a time.

    screenshot
    screenshot
  • Let’s add each encrypted character to your newMessage variable.

    screenshot
    screenshot
  • You can print the newMessage as it is being encrypted.

    screenshot
    screenshot
  • If you delete the spaces before the print statement, the encrypted message will only be displayed once at the end. You can also delete the code for printing the character positions.

    screenshot
    screenshot

Save Your Project

Step 4: Extra characters

Some characters aren’t in the alphabet, which causes an error.

Activity Checklist

  • Test out your code with some characters that aren’t in the alphabet.

    For example, you could use the message hi there!!.

    screenshot
    screenshot

    Notice that the space and the ! characters are all encrypted as the letter ‘c’!

  • To fix this, you only want to translate a character if it’s in the alphabet. To do this, add an if statement to your code, and indent the rest of your code.

    screenshot
    screenshot
  • Test your code with the same message. What happens this time?

    screenshot
    screenshot

    Now, your code just skips any character if it’s not in the alphabet.

  • It would be better if your code didn’t encrypt anything not in the alphabet, but just used the original character.

    Add an else statement to your code, which just adds the original character to the encrypted message.

    screenshot
    screenshot
  • Test your code. You should see that any character in the alphabet is encrypted, but any other characters are left alone!

    screenshot
    screenshot

Save Your Project

Challenge: Encrypting and decrypting messages

Encrypt some messages, and give them to a friend along with the secret key. See if they can decrypt them using their program!

You could also duplicate the project and create a separate program for decrypting messages.

Save Your Project

Challenge: Friendship calculator

Write a program to show how compatible 2 people are, by calculating a friendship score.

screenshot
screenshot

The program could loop through each of the characters in the 2 names, and add points to a score variable each time certain letters are found.

You should decide on rules for awarding points. For example, you could award points for vowels, or characters that are found in the word “friend”:

screenshot
screenshot

You could also give the user a personalised message, based on their score:

screenshot
screenshot

Save Your Project