← ACARA Digital Technologies Years 9–10

Decrypting a Caesar cipher in Python

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.

Understanding Decrypting a Caesar cipher in Python in ACARA Digital Technologies Years 9–10

What it does: Encrypting with 3 and then calling the same function with -3 should give the original message back, so the comparison prints True. The decrypt shift is the negative of the encrypt shift, so a shift of 5 is undone with -5. The same function works both ways. Only the sign of the shift changes. The % operator only handles wrap-around. It adds no real security. Shifting A back by 1 wraps around to Z because % 26 keeps the position inside the alphabet. Going back by 3 lands on the same letter as going forward by 23, because 26 - 3 = 23.

Common mistakes: 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). 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.

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 860–875) and word search (Modules 860–944).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A message was encrypted with caesar(text, 3). Which call decrypts it?
caesar(cipher, 3)caesar(cipher, -3)
Matches
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.
Applying
Meanings
Term
caesar(cipher, -shift)
Meaning
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.
Practice
def caesar(text, shift):
Testing
Gaps
plain = caesar(cipher, )
Check Answer
Spots
2cipher = caesar("HELLO", shift)
3plain = caesar(cipher, shift)

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

The 'Gazelle' badge — Tuple Sprinter, 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 message was encrypted with caesar(text, 3). Which call decrypts it?
A. caesar(cipher, -3) ✓
B. caesar(cipher, 3)
Shifting back by the same amount undoes the encryption, so you call the function with the negative shift.
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 ✓
The same function works both ways. Only the sign of the shift changes.
3. Which test shows that decrypting reverses encrypting?
A. caesar(caesar('HI', 5), -5) == 'HI' ✓
B. caesar(caesar('HI', 5), 5) == 'HI'
Encrypt with 5, then decrypt with -5. If you get 'HI' back, the decrypt works. Shifting by 5 twice shifts forward 10.
4. In Python, what does (0 - 3) % 26 give?
A. 23 ✓
B. -3
Python's % with a positive divisor gives a result from 0 to 25. A negative shift therefore still wraps around to the end of the alphabet.
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 ✓
The % operator only handles wrap-around. It adds no real security.

Match the Pairs

plain = caesar(cipher, -shift) → Decrypts the message by calling the same Caesar function with the negative of the original shift.
(ord(ch) - 65 + shift) % 26 → Moves a letter along by the shift and wraps around the alphabet, even when the shift is negative.
Checking that decrypt reverses encrypt: the decrypted text should equal the original message. → print(caesar(caesar(msg, 3), -3) == msg)
Decrypting a message that was encrypted with a shift of 5, using the same function. → plain = caesar(cipher, -5)
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.
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).

Key Term

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.

Fill in the Blanks

1. plain = caesar(cipher, -shift)
Hint: Type the argument that undoes the original shift: the shift variable made negative.
2. print(caesar(cipher, -3))
Hint: Type the variable name that holds the encrypted message being decrypted.
3. cipher = caesar("HELLO", 3) print(caesar(cipher, -3) == "HELLO")
Hint: Type the number that shifts back by the same amount used to encrypt.
4. new = (ord(ch) - 65 + shift) % 26
Hint: Type the operator that wraps the result back into 0 to 25, even when the shift is negative.
5. def decrypt(cipher, shift): return caesar(cipher, -shift)
Hint: Type the name of the existing function that decrypt reuses with a negative shift.

Spot the Error

Question 1
A shift = 3
B cipher = caesar("HELLO", shift)
C plain = caesar(cipher, shift)
D print(plain)
Line 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).
Question 2
A shift = 5
B back = shift
C plain = caesar(cipher, back)
D print(plain)
Line 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.
Question 3
A msg = "HELLO"
B cipher = caesar(msg, 4)
C check = caesar(msg, -4)
D print(check == msg)
Line C — To check that decrypt reverses encrypt, you must decrypt the cipher text, not the original message. It should be caesar(cipher, -4).
Question 4
A msg = "CAT"
B cipher = caesar(msg, -7)
C plain = caesar(cipher, -7)
D print(plain == msg)
Line 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).
Question 5
A def caesar(ch, shift)
B n = ord(ch) - 65
C n = (n + shift) % 26
D return chr(n + 65)
Line A — A Python function definition must end with a colon. It should be def caesar(ch, shift):

Student with a class code? Join your class →