Skip to content

Rewrite the Simple Cipher description. #2565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 25 additions & 52 deletions exercises/simple-cipher/description.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,39 @@
# Description

Implement a simple shift cipher like Caesar and a more secure substitution cipher.
Create an implementation of the [Vigenère cipher][wiki].
The Vigenère cipher is a simple substitution cipher.

## Step 1
## Cipher terminology

"If he had anything confidential to say, he wrote it in cipher, that is, by so changing the order of the letters of the alphabet, that not a word could be made out.
If anyone wishes to decipher these, and get at their meaning, he must substitute the fourth letter of the alphabet, namely D, for A, and so with the others."
—Suetonius, Life of Julius Caesar
A cipher is an algorithm used to encrypt, or encode, a string.
The unencrypted string is called the _plaintext_ and the encrypted string is called the _ciphertext_.
Converting plaintext to ciphertext is called _encoding_ while the reverse is called _decoding_.

Ciphers are very straight-forward algorithms that allow us to render text less readable while still allowing easy deciphering.
They are vulnerable to many forms of cryptanalysis, but Caesar was lucky that his enemies were not cryptanalysts.
In a _substitution cipher_, each plaintext letter is replaced with a ciphertext letter which is computed with the help of a _key_.
(Note, it is possible for replacement letter to be the same as the original letter.)

The Caesar cipher was used for some messages from Julius Caesar that were sent afield.
Now Caesar knew that the cipher wasn't very good, but he had one ally in that respect: almost nobody could read well.
So even being a couple letters off was sufficient so that people couldn't recognize the few words that they did know.
## Encoding details

Your task is to create a simple shift cipher like the Caesar cipher.
This image is a great example of the Caesar cipher:
In this cipher, the key is a series of lowercase letters, such as `"abcd"`.
Each letter of the plaintext is _shifted_ or _rotated_ by a distance based on a corresponding letter in the key.
An `"a"` in the key means a shift of 0 (that is, no shift).
A `"b"` in the key means a shift of 1.
A `"c"` in the key means a shift of 2, and so on.

![Caesar cipher][img-caesar-cipher]
The first letter of the plaintext uses the first letter of the key, the second letter of the plaintext uses the second letter of the key and so on.
If you run out of letters in the key before you run out of letters in the plaintext, start over from the start of the key again.

For example:
If the key only contains one letter, such as `"dddddd"`, then all letters of the plaintext are shifted by the same amount (three in this example), which would make this the same as a rotational cipher or shift cipher (sometimes called a Caesar cipher).
For example, the plaintext `"iamapandabear"` would become `"ldpdsdqgdehdu"`.

Giving "iamapandabear" as input to the encode function returns the cipher "ldpdsdqgdehdu".
Obscure enough to keep our message secret in transit.
If the key only contains the letter `"a"` (one or more times), the shift distance is zero and the ciphertext is the same as the plaintext.

When "ldpdsdqgdehdu" is put into the decode function it would return the original "iamapandabear" letting your friend read your original message.
Usually the key is more complicated than that, though!
If the key is `"abcd"` then letters of the plaintext would be shifted by a distance of 0, 1, 2 then 3.
Using that key, `"hello"` would become `"hfnoo"`.
Comment on lines +32 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to put this example in its own subsection and show the process in more detail? For instance: "e" is shifted by 1, so it becomes the next letter, "f". "l" is shifted by 2, so it becomes the letter two positions after "l", and so on. For "o", we've run out of key letters, so we start over with "a", and the shift is 0. Basically, just more explanation about how the instructions are applied to the example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷
Either way makes sense. I think this is detailed enough to get students started. Do you think it needs to be more detailed for students to understand the algorithm and solve this exercise?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW no one on the forum asked for more detail here, implicitly suggesting they feel this is sufficiently detailed as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is detailed enough to get students started. Do you think it needs to be more detailed for students to understand the algorithm and solve this exercise?

I agree, this is enough to get started. It's enough for me since I already understand how the algorithm works, and it's probably enough for someone who figures it out from the input and output. So if the goal is for students to work out the algorithm based on the description and tests, then the given instructions are probably sufficient. It really depends on what you have in mind. Personally, I would prefer to see the algorithm in action to make things clearer. Even a simple rephrasing would help:

If the plaintext is "hello" and the key is "abcd", the letters `h, e, l, l, o` are shifted by `0, 1, 2, 3, 0` positions respectively, resulting in "hfnoo".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So ... to clarify what you're saying, you specifically would like the example which calls out/shows the part about how the key is repeated?


## Step 2
## Random keys

Shift ciphers quickly cease to be useful when the opposition commander figures them out.
So instead, let's try using a substitution cipher.
Try amending the code to allow us to specify a key and use that for the shift distance.
If no key is provided, generate a key which consists of at least 100 random lowercase letters from the Latin alphabet.

Here's an example:

Given the key "aaaaaaaaaaaaaaaaaa", encoding the string "iamapandabear"
would return the original "iamapandabear".

Given the key "ddddddddddddddddd", encoding our string "iamapandabear"
would return the obscured "ldpdsdqgdehdu"

In the example above, we've set a = 0 for the key value.
So when the plaintext is added to the key, we end up with the same message coming out.
So "aaaa" is not an ideal key.
But if we set the key to "dddd", we would get the same thing as the Caesar cipher.

## Step 3

The weakest link in any cipher is the human being.
Let's make your substitution cipher a little more fault tolerant by providing a source of randomness and ensuring that the key contains only lowercase letters.

If someone doesn't submit a key at all, generate a truly random key of at least 100 lowercase characters in length.

## Extensions

Shift ciphers work by making the text slightly odd, but are vulnerable to frequency analysis.
Substitution ciphers help that, but are still very vulnerable when the key is short or if spaces are preserved.
Later on you'll see one solution to this problem in the exercise "crypto-square".

If you want to go farther in this field, the questions begin to be about how we can exchange keys in a secure way.
Take a look at [Diffie-Hellman on Wikipedia][dh] for one of the first implementations of this scheme.

[img-caesar-cipher]: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Caesar_cipher_left_shift_of_3.svg/320px-Caesar_cipher_left_shift_of_3.svg.png
[dh]: https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange
[wiki]: https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher
2 changes: 1 addition & 1 deletion exercises/simple-cipher/metadata.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title = "Simple Cipher"
blurb = "Implement a simple shift cipher like Caesar and a more secure substitution cipher."
blurb = "Implement the Vigenère cipher, a simple substitution cipher."
source = "Substitution Cipher at Wikipedia"
source_url = "https://en.wikipedia.org/wiki/Substitution_cipher"