One-time pad cipher

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

One-time pad cipher

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. How is each key letter in a one-time pad chosen?
A Randomly
B From a meaningful word like a name or pet
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
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
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

Match the Pairs

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

Left columnRight column
1key = ''.join(random.choice(string.ascii_uppercase) for _ in message) Akey = generate_key(len(message))
2if len(key) != len(message): BThe 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.
3Each message letter is shifted by the matching key letter, so the key never repeats CChecks that the key is the same length as the message
4A key that has been used once must be thrown away and never reused DThe same key is used for two messages. Reusing a one-time pad key makes it breakable.
5key = 'KEY' for i in range(len(message)): shift = ord(key[i]) - 65 Eshift = ord(key[i]) - 65
6key = 'XMCKL' secret1 = encrypt('HELLO', key) secret2 = encrypt('WORLD', key) FBuilds a random key with one random letter for every letter in the message

Definitions

one-time pad

Fill in the Blanks

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

1.
key = "" for i in range(len(message)): key += random.______(letters)
2.
for i in range(___(message)): key += random.choice(letters)
3.
shift = ord(kch) - 65 code = (ord(ch) - 65 + shift) _ 26
4.
if key __ used: print("Key reused - not secure!")
5.
if len(key) _ len(message): print("Key too short!")

Spot the Error

Shade in the letter of the line that contains an 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)
Question 2
A msg = "ATTACK"
B key = "XMCKLQ"
C if len(key) == len(msg):
D print("Key must match msg")
Question 3
A m = ord("H") - 65
B k = ord("X") - 65
C c = (m + k) % 25
D print(chr(c + 65))
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
Question 5
A import math
B key = ""
C for i in range(5):
D k = random.randint(65, 90)
E key += chr(k)

Answer Key

One-time pad cipher
Section A: Quick Questions
Q1
A — At least as long as the message
Q2
A — Randomly
Q3
B — The key must be used once only
Q4
B — The key is random, as long as the message and never reused, so the ciphertext shows no pattern
Q5
A — Pick 5 random letters, one for each letter of the message
Section B: Match the Pairs
Q1
key = ''.join(random.choice(string.ascii_uppercase) for _ in message) → Builds a random key with one random letter for every letter in the message
Q2
if len(key) != len(message): → Checks that the key is the same length as the message
Q3
Each message letter is shifted by the matching key letter, so the key never repeats → shift = ord(key[i]) - 65
Q4
A key that has been used once must be thrown away and never reused → key = generate_key(len(message))
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
key = "" for i in range(len(message)): key += random.choice(letters)
Q2
for i in range(len(message)): key += random.choice(letters)
Q3
shift = ord(kch) - 65 code = (ord(ch) - 65 + shift) % 26
Q4
if key in used: print("Key reused - not secure!")
Q5
if len(key) < len(message): print("Key too short!")
Section E: Spot the Error
Q1
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.
Q2
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).
Q3
C — The alphabet has 26 letters, so the shifted value must wrap around with % 26. Using % 25 gives wrong ciphertext letters.
Q4
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".
Q5
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.