…
…
Write functions that encrypt and decrypt by shifting letters in the English alphabet, then break the cipher without the key using brute force and letter frequency.
The method Julius Caesar is said to have used in his letters is very simple: every letter is shifted forward by a fixed number of places in the alphabet. With key 3, a becomes d and b becomes e. In this project you will write an encryptor that works with the English alphabet, then break the cipher in two different ways without knowing the key. In the last step you will try a method (Vigenère) that resists both attacks.
Lessons used: remainder with % (3), conditions and in (6–7), loops and the largest-value pattern (10–13), functions (14–16), indexing, string methods and f-strings (20–22). No list work is needed; the last step is an optional Advanced step.
Texts are written in lowercase; characters that are not in the alphabet (spaces, punctuation) stay as they are.
What you'll do
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The English alphabet has 26 letters. ALPHABET.index(letter) gives a letter's position (lesson 21), and an index turns a position back into a letter: ALPHABET[3] (lesson 20). When you shift, you can run past the end of the alphabet. % 26 (the remainder from lesson 3) wraps the number back to the start: shifting z forward by 1 gives a. In Python, % returns a result from 0 to 25 even for negative numbers, so shifting backward works with the same formula.
Characters that are not in the alphabet (spaces, punctuation) come back unchanged. index raises an error for a letter that is not there, so check with in first.
This step's code cell is for members.Sign up free
encrypt(text, key) shifts every character of the text with shift_letter and joins the results into a new string. Strings do not change (lesson 20), so you start with an empty result string and add each shifted character to the end with += inside the loop. It is the same pattern as in lesson 13: the line of text starts empty and grows in the loop.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
This step's code cell is for members.Sign up free
Decrypting means shifting back by the same amount. You do not need a new loop: decrypt(text, key) can call encrypt with a negative key. Reusing a function you already have is shorter and less error-prone.
When you encrypt and then decrypt, you should get the original text back.
This step's code cell is for members.Sign up free
Someone who does not know the key only has to try 25 possibilities. Decrypt with every possible key and give the result a score for how "English" it looks: how many of the known words appear in the decrypted text? word in text asks whether one string appears inside another (lesson 6).
Then use the largest value and which one pattern from lesson 12: keep the highest score so far and the key that produced it in two variables, and update both together whenever a key gives a higher score.
This step's code cell is for members.Sign up free
You can also break the cipher without a word list. In English, the most common letter is e. In a long encrypted text, the most common letter is probably a shifted e. The difference between their positions in the alphabet gives the key.
For each letter of the alphabet, secret_message.count(letter) (lesson 21) tells you how many times it appears. Use the largest value and which one pattern again to find the most common letter and its count, then calculate the key and decrypt.
This step's code cell is for members.Sign up free
*Optional advanced step.* In the Caesar cipher every letter moves by the same amount, so letter frequency gives it away. In the Vigenère cipher the shift comes from a keyword and changes from letter to letter: if the keyword is key, the first letter moves by the position of k, the second by the position of e, the third by the position of y, and then it starts over.
Only letters in the alphabet advance through the keyword; spaces are skipped. With direction=-1 the same function decrypts. You get the next keyword letter by indexing; j % len(keyword) wraps back to the start when you reach the end of the keyword.
This step's code cell is for members.Sign up free