Ciphers and Symmetric Encryption
Cryptography
Crypto plus graphy equals secret plus writing.To make information secret, use a cipher: an algorithm that converts plaintext to ciphertext, which is gibberish unless you have a key that undoes the cipher.
Encryption is the process of making text secret. Decryption is the reverse process, unscrambling gibberish text back to plaintext.
The Caesar Cipher
Julius Caesar used what is now called a Caesar cipher: he shifts all of the letters forward by three.A becomes D, and
brutus becomes euxwxv.To decipher, you need both the algorithm and the shift number, which acts as the key. In this case, the key is 3.
Running off the end
Shifting forward by three needs one more rule, because three of the letters have nothing three places ahead of them. The alphabet wraps around: x becomes a, y becomes b, z becomes c.It has to work that way. A cipher that stopped at z, leaving those three letters alone, could not be undone by the receiver, who would have no way of telling a letter that was shifted from a letter that was left behind. Wrapping is what keeps the cipher reversible.
Trying every key
The Caesar cipher uses 26 keys and is easy to break.Twenty-six is how many shifts there are, since shifting by 26 brings every letter all the way around and back to itself. One of those shifts, a shift of zero, leaves the message exactly as it was, so 25 of them actually encipher anything.
Either way the number is tiny. Someone who intercepts a note and knows only that a Caesar cipher was used can write the note out under every shift and read down the list until one of the lines is English. That is a few minutes of work with a pencil, and it is the whole reason a strong cipher needs an enormous number of possible keys.
The program below prints the cipher alphabet for one key: each letter, and the letter it becomes. It adds the key to a letter's position and divides by 26. The remainder is the position of the new letter, so the end of the alphabet wraps around to a. Run it, then change it and run it again:
• Set
key to 3, and compare the table with the picture of Caesar's cipher above.• Set
key to 26, then to 0, and compare the two tables.• Press Reset, then change
(i + key) % 26 to i + key, which leaves out the wrap, and find the letter where the program stops with an error. alphabet holds its letters at positions 0 to 25, counted from 0 as in a list.alphabet = "abcdefghijklmnopqrstuvwxyz"
key = 10
for i in range(26):
# Past z, the remainder after dividing by 26 starts again at a
spot = (i + key) % 26
print(alphabet[i], "becomes", alphabet[spot])
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Modern ciphers write the key as a string of bits, and the number of possible keys depends on how long that string is. A key of n bits has 2 to the power n possible values, so each extra bit doubles the count. The number of keys grows exponentially with the length of the key. A 128-bit key, a size in everyday use, has about 3.4 times 10 to the power 38 possible values. Trying every one of them is an algorithm with exponential efficiency, and it does not finish in a reasonable amount of time.
The program below counts the possible keys for key lengths from 8 to 64 bits. It also works out how many full years a computer trying a million keys a second would need to try them all. Run it, then change it and run it again:
• Add 65 to the end of the list, and compare its line with the line for 64.
• Add 128 to the end of the list, and compare its count of keys with the figure for a 128-bit key given above.
• Make
guesses_per_second a thousand times larger.guesses_per_second = 1000000
seconds_per_year = 60 * 60 * 24 * 365
for bits in [8, 16, 24, 32, 40, 48, 56, 64]:
keys = 2 ** bits
years = keys // guesses_per_second // seconds_per_year
print(bits, "bits:", keys, "keys. Full years to try them all:", years)
Output
Press Run to see what this program prints.
Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.
Substitution Ciphers
The Caesar cipher is an example of a larger class of ciphers called substitution ciphers: every letter is replaced by a different letter.A Caesar cipher is the tidy special case, where the replacement is always the same distance along the alphabet, so its key is one number. A general substitution cipher does not have to be tidy at all. It can pair the letters up any way it likes, and its key is the whole table of pairings, which means there are enormously more keys to choose from.

Mary, Queen of Scots
It was the breaking of a substitution cipher that led to the execution of Mary, Queen of Scots, in 1587, for plotting to kill Queen Elizabeth.Mary was a prisoner, and her letters to the plotters were smuggled out in a waterproof pouch hidden in the bung of a beer barrel. They were written in a cipher of invented symbols rather than letters, which changes nothing: the symbols were counted, the common ones were matched against the common letters of English, and the messages were read. Her replies were read too, and by people who then forged an addition to one of them to draw out the names of her fellow plotters.
What convicted her was her own enciphered words.

Symmetric Encryption
Every technique so far relies on a key known by both the sender and the receiver. The sender encrypts the message with a key and the receiver decrypts it with the same key.Symmetric encryption is an encryption technique that uses the same key for both encryption and decryption.
For example, in the Caesar cipher, if encryption is shifting right by 3 letters, then decryption is shifting left by 3 letters. Both use the key 3. The Caesar cipher is therefore a symmetric encryption technique.
The two directions being opposite is not what makes a scheme asymmetric. Encryption and decryption are always opposite operations. What would make it asymmetric is the two directions using different keys, and here they do not.
| Term | What it means |
|---|---|
| plaintext | the readable message, before the cipher is applied |
| ciphertext | the gibberish the cipher produces |
| cipher | the algorithm that converts one into the other |
| encryption | the process of making text secret: plaintext to ciphertext |
| decryption | the reverse process: ciphertext back to plaintext |
| key | the secret the cipher needs. For a Caesar cipher, the shift number |
| substitution cipher | a cipher in which every letter is replaced by a different letter |
| symmetric encryption | the same key is used to encrypt and to decrypt |
Getting the key to the other side
In the old days, keys were shared physically or by voice. Wartime codebooks listing the setting for each day of the month were printed and issued in advance, so both ends of a conversation woke up already agreeing.Today a server needs to send a key over the internet: making a purchase on Amazon, logging in to Gmail. Keys can be intercepted.
