AQA 7517, section 4.5.6.10
Encryption is the process of scrambling data so that it can only be read by someone who has the key. It protects data from being understood if it is stolen or intercepted.
Terms
- Plaintext: the original, readable message.
- Ciphertext: the scrambled message produced by encryption.
- Cipher: the algorithm used to encrypt and decrypt.
- Key: a secret value that controls how the cipher works.
- Encrypt: plaintext ciphertext. Decrypt: ciphertext plaintext.
The ciphers below are at opposite extremes. The Caesar cipher is easily broken, and the Vernam cipher offers perfect security. Between them are modern ciphers with computational security.
Caesar cipher
The Caesar cipher shifts each letter along the alphabet by a fixed number of places. The shift is the key.
Encrypting (shift of 3)
Each letter moves 3 places forward, wrapping from Z back to A.
| Plain | A | B | C | D | E | ... | W | X | Y | Z |
|---|---|---|---|---|---|---|---|---|---|---|
| Cipher | D | E | F | G | H | ... | Z | A | B | C |
Encrypt HELLO with a shift of 3:
| Plain | H | E | L | L | O |
|---|---|---|---|---|---|
| Cipher | K | H | O | O | R |
The ciphertext is KHOOR.
Decrypting
Shift back by the same amount. KHOOR shifted back 3 gives HELLO.
def caesar(text, shift):
result = ""
for ch in text:
if ch.isalpha():
base = ord("A")
result = result + chr((ord(ch) - base + shift) % 26 + base)
else:
result = result + ch
return result
secret = caesar("HELLO", 3)
print(secret) # KHOOR
print(caesar(secret, -3)) # HELLO
Why is it easily cracked?
- Only 25 possible keys. An attacker can simply try them all (a brute force attack), and it takes seconds to look for the one that gives readable text.
- Frequency analysis. Each letter is always replaced by the same letter, so the pattern of letter frequencies remains. In English,
Eis the commonest letter, so the commonest letter in the ciphertext is probablyE, which reveals the shift immediately. - The algorithm is simple, so there's no real secrecy once you know the idea.
Vernam cipher (one-time pad)
The Vernam cipher combines the plaintext with a random key that is as long as the message, using XOR on the bits.
How it works
Each bit of the plaintext is XORed with the matching bit of the key. XOR gives 1 if the bits are different and 0 if they are the same.
| Plain bit | Key bit | Cipher bit |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Encrypt the letter H (ASCII 72 = 01001000) with key 10110101:
Decrypt by XORing the ciphertext with the same key again:
XORing twice with the same key restores the original, so the same operation encrypts and decrypts.
plain = 0b01001000
key = 0b10110101
cipher = plain ^ key
print(format(cipher, "08b")) # 11111101
print(format(cipher ^ key, "08b")) # 01001000
The conditions
The Vernam cipher is secure only if:
- the key is truly random;
- the key is at least as long as the message;
- the key is used once only, which is why it is called a one-time pad;
- the key is kept secret.
Why does it offer perfect security?
Because the key is chosen uniformly at random, the ciphertext is also completely random. Every possible plaintext of that length could have produced the same ciphertext with some key, and they are all equally likely. So the ciphertext gives an attacker no information at all about the message. Even with unlimited time and computing power it can't be broken, because there's nothing to find. It's the only cipher mathematically proved to be completely secure.
If a key is reused, the security is lost, because comparing two ciphertexts reveals information.
Disadvantages
- The key must be as long as the message, so large amounts of data need huge keys.
- It's difficult to generate truly random keys.
- The key must be shared securely with the receiver in advance, which is the same problem as sending the message securely, and each key can be used only once.
Computational security
Almost all real-world ciphers (such as AES and RSA) rely on computational security. In theory, every cryptographic algorithm except the Vernam cipher can be broken, given enough ciphertext and time. But they are designed so that breaking them would need an impractical amount of computing power or time, such as millions of years. Their strength depends on the problem being intractable (4.4.4.5).
Comparing the ciphers
| Caesar | Vernam | Modern (e.g. AES) | |
|---|---|---|---|
| Key | A shift (25 possible values) | Random, as long as the message | Short random key (such as 256 bits) |
| Security | Very weak: easily broken | Perfect: can't be broken | Computational: very hard to break in practice |
| Practical | Simple but useless | Impractical for most uses (key length and sharing) | Practical and widely used |
| Weakness | Brute force, frequency analysis | Key handling, one use only | Could be broken with enough time |
Worked example
Task: encrypt ABC with a Caesar cipher with a shift of 2. Then explain why a message encrypted this way is not safe.
Ashifts toC,BtoD, andCtoE, givingCDE.- It is not safe, because there are only 25 possible shifts, so an attacker can try every one and see which gives a sensible message. Frequency analysis would also find the shift immediately for a longer message.
Exam tip: for the Caesar cipher, show your working letter by letter and use modular wrap-around (Z to A). For the Vernam cipher, mention all the conditions: a random key, as long as the message, used once only. For "perfect security", say that the ciphertext gives no information about the plaintext.
Check yourself
Encrypt CAT with a Caesar cipher using a shift of 1
DBU.
Why is the Caesar cipher easily cracked?
There are only a few possible keys (25), so all can be tried quickly, and letter frequencies are unchanged, so frequency analysis reveals the shift.
Why is the Vernam cipher considered to offer perfect security?
The key is random, as long as the message and used only once, so the ciphertext is completely random and gives no information about the plaintext. It has been mathematically proved secure.
What does computational security mean?
A cipher that could be broken in theory, but that would need an impractical amount of time or computing power to break in practice.
Give one practical problem with the Vernam cipher
The key must be as long as the message, truly random, shared securely in advance, and never reused.