Generating a one-time pad key in Python

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Generating a one-time pad key in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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)
3. Which import is needed before using random.randint() to make a key?
A import random
B import 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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

key = [random.randint(0, 25) for c in message]

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
import ______ msg = "HELLO" key = []
2.
key = [random._______(0, 25) for c in msg]
3.
key = [random.randint(0, __) for c in msg]
4.
key = [] for c in ___: key.append(random.randint(0, 25))
5.
key = [_______.randbelow(26) for c in msg]

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A from random import randint
B key = ''
C for c in 'HELLO':
D key.append(randint(0, 25))
Question 2
A message = 'ATTACK'
B import random
C key = []
D for c in message:
E n = secrets.randbelow(26)
F key.append(n)
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)
Question 4
A from random import randint
B m = 'ATTACK'
C k = [randint(0,25)] * len(m)
D print(len(k) == len(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)

Answer Key

Generating a one-time pad key in Python
Section A: Quick Questions
Q1
A — 5 values, one for each character
Q2
A — key = [random.randint(0, 25) for c in message]
Q3
A — import random
Q4
B — Any whole number from 0 to 25, including both ends
Q5
B — Never reuse it for another message
Section B: Match the Pairs
Q1
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
Q2
import random → Loads Python's random module so you can call random.randint() to pick random key values
Q3
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]
Q4
For stronger security, the secrets module can pick each random key value instead of random → key = [secrets.randbelow(26) for c in message]
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
import random msg = "HELLO" key = []
Q2
key = [random.randint(0, 25) for c in msg]
Q3
key = [random.randint(0, 25) for c in msg]
Q4
key = [] for c in msg: key.append(random.randint(0, 25))
Q5
key = [secrets.randbelow(26) for c in msg]
Section E: Spot the Error
Q1
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 = [].
Q2
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.
Q3
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:.
Q4
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].
Q5
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):.