Caesar cipher

Caesar cipher

An encryption utility.

Try


Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

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

Command summary

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


Success Criteria

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.

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Run your code to check that your program has met the success criteria and produces the required output.

Enter a message: The quick brown fox jumps over the lazy dog.

Message to send: Ymj%vznhp%gwt|s%kt}%ozrux%t{jw%ymj%qf ~%itl3

Message is decrypted to: The quick brown fox jumps over the lazy dog.