Unit 7: Online data security · Data encryption techniques

Article

Encryption with one shared key

8 min read

You write a note to a friend, and someone else might pick it up on the way. How can you make sure only your friend can read it? You scramble it with a secret rule that only the two of you know. That is the idea behind encryption.

Locking and unlocking data

Encryption means changing data into a scrambled form, so that only a person with the right key can read it. A key is a secret value, often a number, that controls how the data is scrambled. Decryption is the reverse: using a key to turn the scrambled data back into the original.

Two more words name the two forms of the data. Plaintext is the original data that anyone could read. Ciphertext is the scrambled data. The method of scrambling is called a cipher.

Encrypt, send, decrypt

  1. Plaintext

    the readable message

    encrypt with the key
  2. Ciphertext

    looks like nonsense, safe to send

    decrypt with the key
  3. Plaintext again

    the same message as before

Your data crosses many routers on its way. Anyone could copy the ciphertext, but without the key it is useless.

Encryption does not stop people copying your data. It makes the copy worthless to them.

A cipher you can do by hand

The Caesar cipher is a very old cipher. Its key is a number: the shift. You move every letter forward in the alphabet by that many places. Z wraps around to A. This is the alphabet shifted by 3:

Plain:  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Cipher: D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

To encrypt HELLO with key 3, shift each letter by 3: H becomes K, E becomes H, L becomes O, L becomes O, and O becomes R. The ciphertext is KHOOR. To decrypt, shift every letter 3 places back.

In code, number the letters 0 to 25 (A is 0). Then the new place is the old place plus the key, with a wrap-around from MOD 26:

AP pseudocode
newPlace ← (place + key) MOD 26

Here is the whole cipher in Python, where % means MOD. The exam won't ask you to write this. Read it, run it, then change the message and the key.

Runs in your browser

Check your understanding

A message was encrypted with a Caesar cipher using a shift of 3. The ciphertext is FDE. What was the plaintext?

Why this cipher is easy to break

There are only 25 useful keys (a shift of 0 or 26 changes nothing). An attacker who has the ciphertext can simply try every key and look for the one that makes sense. Trying every possible key is called a brute-force attack. Run this and find the line that is English:

Runs in your browser

A computer does this in a blink. A good cipher needs far more keys than 25.

One key for both jobs

The Caesar cipher is symmetric encryption: the same key encrypts and decrypts. Both people need a copy. Modern symmetric ciphers are far stronger than a shift, and they are fast, so they are used to protect big amounts of data.

But there is a catch, the key-sharing problem. How does your friend get the key? If you send it over the Internet, anyone watching can copy it and read everything. Meeting in person works for two friends. It does not work for you and a website you have never met. The next article shows the fix.

More bits, many more keys

Modern keys are numbers written in bits. An n-bit key has 2ⁿ possible values, as in Unit 1. Each extra bit doubles the number of keys. Click to see it:

Pattern 00000000 is one of 256 different patterns 8 bits can make (28).

Now imagine an attacker who can test one billion keys every second:

Key sizeNumber of keysTime to try them all
8 bits256much less than a second
32 bits4,294,967,296about 4 seconds
40 bitsabout 1.1 trillionabout 18 minutes
56 bitsabout 7.2 × 10¹⁶about 2.3 years
64 bitsabout 1.8 × 10¹⁹about 585 years
128 bitsabout 3.4 × 10³⁸about 10²² years

This is the unreasonable time from Unit 4: the work grows exponentially. For the person who owns the key, one more bit costs almost nothing. For the attacker it doubles the work. With a 128-bit key, guessing every key takes far longer than the universe has existed.

Check your understanding

A system changes its key from 8 bits to 9 bits. What happens to the number of possible keys?

Worked example

A system has a 10-bit key. Its designers change to a 13-bit key. How many times more keys are there?

  1. 10 bits give 2¹⁰ = 1,024 keys.
  2. 13 bits give 2¹³ = 8,192 keys.
  3. The new key has 3 extra bits. Each one doubles the keys: 2 × 2 × 2 = 8.
  4. Check: 8,192 ÷ 1,024 = 8.

8 times more keys, not 3 times more.

Common mistakes

  • "One more bit gives one more key." It doubles the keys.
  • "A 128-bit key is twice as strong as a 64-bit key." It has 2⁶⁴ times as many keys: far more than twice.
  • "Plaintext means plain letters only." Plaintext is any readable data: text, photos, files.
  • "Encryption stops copying." No. It makes the copy unreadable without the key.
  • "A long key cannot fail." Guessing every key becomes unreasonable, but if the key is leaked or written on a sticky note, the data is open.

On the exam

Expect: "Which describes symmetric encryption?" (one key for both encrypting and decrypting), "What is the problem with it?" (the key must be shared safely), and "How many times more keys…?" questions. For the last one, count the extra bits and double once per bit.

Key words

Encryption: scrambling data so only someone with the key can read it.

Decryption: turning ciphertext back into plaintext with a key.

Plaintext: the original, readable data.

Ciphertext: the scrambled data.

Key: the secret value that controls encryption and decryption.

Symmetric encryption: encryption where one key both encrypts and decrypts.

Brute-force attack: trying every possible key or password until one works.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.