Encryption

Plaintext, keys and ciphertext, the Caesar cipher, symmetric and asymmetric, and a secret over the radio.

F11.6Cyber securityGCSE20 min

Covers OCR J277 1.4.2 Identifying and preventing vulnerabilities · AQA 8525 3.6.3 Methods to detect and prevent cyber security threats · Edexcel 1CP2 5.3.2 Protecting digital systems

Do this lesson in the simulator

Data travelling across a network can be intercepted (lesson F11.4). Encryption makes that harmless: it scrambles the data so that only someone with the key can read it. Intercepted, it is gibberish. This lesson builds a cipher, sends a secret message to another robot, and explains how the whole web is kept private.

The idea

  • Plaintext: the readable message, MEET AT NOON.
  • A key: the secret setting that controls the scrambling.
  • Ciphertext: the scrambled result, PHHW DW QRRQ.
  • Encrypt: plaintext plus key to ciphertext. Decrypt: ciphertext plus key back to plaintext.

Without the key, the ciphertext should be useless.

The Caesar cipher

The oldest cipher shifts every letter along the alphabet by the key. With a key of 3, A becomes D, B becomes E, and so on, wrapping round at the end. To decrypt, shift back.

Two alphabets, the lower one shifted along by 3, with the letters of MEET AT NOON marked, and the message encrypted letter by letter underneathplaincipherADBECFDGEHFIGJHKILJMKNLOMPNQORPSQTRUSVTWUXVYWZXAYBZCeach letter moves 3 along; X, Y and Z wrap round to A, B and CMPEHEHTWspaceADTWspaceNQORORNQplaintextciphertextencrypt: +3decrypt: -3the same key, 3, locks and unlocks
The key of 3 is the whole secret: it lines up the two alphabets. Read down to encrypt and up to decrypt. The space is left alone, and the two Es, like the two Os, both come out the same, which is one reason a Caesar cipher is easy to break.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def caesar(text, key):
    out = ""
    for ch in text:
        if ch.isalpha():
            base = ord("A")
            out = out + chr((ord(ch.upper()) - base + key) % 26 + base)
        else:
            out = out + ch                    # leave spaces and punctuation
    return out

secret = caesar("MEET AT NOON", 3)
print("encrypted:", secret)
print("decrypted:", caesar(secret, -3))

Run this in the simulator

The Caesar cipher is easy to break: there are only 25 keys to try. Real encryption uses keys so large that trying them all would take longer than the age of the universe.

Symmetric and asymmetric

  • Symmetric encryption uses the same key to encrypt and decrypt, like the Caesar cipher. It is fast, but both sides must somehow share the key secretly first.
  • Asymmetric encryption uses a pair of keys: a public key anyone can use to encrypt a message to you, and a private key only you have, to decrypt it. The public key can be shared openly, which solves the problem of sharing keys.

When you see the padlock and https in a browser, asymmetric encryption is being used to agree a secret key, and then everything you send, passwords and card numbers included, is encrypted. Intercepting it gives an attacker only ciphertext.

Symmetric encryption, where both people hold the same key, which must first be shared secretly, and asymmetric encryption, where a public key is shared openly and only the private key decryptssymmetric: one shared keyyoufriendciphertextasymmetric: a key pairyoufriendciphertextencrypts with the keydecrypts with the same keybut first the key itself must be shared secretlypublic key, shared openlyencrypts with the public keydecrypts with the private keypublicprivatethe private key never leaves its owner
With symmetric encryption, in the top row, the hard part is getting the one key to your friend without anyone copying it. Asymmetric encryption, in the bottom row, only ever sends the public key. The padlock in a browser uses the bottom row to agree a key, then the top row for everything else.

A secret over the radio

The radio is a broadcast: every robot hears every message (lesson F10.1). So to keep a message private, encrypt it. The Ally robot below shares your key and reads your message; anyone else hears only ciphertext.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def caesar(text, key):
    out = ""
    for ch in text:
        if ch.isalpha():
            base = ord("A")
            out = out + chr((ord(ch.upper()) - base + key) % 26 + base)
        else:
            out = out + ch
    return out

KEY = 7
send(caesar("GO NORTH", KEY))
for tick in range(10):
    wait(0.1)
    for sender, text in messages():
        print("heard:", text, "-> means:", caesar(text, -KEY))

Run this in the simulator

Task: send a secret

Write caesar(text, key) that shifts letters by the key and leaves anything else unchanged. Using a key of 7, encrypt message, print sending: <ciphertext>, and send the ciphertext by radio. The Ally replies with its own encrypted message; decrypt each reply and print reply means: <plaintext>. You should read the Ally saying ALL CLEAR.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

message = "MEET AT BASE"

Challenges

  1. Break a Caesar cipher: given only the ciphertext, print all 25 possible decryptions and pick the one that reads as English.
  2. Why is a shift of 13 special? Encrypt a message twice with key 13.
  3. Explain why the public key can be shared openly without helping an attacker.

Past paper questions on this

Real exam questions that test this, each worked through with a program you can run.