← QCE Digital Solutions Unit 4

One-time pad

Encryption using a truly random key as long as the message, used only once. Never reused, it is unbreakable in theory, but the pad must be shared securely.

Understanding One-time pad in QCE Digital Solutions Unit 4

What it does: This check finds a key that is too short, which breaks the rule that the pad is as long as the message. A 2 GB video needs a 2 GB pad, which must also be shared securely, so it is impractical for large data. The pad must be at least as long as the message. If the key repeats, patterns appear and the cipher can be attacked. A predictable key gives an attacker something to guess. A truly random key leaves no pattern to exploit. Reusing a key lets an attacker compare the two ciphertexts and work out information about both messages. That is why it is called one-time.

Common mistakes: The key must be as long as the message. Looping over only 3 values gives a key shorter than the 5-number message. It should be for n in msg: so there is one random key value per message value. A one-time pad key must be exactly as long as the message. Subtracting 2 leaves the last two characters with no key value. It should be key = make_key(len(msg)).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 17–88) and word search (Modules 17–125).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which key length makes a one-time pad work?
A short key that repeats over the messageA key as long as the message
Matches
key = "SECRET" message = "MEET AT NOON"
↓
The key is shorter than the message and is not random, so it would have to repeat and the pad is no longer unbreakable
Applying
Meanings
Term
one-time pad
Meaning
Encryption using a truly random key as long as the message, used only once. Never reused, it is unbreakable in theory, but the pad must be shared securely.
Practice
message = "HELLO"
Testing
Gaps
if len(key) len(message):
Check Answer
Spots
3key = []
4for n in range(3):

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Sperm Whale' badge — Deep Sea Champion, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which key length makes a one-time pad work?
A. A key as long as the message ✓
B. A short key that repeats over the message
The pad must be at least as long as the message. If the key repeats, patterns appear and the cipher can be attacked.
2. How should the key for a one-time pad be generated?
A. From a memorable phrase such as a pet's name
B. Truly randomly ✓
A predictable key gives an attacker something to guess. A truly random key leaves no pattern to exploit.
3. Alex encrypts two different messages with the same pad page. Is this safe?
A. No, a pad key must never be reused ✓
B. Yes, the key is random so it can be used again
Reusing a key lets an attacker compare the two ciphertexts and work out information about both messages. That is why it is called one-time.
4. Why is a one-time pad described as unbreakable in theory?
A. A random, single-use key leaves no pattern to analyse ✓
B. Computers are not yet fast enough to try every key
The security comes from the key, not from the attacker's computing power. Every possible message is equally likely, so brute force and frequency analysis give nothing.
5. Which is the biggest challenge of sharing a one-time pad?
A. The pad can be sent openly because it is random
B. Both parties need the same pad, delivered securely ✓
A one-time pad is symmetric, so the sender and receiver both need the same secret key. If an interceptor gets the pad, they can decrypt everything.

Match the Pairs

key = random_bytes(len(message)) → Generates a truly random key exactly as long as the message, as a one-time pad needs
pad_used = True → Marks the pad as used so it is never used to encrypt a second message
A one-time pad key must be at least as long as the message, so a 500-character message needs a 500-character key → if len(key) < len(message):
A one-time pad is impractical for large data because the pad must be as big as the data and shared securely first → pad_size = len(video_file)
key = "SECRET" message = "MEET AT NOON" → The key is shorter than the message and is not random, so it would have to repeat and the pad is no longer unbreakable
encrypt(msg1, pad) encrypt(msg2, pad) → The same pad is reused for two messages, so an attacker can compare the ciphertexts and the security is lost

Key Term

one-time pad
Encryption using a truly random key as long as the message, used only once. Never reused, it is unbreakable in theory, but the pad must be shared securely.

Fill in the Blanks

1. if len(key) == len(message): print("Pad is long enough")
Hint: Type the operator that checks the key length is equal to the message length.
2. cipherNum = (letterNum + padNum) % 26
Hint: Type the operator that wraps a number back into the 0 to 25 alphabet range.
3. if pad in usedPads: print("Never reuse a pad")
Hint: Type the keyword that checks whether a pad is already inside the list of used pads.
4. key = [random.randint(0, 25) for c in msg]
Hint: Type the random module function that picks a whole number between two limits.
5. padSize = len(bigFile) print(padSize, "bytes to share securely")
Hint: Type the function that counts how many items or bytes are in the data, which sets how big the pad must be.

Spot the Error

Question 1
A import random
B msg = [7, 4, 11, 11, 14]
C key = []
D for n in range(3):
E key.append(random.randint(0,25))
Line D — The key must be as long as the message. Looping over only 3 values gives a key shorter than the 5-number message. It should be for n in msg: so there is one random key value per message value.
Question 2
A key = make_key(len(msg) - 2)
B cipher = []
C for i in range(len(msg)):
D c = (msg[i] + key[i]) % 26
E cipher.append(c)
Line A — A one-time pad key must be exactly as long as the message. Subtracting 2 leaves the last two characters with no key value. It should be key = make_key(len(msg)).
Question 3
A msg = input('Message: ')
B pad = last_pad
C cipher = encrypt(msg, pad)
D send(cipher)
E destroy(pad)
Line B — A one-time pad key must never be reused. Using last_pad recycles an old key, which breaks the security. It should generate a fresh random pad: pad = random_pad(len(msg)).
Question 4
A pad = random_pad(len(msg))
B share(pad, 'public_forum')
C cipher = encrypt(msg, pad)
D send(cipher, 'internet')
Line B — The pad must be shared securely, such as by handing it over in person. Posting it on a public forum lets an interceptor read the message. It should be share(pad, 'in_person').
Question 5
A for i in range(len(c)):
B n = (c[i] + pad[i]) % 26
C plain.append(n)
Line B — Encryption adds the pad value, so decryption must subtract the same pad value. Adding again gives the wrong letters. It should be n = (c[i] - pad[i]) % 26.

Student with a class code? Join your class →