← QCE Digital Solutions Unit 4

One-time pad cipher

A cipher that encrypts each letter with a random key letter. The key is as long as the message and is used once only, so it is unbreakable if used correctly.

Understanding One-time pad cipher in QCE Digital Solutions Unit 4

What it does: The key letter at position i gives the shift for the message letter at position i, so every letter has its own key letter. Making a fresh random key for every new message follows the use-once-only rule of a one-time pad. A truly random key leaves no pattern for an attacker to find. Words and names create patterns that can be guessed. The key length must match the message length. Using one letter for the whole message is just a Caesar cipher, which is easy to break.

Common mistakes: The key is built up as a string of random letters, so it must start as an empty string (key = ""). Starting at 0 would cause a type error when a letter is added. A one-time pad key must be as long as the message. The warning should show when the lengths are NOT equal, so the test should be len(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 334–401) and word search (Modules 127–401).

Take this worksheet further — free with Cloodit

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

Learning
Duo
How long must a one-time pad key be compared with the message?
A short word that repeats along the messageAt least as long as the message
Matches
key = 'KEY' for i in range(len(message)): shift = ord(key[i]) - 65
↓
The key is shorter than the message, so key[i] causes an IndexError and the key would have to repeat. A one-time pad key must be as long as the message.
Applying
Meanings
Term
one-time pad
Meaning
A cipher that encrypts each letter with a random key letter. The key is as long as the message and is used once only, so it is unbreakable if used correctly.
Practice
message = "HELLO"
Testing
Gaps
key += random.(letters)
Check Answer
Spots
1import random
2key = 0

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

The 'Woodland Hedgehog' badge — Import Master, 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. How long must a one-time pad key be compared with the message?
A. At least as long as the message ✓
B. A short word that repeats along the message
The key needs one random letter for every letter of the message. A repeating short key would make the cipher a Vigenere cipher, which can be broken.
2. How is each key letter in a one-time pad chosen?
A. Randomly ✓
B. From a meaningful word like a name or pet
A truly random key leaves no pattern for an attacker to find. Words and names create patterns that can be guessed.
3. Which statement about reusing a one-time pad key is correct?
A. The same key can safely encrypt many messages
B. The key must be used once only ✓
Reusing a key leaks patterns between messages, so an attacker can compare them and start to break the cipher. That is why it is called one-time.
4. Why is a one-time pad unbreakable when it is used correctly?
A. The algorithm is so complicated that computers cannot run it
B. The key is random, as long as the message and never reused, so the ciphertext shows no pattern ✓
The security comes from the key, not from a complicated algorithm. With a random, single-use key as long as the message, every possible message is equally likely.
5. Which Python idea fits generating a one-time pad key for the message "HELLO"?
A. Pick 5 random letters, one for each letter of the message ✓
B. Pick 1 random letter and use it for all 5 letters
The key length must match the message length. Using one letter for the whole message is just a Caesar cipher, which is easy to break.

Match the Pairs

key = ''.join(random.choice(string.ascii_uppercase) for _ in message) → Builds a random key with one random letter for every letter in the message
if len(key) != len(message): → Checks that the key is the same length as the message
Each message letter is shifted by the matching key letter, so the key never repeats → shift = ord(key[i]) - 65
A key that has been used once must be thrown away and never reused → key = generate_key(len(message))
key = 'KEY' for i in range(len(message)): shift = ord(key[i]) - 65 → The key is shorter than the message, so key[i] causes an IndexError and the key would have to repeat. A one-time pad key must be as long as the message.
key = 'XMCKL' secret1 = encrypt('HELLO', key) secret2 = encrypt('WORLD', key) → The same key is used for two messages. Reusing a one-time pad key makes it breakable.

Key Term

one-time pad
A cipher that encrypts each letter with a random key letter. The key is as long as the message and is used once only, so it is unbreakable if used correctly.

Fill in the Blanks

1. key = "" for i in range(len(message)): key += random.choice(letters)
Hint: Type the random function that picks one item from a sequence, so each key letter is unpredictable.
2. for i in range(len(message)): key += random.choice(letters)
Hint: Type the built-in function that gives the message length, so the key is as long as the message.
3. shift = ord(kch) - 65 code = (ord(ch) - 65 + shift) % 26
Hint: Type the operator that wraps the shifted value back into the 26 letters of the alphabet.
4. if key in used: print("Key reused - not secure!")
Hint: Type the keyword that checks whether a value is already inside a list of past keys.
5. if len(key) < len(message): print("Key too short!")
Hint: Type the comparison operator that detects a key shorter than the message.

Spot the Error

Question 1
A import random
B key = 0
C for ch in msg:
D k = random.randint(0, 25)
E key += chr(65 + k)
Line B — The key is built up as a string of random letters, so it must start as an empty string (key = ""). Starting at 0 would cause a type error when a letter is added.
Question 2
A msg = "ATTACK"
B key = "XMCKLQ"
C if len(key) == len(msg):
D print("Key must match msg")
Line C — A one-time pad key must be as long as the message. The warning should show when the lengths are NOT equal, so the test should be len(key) != len(msg).
Question 3
A m = ord("H") - 65
B k = ord("X") - 65
C c = (m + k) % 25
D print(chr(c + 65))
Line C — The alphabet has 26 letters, so the shifted value must wrap around with % 26. Using % 25 gives wrong ciphertext letters.
Question 4
A msg = "HELLO"
B key = msg
C for i in range(len(msg)):
D m = ord(msg[i]) - 65
E k = ord(key[i]) - 65
Line B — The key of a one-time pad must be random. Using the message as its own key is predictable and gives no security. The key should be a random string such as "XMCKL".
Question 5
A import math
B key = ""
C for i in range(5):
D k = random.randint(65, 90)
E key += chr(k)
Line A — The random key letters come from random.randint(), so the random module must be imported. The math module has no randint function. It should be import random.

Student with a class code? Join your class →