Edexcel International Advanced Level Computer Science, point 3.2.4
Definition
The Vernam cipher is a stream cipher that combines each bit (or character) of the plaintext with the matching bit of a secret key, using the XOR operation. If the key is truly random, as long as the message and used only once, it's called a one-time pad, and it is theoretically unbreakable.
Function and characteristics
- Symmetric: the same key encrypts and decrypts.
- The key (the pad) must be:
- Truly random.
- At least as long as the message.
- Used only once (never reused).
- Kept secret, and shared securely in advance.
- Encrypting and decrypting are the same operation (XOR), because XOR is its own inverse.
Operation
The XOR operation
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
XOR gives 1 when the two bits are different. The key property:
so XORing with the same key twice gets back the original.
Encrypting
- Write the plaintext as binary using its character codes (7-bit ASCII in the book; the examples here use 8 bits, which works the same way, 2.3.1).
- Write the key in binary, the same length.
- XOR each plaintext bit with the matching key bit, giving the ciphertext.
Decrypting
XOR the ciphertext with the same key: the plaintext reappears.
Example: encrypt and decrypt "H"
The plaintext H is 01001000 in ASCII. XOR it with the key to get the ciphertext, then XOR the ciphertext with the same key to get H back:
Check one bit pattern: 01001000 XOR 10110010: 0 XOR 1 = 1, 1 XOR 0 = 1, 0 XOR 1 = 1, 0 XOR 1 = 1, 1 XOR 0 = 1, 0 XOR 0 = 0, 0 XOR 1 = 1, 0 XOR 0 = 0. That gives 11111010. ✓
A letter version (for reference)
If letters are numbered A = 0 to Z = 25, a one-time pad of numbers can be added modulo 26. For example, HELLO with key 3, 7, 1, 22, 9:
| Plain | H (7) | E (4) | L (11) | L (11) | O (14) |
|---|---|---|---|---|---|
| Key | 3 | 7 | 1 | 22 | 9 |
| Sum mod 26 | 10 | 11 | 12 | 7 | 23 |
| Cipher | K | L | M | H | X |
To decrypt, subtract the key mod 26. Both versions work the same way: combine with a random key that is used once.
Why it is unbreakable (in theory)
If the key is truly random and used once, every possible plaintext of that length is equally likely for a given ciphertext. For example, 11111010 could be the result of any plaintext, with a suitable key. So the ciphertext gives an attacker no information, however much computing power they have. This is perfect secrecy.
Benefits
- Perfect secrecy when used correctly: can't be broken, even with unlimited computing power.
- Simple and fast: XOR is a single, very fast operation.
- No patterns in the ciphertext, so no frequency analysis (unlike 3.2.3).
Drawbacks
- The key must be as long as the message. Encrypting a 1 GB file needs a 1 GB key.
- Key distribution: a huge random key must be shared securely beforehand, and this is as hard as sending the message itself (the key exchange problem, 3.2.2).
- Keys can't be reused. If two messages use the same key, XORing the two ciphertexts cancels the key and gives plaintext XOR plaintext, which leaks information and can be broken: .
- Truly random numbers are hard to generate (computers produce pseudo-random numbers, which aren't truly random).
- Keys must be stored and destroyed safely.
- No built-in authentication or integrity: an attacker who knows part of the plaintext can alter the ciphertext in a predictable way.
Because of these problems, the one-time pad is used only for very high security needs. Modern systems use stream ciphers that expand a short key into a long pseudo-random stream and XOR it with the data, which isn't perfectly secure but is practical.
Worked example
Task: encrypt the message CAT in 7-bit ASCII with the key 1011010 1001100 0110101, then check by decrypting.
Encrypt:
| C | A | T | |
|---|---|---|---|
| Plaintext (7-bit ASCII) | 1000011 | 1000001 | 1010100 |
| Key | 1011010 | 1001100 | 0110101 |
| Ciphertext | 0011001 | 0001101 | 1100001 |
Decrypt (XOR the ciphertext with the key again):
| Ciphertext | 0011001 | 0001101 | 1100001 |
| Key | 1011010 | 1001100 | 0110101 |
| Plaintext | 1000011 | 1000001 | 1010100 |
| Character | C | A | T |
The ciphertext is kept as binary, not turned back into letters: after the XOR the bits often aren't a printable character (0011001 is code 25, a control code), so it is stored and sent as bits.
Why can't the same key be used for a second message? If another message of the same length used this key, XORing the two ciphertexts removes the key, revealing the XOR of the two plaintexts, from which an attacker can work out both messages by guessing common words.
Exam tip: write the plaintext, key and result in three aligned rows, and XOR column by column ("1 only when the bits differ"). To decrypt, XOR again with the same key. For the properties of a one-time pad, remember the four conditions: random, as long as the message, used once, secret. A key weakness is key distribution.
Check yourself
What is 01100110 XOR 10101010?
Why is the same operation used for both encryption and decryption in the Vernam cipher?
XORing with the same key twice returns the original value, because XOR is its own inverse.
State three conditions the key must meet for a one-time pad to be secure
Any three of: truly random; as long as the message; used only once; kept secret.
Why must a one-time pad key never be reused?
XORing two ciphertexts made with the same key cancels the key, giving the XOR of the two plaintexts, which leaks information and can be used to recover the messages.
Give two drawbacks of the Vernam cipher in practice
Any two of: the key is as long as the message; the key must be shared securely in advance; keys can't be reused; truly random keys are hard to produce.