Encryption
Plaintext, keys and ciphertext, the Caesar cipher, symmetric and asymmetric, and a secret over the radio.
Do this lesson in the simulatorData 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.
# 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))
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.
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))
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
- Break a Caesar cipher: given only the ciphertext, print all 25 possible decryptions and pick the one that reads as English.
- Why is a shift of 13 special? Encrypt a message twice with key 13.
- Explain why the public key can be shared openly without helping an attacker.