COS 100: Introduction to Programming
Interim 2022
HW Project 07: Encryption
Due: 01/20 Thu 10pm
Now therefore, I pray, if I have found grace in Your sight, show me now Your way, that I may know You and that I may find grace in Your sight.
Exodus 33:13a
If you are pair programming, do NOT start coding without your partner.
You should read the assignment first, feel free to think about it, but do not actually start coding.
Also, please read pair programming guidelines before your first meeting.
Project goal
Encrypt a message (string) by using the Caesar cipher.Background information
- Julius Caesar has a lot of enemies, so he would like to transmit coded messages to his friends.
- Suppose he wants to tell his friend Brutus what he wants for dinner.
- Caesar and Brutus agreed beforehand on a secret number called the key. (Caesar usually used the number 3 as the key, but any integer works.)
- Caesar replaces each letter in his message by some fixed position down the alphabet.
- For example, if the message is "Tofu" and the key is 3, he would transmit "Wrix" instead. This is because "W" is 3 letters after "T", "r" is 3 letters after "o", and so on.
- We call this step encryption: Caesar has encrypted his original message ("Tofu") into an encrypted message ("Wrix") using the secret key.
- Upon receiving the message, Brutus can recover the original message by shifting every letter backwards by 3 positions.
- We call this step decryption: Brutus has decrypted the encrypted message ("Wrix") back to the original message ("Tofu") using the secret key.
- This simple encryption/decryption scheme is called the Caesar cipher.
Project specification
- Write a program that prompts the user for a secret key, a message, and prints out the encrypted message using the Caesar cipher.
-
You should put the computational heavy-lifting portion into a function named
caesar_cipher
.- Parameters: a string message and an integer key, in that order.
- Encrypt/shift the message with the key using the Caesar cipher.
- Return: the encrypted/shifted message.
- Note: To facilitate grading, you must make a function named exactly
caesar_cipher
, and it should accept two arguments in exactly this specified order, and it should return (not print) the encrypted message.
- As usual, to complete your program, you should write (and call) a main function. Its job would be to do everything else: to prompt the user for stuff, call the function you wrote, and print out the returned result.
Notes and hints
-
Name your project source code
encrypt.py
. -
Do NOT type out the letters "A" through "Z" and have 26 cases in some kind of long
if
-elif
-...-elif
-else
chain. Even if your program works, you will lose points if you do it that way. (Hint: the letter "B" does not occur anywhere in my sample solution. It shouldn't appear in yours either.) -
Instead, you should use the built-in functions
ord
andchr
. They allow you to convert a character to its ASCII value and back. The ASCII values of letters are conveniently sequential: the ASCII value of "C" is 2 more than the ASCII value of "A". See Zelle 5.4 for more information. -
Here are some more details of what
caesar_cipher
should do in various situations.-
Make the shifts cyclic:
caesar_cipher("Stuvw", 5)
should return"Xyzab"
. -
Allow negative keys: one nice thing about the Caesar cipher is that to decrypt, we can shift with the negative of the key:
caesar_cipher("Xyzab", -5)
should return"Stuvw"
. -
Allow large (positive and negative) keys: handle larger shifts simply by wrapping around the alphabet many times:
caesar_cipher("Stuvw", 100)
should return"Opqrs"
. -
Try to make your code shift letters only and leave other things intact:
caesar_cipher("23 is a great number! *^_^*", 23)
should return"23 fp x dobxq krjybo! *^_^*"
.
-
Make the shifts cyclic:
-
To check if a character is a letter, you might need to compare its ASCII value with 65 and 90, which are the ASCII values of "A" and "Z".
In your code, you should use
ord("A")
andord("Z")
when you need these numbers, instead of typing65
and90
directly. This makes your code easier to read and understand (otherwise someone might wonder what those numbers mean). - Numbers like 65 and 90 that are not meaningful to non-programmers (but make your code work) are called magic numbers. The only number you are allowed to have in your code is 26, the number of letters in the English alphabet. You will lose points if your code contains any other "magic" numbers.
- It is often convenient to use a key of 13, because then both encryption and decryption is the same process of shifting by 13. This is known as rot13. I personally use this a lot when sending emails to friends that may contain spoilers. Unfortunately, we use it so much we start to be able to recognize certain words even after being rot13'd.
Suggested order of development
-
In the Shell, play around with
ord
andchr
until you fully understand how to convert between characters and their ASCII values. -
Make
caesar_cipher
work for the case where the message is a single uppercase letter and the key is 1. - Make sure "Z" wraps around to "A".
- Make it work when the key is 5. Make sure "W" wraps around to "B".
- Make it work when the key is -5.
- Make it work when the key is 100.
- Extend it so the message can be a string of uppercase letters.
- Extend it so the message can be a mixture of uppercase and lowercase letters.
- Extend it so the message can have non-letters, which are to be left intact.
Sample runs
-
Secret key: 23 Text to shift: Methinks 23 is a great number! *^_^* Shifted message is: Jbqefkhp 23 fp x dobxq krjybo! *^_^*
-
Secret key: -23 Text to shift: Jbqefkhp 23 fp x dobxq krjybo! *^_^* Shifted message is: Methinks 23 is a great number! *^_^*
Grading
- Total points: 20
caesar_cipher
function: 12main
function: 4- Style: 4
- Follow all the previous style guidelines, and also:
- Do not use "magic numbers" (besides the number 26).
- Do not type out "ABC...Z", or have 26 cases, etc.
Optional challenge
- Research some other more sophisticated encryption schemes.
- The Wikipedia article on Caesar cipher is a good place to start. There's a box at the bottom with links to a bunch of ciphers. The other substitution ciphers might be feasible.
- Pick one of interest and implement it.
- If you want, you may submit this extension to Moodle as a second file, but it will not be graded.
Start early, have fun, and discuss questions on Moodle.