An encryption utility.

Caesar cipher

Try

Note this is an advanced Caesar cipher that also encrypts punctuation and some special characters, including space. A standard Caesar cipher would only encrypt the letters A-Z.

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

The new commands used in this program and others that may be useful. Select them below to learn more:

x = chr(y)

x is assigned to be the character for the ASCII code y.

x = ord(y)

x is assigned to be the ASCII code for character y.

Investigate

Questions to think about with this program to check your understanding:

Relation question

Outline the relationship between the number 65 and the letter A, explaining why “A” != “a” and also why when sorted, “103” is before “23”.

Reveal answer

Every character is stored as a number by the computer. The letter “A” is the number 65. “a” is 97, so “A” and “a” are not the same because 65 is not 97.

The alphanumeric character “1” is stored as the number 49 and “2” is stored as 50, so 49 is before 50. Each character is taken in turn when data is sorted in a simplistic way.

Approach question

Explain why using ord and chr in lines 10 and 14 is the easiest way to approach this program.

Reveal answer

The key requires a shift of five characters. It is much easier to convert a character to a number, add five and then convert it back again. Otherwise you would need a very long program with lots of conditions, or an array to hold the data. Using the built-in ASCII table is a simple solution.

Make

Change the program so that it:

  1. Has a decrypt_message function that takes an encrypted message and returns the plain text message.
  2. Outputs a decrypted message in plain text.

Typical inputs and outputs from the program would be:

Enter a message: Hello World
Message to send: Mjqqt%\twqi
Message is decrypted to: Hello World
Enter a message: The weather is cloudy with a small chance of rain.
Message to send: Ymj%|jfymjw%nx%hqtzi~%|nym%f%xrfqq%hmfshj%tk%wfns3
Message is decrypted to: The weather is cloudy with a small chance of rain.