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.
Caesar cipher with key 3. The plain alphabet a to z sits above the cipher alphabet, each letter shifted forward 3: a becomes d, b becomes e, and so on to w, which becomes z. The last three are marked: x, y, z wrap round to a, b, c. Encrypting the word brutus: b becomes e, r becomes u, u becomes x, t becomes w, u becomes x, s becomes v, giving euxwxv. Decrypting shifts back 3 with the same key.

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. Each letter sits at a position counted from 0, as in an array, and alphabet[i] is the letter at position i. 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 first letter that becomes undefined. The letters of alphabet sit at positions 0 to 25, and reading past the last one gives undefined, as it does past the end of an array.
let alphabet = "abcdefghijklmnopqrstuvwxyz";
let key = 10;
for (let i = 0; i < 26; i++) {
  // Past z, the remainder after dividing by 26 starts again at a
  let spot = (i + key) % 26;
  console.log(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.
Possible keys for a key of n bits, as bars for key lengths 1 to 10 bits: 2, 4, 8, 16, 32, 64, 128, 256, 512 and 1,024. The axis is key length in bits. Each extra bit doubles the count. At 128 bits: 2 to the power 128, about 3.4 x 10^38 keys.
The program below counts the possible keys for key lengths from 8 to 48 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 49 to the end of the array, and compare its line with the line for 48.
• Add 128 to the end of the array, and compare its count of keys with the figure for a 128-bit key given above. In JavaScript, 3.4e+38 means 3.4 times 10 to the power 38.
• Keep 128 in the array, and make guessesPerSecond a thousand times larger.
let guessesPerSecond = 1000000;
let secondsPerYear = 60 * 60 * 24 * 365;
for (const bits of [8, 16, 24, 32, 40, 48]) {
  let keys = 2 ** bits;
  let years = Math.floor(keys / guessesPerSecond / secondsPerYear);
  console.log(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.
A round paper cipher disk with two rings. The inner ring holds letters and symbols; the outer ring holds codes made of the digits 1 and 8. The centre reads A.J.M., No. 000.
A Union Army cipher disk from the American Civil War. Turning one ring against the other changes which code stands for each letter. Photo: Ryan Somma, CC BY 2.0, via Wikimedia Commons.
Letter counts in one English passage. Before, the plaintext: e 111, t 67, r 50, o 50, n 46, h 44, a 39, s 37. After a substitution cipher: x 111, k 67, z 50, f 50, g 46, v 44, u 39, w 37. The cipher turned every e into x. The bars stay the same height; only their labels change, so the tallest bar still points to e.
Counted in this lesson's account of Mary, Queen of Scots and the first two sentences under Symmetric Encryption.

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.
A painted portrait of Mary, Queen of Scots, in a black dress with a white lace ruff and cap, wearing a crucifix and a gold cross on chains, one hand resting on a red-covered table.
Mary, Queen of Scots, in a portrait after Nicholas Hilliard. Public domain, via Wikimedia Commons.

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.
TermWhat it means
plaintextthe readable message, before the cipher is applied
ciphertextthe gibberish the cipher produces
cipherthe algorithm that converts one into the other
encryptionthe process of making text secret: plaintext to ciphertext
decryptionthe reverse process: ciphertext back to plaintext
keythe secret the cipher needs. For a Caesar cipher, the shift number
substitution ciphera cipher in which every letter is replaced by a different letter
symmetric encryptionthe 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.
A wooden case in a museum display holding an Enigma machine's rotors, with more of the machine in the background under orange light.
An Enigma machine and rotors, a German cipher machine of the Second World War. Its operators set it from a printed sheet of daily settings. Photo: Nachosan, CC BY-SA 3.0, via Wikimedia Commons.