← QCE Digital Solutions Unit 4

Generating a one-time pad key in Python

Builds a one-time pad key by picking one random number from 0 to 25 for each character in the message, so the key is as long as the message. Never reuse a key.

Understanding Generating a one-time pad key in Python in QCE Digital Solutions Unit 4

What it does: Looping over message gives one random number for each character, so len(key) equals len(message). secrets.randbelow(26) gives a number from 0 to 25 and is designed for security use, so it suits one-time pad keys. randint is a function inside the random module, so you import random and then call random.randint(). Reusing a key lets an attacker compare messages and find patterns. This breaks the security of the one-time pad. A pattern can be guessed. Every key value must be random and independent of the others. Generate a brand new key for every message, by running the key-building line again with the new message.

Common mistakes: The key must start as an empty list so that .append() can add one random number per character. A string has no append method, so it should be key = []. The code calls secrets.randbelow(26), so it must import the secrets module. Importing random leaves secrets undefined and causes a NameError. It should be import secrets.

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
A one-time pad key for the message "HELLO" must be how long?
3 values, since a short key repeated is fine5 values, one for each character
Matches
key = [random.randint(0, 25)] * len(message)
↓
The same random number is repeated for every character, so the key is really one value copied many times and is easy to crack.
Applying
Meanings
Term
key = [random.randint(0, 25) for c in message]
Meaning
Builds a one-time pad key by picking one random number from 0 to 25 for each character in the message, so the key is as long as the message. Never reuse a key.
Practice
import random
Testing
Gaps
import
Check Answer
Spots
1from random import randint
2key = ''

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. A one-time pad key for the message "HELLO" must be how long?
A. 5 values, one for each character ✓
B. 3 values, since a short key repeated is fine
The key must be as long as the message. One random value is chosen for every character.
2. Which line builds a one-time pad key with one random number (0 to 25) per character?
A. key = [random.randint(0, 25) for c in message] ✓
B. key = random.randint(0, 25)
The list comprehension runs once for each character in message. A single randint call gives only one number, not a full key.
3. Which import is needed before using random.randint() to make a key?
A. import random ✓
B. import randint
randint is a function inside the random module, so you import random and then call random.randint().
4. What range of numbers does random.randint(0, 25) produce?
A. Any whole number from 0 to 24 only
B. Any whole number from 0 to 25, including both ends ✓
random.randint includes both end values. That gives 26 possible values, one for each letter of the alphabet.
5. After using a one-time pad key to encrypt a message, what should you do with that key?
A. Reuse it to save generating a new one
B. Never reuse it for another message ✓
Reusing a key lets an attacker compare messages and find patterns. This breaks the security of the one-time pad.

Match the Pairs

key = [random.randint(0, 25) for c in message] → Builds a key list with one random number from 0 to 25 for every character in the message
import random → Loads Python's random module so you can call random.randint() to pick random key values
A one-time pad key must be exactly as long as the message, so build it with one random value per character → key = [random.randint(0, 25) for c in message]
For stronger security, the secrets module can pick each random key value instead of random → key = [secrets.randbelow(26) for c in message]
key = [random.randint(0, 25)] * len(message) → The same random number is repeated for every character, so the key is really one value copied many times and is easy to crack.
key = [random.randint(0, 25) for c in message] # reuse the same key for the next message → A one-time pad key must never be reused. Using it for a second message lets attackers compare the two messages and break the encryption.

Key Term

key = [random.randint(0, 25) for c in message]
Builds a one-time pad key by picking one random number from 0 to 25 for each character in the message, so the key is as long as the message. Never reuse a key.

Fill in the Blanks

1. import random msg = "HELLO" key = []
Hint: Type the name of the standard library module that provides chance-based number generation.
2. key = [random.randint(0, 25) for c in msg]
Hint: Type the function name that picks a whole number between two limits, inclusive.
3. key = [random.randint(0, 25) for c in msg]
Hint: Type the highest shift value, which is the final position in the 26-letter alphabet when counting from zero.
4. key = [] for c in msg: key.append(random.randint(0, 25))
Hint: Type the variable the loop must step through so there is one key value for every character.
5. key = [secrets.randbelow(26) for c in msg]
Hint: Type the module designed for cryptographically secure random numbers, a safer choice for real keys.

Spot the Error

Question 1
A from random import randint
B key = ''
C for c in 'HELLO':
D key.append(randint(0, 25))
Line B — The key must start as an empty list so that .append() can add one random number per character. A string has no append method, so it should be key = [].
Question 2
A message = 'ATTACK'
B import random
C key = []
D for c in message:
E n = secrets.randbelow(26)
F key.append(n)
Line B — The code calls secrets.randbelow(26), so it must import the secrets module. Importing random leaves secrets undefined and causes a NameError. It should be import secrets.
Question 3
A import random
B message = 'HELLO'
C key = []
D for c in key:
E n = random.randint(0, 25)
F key.append(n)
Line D — The loop must run once for each character of the message. key is still empty, so the loop never runs and no key is built. It should be for c in message:.
Question 4
A from random import randint
B m = 'ATTACK'
C k = [randint(0,25)] * len(m)
D print(len(k) == len(m))
Line C — This picks one random number and repeats it for every position, so the key is not random per character. The comprehension should pick a new number for each character: k = [randint(0,25) for c in m].
Question 5
A from random import randint
B def make_key():
C key = []
D for c in message:
E n = randint(0, 25)
F key.append(n)
Line B — The function needs the message as a parameter so the key can be as long as the message. Without it, message is undefined inside the loop. It should be def make_key(message):.

Student with a class code? Join your class →