Decrypting a Caesar cipher in Python

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

Decrypting a Caesar cipher in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A message was encrypted with caesar(text, 3). Which call decrypts it?
A caesar(cipher, -3)
B caesar(cipher, 3)
2. Which is true about decrypting a Caesar cipher in Python?
A Write a totally different decrypt function from scratch
B Reuse the same caesar() function with -shift
3. Which test shows that decrypting reverses encrypting?
A caesar(caesar('HI', 5), -5) == 'HI'
B caesar(caesar('HI', 5), 5) == 'HI'
4. In Python, what does (0 - 3) % 26 give?
A 23
B -3
5. Why does the caesar function use % 26?
A It makes the cipher much harder to crack
B It keeps letters wrapping around, even for negative shifts

Match the Pairs

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

Left columnRight column
1plain = caesar(cipher, -shift) Aplain = caesar(cipher, -5)
2(ord(ch) - 65 + shift) % 26 BCalling caesar with the same positive shift shifts the letters forward again instead of back, so the message is not decrypted. It should be -shift.
3Checking that decrypt reverses encrypt: the decrypted text should equal the original message. Cprint(caesar(caesar(msg, 3), -3) == msg)
4Decrypting a message that was encrypted with a shift of 5, using the same function. DMoves a letter along by the shift and wraps around the alphabet, even when the shift is negative.
5plain = caesar(cipher, shift) EDecrypts the message by calling the same Caesar function with the negative of the original shift.
6new = chr(ord(ch) - 65 + shift + 65) FThe code is missing % 26, so letters do not wrap around the alphabet and negative shifts can give the wrong characters. It should be chr((ord(ch) - 65 + shift) % 26 + 65).

Definitions

caesar(cipher, -shift)

Fill in the Blanks

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

1.
plain = caesar(cipher, ______)
2.
print(caesar(______, -3))
3.
cipher = caesar("HELLO", 3) print(caesar(cipher, __) == "HELLO")
4.
new = (ord(ch) - 65 + shift) _ 26
5.
def decrypt(cipher, shift): return ______(cipher, -shift)

Spot the Error

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

Question 1
A shift = 3
B cipher = caesar("HELLO", shift)
C plain = caesar(cipher, shift)
D print(plain)
Question 2
A shift = 5
B back = shift
C plain = caesar(cipher, back)
D print(plain)
Question 3
A msg = "HELLO"
B cipher = caesar(msg, 4)
C check = caesar(msg, -4)
D print(check == msg)
Question 4
A msg = "CAT"
B cipher = caesar(msg, -7)
C plain = caesar(cipher, -7)
D print(plain == msg)
Question 5
A def caesar(ch, shift)
B n = ord(ch) - 65
C n = (n + shift) % 26
D return chr(n + 65)

Answer Key

Decrypting a Caesar cipher in Python
Section A: Quick Questions
Q1
A — caesar(cipher, -3)
Q2
B — Reuse the same caesar() function with -shift
Q3
A — caesar(caesar('HI', 5), -5) == 'HI'
Q4
A — 23
Q5
B — It keeps letters wrapping around, even for negative shifts
Section B: Match the Pairs
Q1
plain = caesar(cipher, -shift) → Decrypts the message by calling the same Caesar function with the negative of the original shift.
Q2
(ord(ch) - 65 + shift) % 26 → Moves a letter along by the shift and wraps around the alphabet, even when the shift is negative.
Q3
Checking that decrypt reverses encrypt: the decrypted text should equal the original message. → print(caesar(caesar(msg, 3), -3) == msg)
Q4
Decrypting a message that was encrypted with a shift of 5, using the same function. → plain = caesar(cipher, -5)
Q5
plain = caesar(cipher, shift) → Calling caesar with the same positive shift shifts the letters forward again instead of back, so the message is not decrypted. It should be -shift.
Q6
new = chr(ord(ch) - 65 + shift + 65) → The code is missing % 26, so letters do not wrap around the alphabet and negative shifts can give the wrong characters. It should be chr((ord(ch) - 65 + shift) % 26 + 65).
Section C: Definitions
Q1
caesar(cipher, -shift) — Decrypts by calling the same Caesar function with the negative of the original shift. Shifting back by the same amount undoes the encryption, and % keeps the letters wrapping.
Section D: Fill in the Blanks
Q1
plain = caesar(cipher, -shift)
Q2
print(caesar(cipher, -3))
Q3
cipher = caesar("HELLO", 3) print(caesar(cipher, -3) == "HELLO")
Q4
new = (ord(ch) - 65 + shift) % 26
Q5
def decrypt(cipher, shift): return caesar(cipher, -shift)
Section E: Spot the Error
Q1
C — Calling caesar with the same shift encrypts the text a second time instead of undoing it. To decrypt, pass the negative shift: caesar(cipher, -shift).
Q2
B — The value used for decrypting must be the negative of the original shift. It should be back = -shift, otherwise the text is shifted forward again.
Q3
C — To check that decrypt reverses encrypt, you must decrypt the cipher text, not the original message. It should be caesar(cipher, -4).
Q4
B — Encrypting needs the positive shift 7. Encrypting with -7 means that decrypting with -7 later shifts back twice, so the check fails. It should be caesar(msg, 7).
Q5
A — A Python function definition must end with a colon. It should be def caesar(ch, shift):