Caesar - CS50x
Caesar - CS50x
OpenCourseWare
Caesar
Implement a program that encrypts messages using Caesar’s cipher, per the below.
$ ./caesar 13
plaintext: HELLO
ciphertext: URYYB
Background
Supposedly, Caesar (yes, that Caesar) used to “encrypt” (i.e., conceal in a reversible way) con dential messages by shifting each letter therein by
some number of places. For instance, he might write A as B, B as C, C as D, …, and, wrapping around alphabetically, Z as A. And so, to say
HELLO to someone, Caesar might write IFMMP. Upon receiving such messages from Caesar, recipients would have to “decrypt” them by shifting
letters in the opposite direction by the same number of places.
The secrecy of this “cryptosystem” relied on only Caesar and the recipients knowing a secret, the number of places by which Caesar had shifted
his letters (e.g., 1). Not particularly secure by modern standards, but, hey, if you’re perhaps the rst in the world to do it, pretty secure!
Unencrypted text is generally called plaintext. Encrypted text is generally called ciphertext. And the secret used is called a key.
To be clear, then, here’s how encrypting HELLO with a key of 1 yields IFMMP :
plaintext H E L L O
+ key 1 1 1 1 1
= ciphertext I F M M P
More formally, Caesar’s algorithm (i.e., cipher) encrypts messages by “rotating” each letter by k positions. More formally, if p is some plaintext
(i.e., an unencrypted message), pi is the ith character in p, and k is a secret key (i.e., a non-negative integer), then each letter, ci, in the
ciphertext, c, is computed as
ci = (pi + k) % 26
wherein % 26 here means “remainder when dividing by 26.” This formula perhaps makes the cipher seem more complicated than it is, but it’s
really just a concise way of expressing the algorithm precisely. Indeed, for the sake of discussion, think of A (or a) as 0, B (or b) as 1, …, H (or h)
as 7, I (or i) as 8, …, and Z (or z) as 25. Suppose that Caesar just wants to say Hi to someone con dentially using, this time, a key, k, of 3. And so
his plaintext, p, is Hi, in which case his plaintext’s rst character, p0, is H (aka 7), and his plaintext’s second character, p1, is i (aka 8). His
ciphertext’s rst character, c0, is thus K, and his ciphertext’s second character, c1, is thus L. Can you see why?
Let’s write a program called caesar that enables you to encrypt messages using Caesar’s cipher. At the time the user executes the program,
they should decide, by providing a command-line argument, on what the key should be in the secret message they’ll provide at runtime. We
shouldn’t necessarily assume that the user’s key is going to be a number; though you may assume that, if it is a number, it will be a positive
integer.
Here are a few examples of how the program might work. For example, if the user inputs a key of 1 and a plaintext of HELLO :
$ ./caesar 1
plaintext: HELLO
ciphertext: IFMMP
1/5
Here’s how the program might work if the user provides a key of 13 and a plaintext of hello, world :
$ ./caesar 13
plaintext: hello, world
ciphertext: uryyb, jbeyq
Notice that neither the comma nor the space were “shifted” by the cipher. Only rotate alphabetical characters!
How about one more? Here’s how the program might work if the user provides a key of 13 again, with a more complex plaintext:
$ ./caesar 13
plaintext: be sure to drink your Ovaltine
ciphertext: or fher gb qevax lbhe Binygvar
Why?
Notice that the case of the original message has been preserved. Lowercase letters remain lowercase, and uppercase letters remain uppercase.
$ ./caesar HELLO
Usage: ./caesar key
$ ./caesar
Usage: ./caesar key
Or even…
$ ./caesar 1 2 3
Usage: ./caesar key
Speci cation
Design and implement a program, caesar , that encrypts messages using Caesar’s cipher.
Pseudocode 2/5
Pseudocode
First, write some pseudocode that implements this program, even if not (yet!) sure how to write it in code. There’s no one right way to write
pseudocode, but short English sentences suf ce. Recall how we wrote pseudocode for nding Mike Smith
(https://cdn.cs50.net/2018/fall/lectures/0/lecture0.pdf). Odds are your pseudocode will use (or imply using!) one or more functions, conditions,
Boolean expressions, loops, and/or variables.
Spoiler
Whatever your pseudocode, let’s rst write only the C code that checks whether the program was run with a single command-line argument
before adding additional functionality.
Speci cally, modify caesar.c in such a way that: if the user provides exactly one command-line argument, it prints Success ; if the user
provides no command-line arguments, or two or more, it prints Usage: ./caesar key . Remember, since this key is coming from the command
line at runtime, and not via get_string , we don’t have an opportunity to re-prompt the user. The behavior of the resulting program should be
like the below.
$ ./caesar 20
Success
or
$ ./caesar
Usage: ./caesar key
or
$ ./caesar 1 2 3
Usage: ./caesar key
Hints
Now that your program is (hopefully!) accepting input as prescribed, it’s time for another step.
Recall that in our program, we must defend against users who technically provide a single command-line argument (the key), but provide
something that isn’t actually an integer, for example:
$ ./caesar xyz
Before we start to analyze the key for validity, though, let’s make sure we can actually read it. Further modify caesar.c such that it not only
checks that the user has provided just one command-line argument, but after verifying that, prints out that single command-line argument. So,
for example, the behavior might look like this:
$ ./caesar 20
Success
20
Hints
3/5
$ ./caesar 20
Success
20
or
$ ./caesar 20x
Usage: ./caesar key
Hints
Extend the functionality of caesar.c such that, after validating the key, we prompt the user for a string and then shift all of its characters by 1,
printing out the result. We can also at this point probably remove the line of code we wrote earlier that prints Success . All told, this might
result in this behavior:
$ ./caesar 1
plaintext: hello
ciphertext: ifmmp
Hints
Your Turn
Now it’s time to tie everything together! Instead of shifting the characters by 1, modify caesar.c to instead shift them by the actual key value.
And be sure to preserve case! Uppercase letters should stay uppercase, lowercase letters should stay lowercase, and characters that aren’t
alphabetical should remain unchanged.
Hints
Walkthrough
H t T tY C d 4/5
How to Test Your Code
Execute the below to evaluate the correctness of your code using check50 . But be sure to compile and test it yourself as well!
check50 cs50/problems/2020/x/caesar
Execute the below to evaluate the style of your code using style50 .
style50 caesar.c
How to Submit
Execute the below, logging in with your GitHub username and password when prompted. For security, you’ll see asterisks ( * ) instead of the
actual characters in your password.
submit50 cs50/problems/2020/x/caesar
5/5