Caesar's Cipher

The questions below are due on Sunday July 14, 2024; 10:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Description

Larry has recently been promoted to be the head of cybersecurity at TakTik, a social media platform. He suggests that user data would be more secure if it was encrypted, and hires you as an intern to find a way to encrpyt data using Caesar's Cipher.1 Caesar's Cipher is a kind of simple plain-text cipher that has been around since (at least) the time of Julius Caesar. It takes in a plain-text string and translates it into a new string by shifting the alphabet being used. This produces an encrypted message that cannot be easily read by someone who doesn't know the "shift" value.

Consider laying out the alphabet along a line and assigning a number to each character ("a" is 0, "b" is 1, "c" is 2, and so on). In a Caesar Cipher:

  • the numerical value of each character is determined,
  • a constant value (the "shift") is added to the number associated with each of the characters in the original sequence
    • if the shifted value goes beyond 25, it should wrap back around to 0
    • if the shifted value goes below 0, it should wrap back around to 25
  • the resulting numbers are converted back into characters.

Define a function caesar_cipher, which takes two arguments:

  • a string of characters to be encrypted, and
  • an integer representing the "shift" value.

caesar_cipher should return a string representing the encrypted form of the input string.

Slight variations on this technique exist, but in our version we will:

  • shift letters as described above, after converting upper-case letters in the input string to lower-case, and
  • leave all non-letter characters unchanged in the resulting string2

Below are several examples:

assert caesar_cipher('abcd',1) == 'bcde'
assert caesar_cipher('abcd',1) == 'bcde'
assert caesar_cipher('ABCD',1) == 'bcde' # (convert to lower-case first)
assert caesar_cipher('abcd',-1) == 'zabc'
assert caesar_cipher('abcd',-3) == 'xyza'

assert caesar_cipher('this is pretty neat!',2) == 'vjku ku rtgvva pgcv!'

assert caesar_cipher("don't you feel like a roman emperor?",13)  == "qba'g lbh srry yvxr n ebzna rzcrebe?"
assert caesar_cipher("don't you feel like a roman emperor?",2) == "fqp'v aqw hggn nkmg c tqocp gorgtqt?"
assert caesar_cipher("don't you feel like a roman emperor?",-50) == "fqp'v aqw hggn nkmg c tqocp gorgtqt?"

Helpful Resources

  • This exercise is focused on reviewing course content - specifically strings, looping, and conditionals. If you want to review how we iterate over strings and make new strings, you can check out this unit 2 assignment
  • The string module contains a string consisting of all the lower-case English letters, available as string.ascii_lowercase. To use this, you must first import the string module by writing import string. Then you can try printing out string.ascii_lowercase to see what it looks like.
  • You can create a string consisting of all the characters in a string x but converted to lowercase by calling x.lower().
  • Recall that you can use the modulo operator % in order to get the positive remainder. For example 29 % 26 will return 3.

Testing

Make a plan before you write any code, and sketch out your plan on paper. Make sure to write and test your code by hand for some simple examples (simpler than those shown above!) before using Python. Draw environment diagrams for a few different test cases. Then, only once you're convinced it will work, run your code with Python on your own machine as a test.

Submission

When you are ready (once you have simulated by hand and tested on your own machine and are convinced that your program will do the right thing), upload your file below for testing:

  No file selected

Next Exercise: Ratings

Back to exercises


 
Footnotes

1Caesar's Cipher is not a safe form of data encryption, but most modern encryption methods go beyond the scope of the course. (click to return to text)

2This is maybe not the most secure thing in the world, but that's okay for now... (click to return to text)