← ACARA Digital Technologies Years 9–10

The XOR cipher

Encrypts by XOR-ing (^ in Python) each character code with a key number. XOR-ing the result with the same key again restores the original, so one key encrypts and decrypts.

Understanding The XOR cipher in ACARA Digital Technologies Years 9–10

What it does: The first XOR scrambles 72 into 65. XOR-ing with 9 again restores 72, so the same key encrypts and decrypts. The loop visits each character in the message and adds its XOR-scrambled version to the output. In Python, ^ is the XOR operator. ** means 'to the power of'. XOR means 'exclusive or'. 0 ^ 1 and 1 ^ 0 give 1, but 1 ^ 1 and 0 ^ 0 give 0. Both bits are the same, so XOR gives 0. XOR each pair of bits: 01000001 ^ 00000101 = 01000100, which is 68. XOR-ing with the same key twice cancels out, so you get the original number back.

Common mistakes: A for loop header must end with a colon. It should be: for ch in text:. & is the AND operator, not XOR. The XOR cipher uses ^, so it should be: code = ord("A") ^ key.

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 876–944) 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
Which Python operator performs XOR on two numbers?
**^
Matches
chr("A" ^ 5)
↓
The ^ operator needs numbers, but "A" is a string. The letter must be converted first with ord(), as in chr(ord("A") ^ 5).
Applying
Meanings
Term
XOR cipher
Meaning
Encrypts by XOR-ing (^ in Python) each character code with a key number. XOR-ing the result with the same key again restores the original, so one key encrypts and decrypts.
Practice
key = 5
Testing
Gaps
print(5 3)
Check Answer
Spots
3out = ""
4for ch in text

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

The 'Barreleye Fish' badge — Clear Vision, 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. Which Python operator performs XOR on two numbers?
A. ^ ✓
B. **
In Python, ^ is the XOR operator. ** means 'to the power of'.
2. When does XOR give an output bit of 1?
A. When the two input bits are different ✓
B. When both input bits are 1
XOR means 'exclusive or'. 0 ^ 1 and 1 ^ 0 give 1, but 1 ^ 1 and 0 ^ 0 give 0.
3. What is the result of 1 ^ 1 in Python?
A. 1
B. 0 ✓
Both bits are the same, so XOR gives 0.
4. What is 65 ^ 5? (65 is 01000001 in binary and 5 is 00000101.)
A. 68 ✓
B. 70
XOR each pair of bits: 01000001 ^ 00000101 = 01000100, which is 68.
5. What does 65 ^ 20 ^ 20 equal?
A. 20
B. 65 ✓
XOR-ing with the same key twice cancels out, so you get the original number back.

Match the Pairs

65 ^ 5 → XORs the numbers 65 and 5 to give 68, the character code for D
chr(ord(ch) ^ key) → Turns a letter into its code, XORs it with the key, then turns the result back into a character
XOR-ing a number with the same key twice gives back the original number → (72 ^ 9) ^ 9
To encrypt a whole message, XOR every character code in it with the key → for c in msg: out += chr(ord(c) ^ key)
chr("A" ^ 5) → The ^ operator needs numbers, but "A" is a string. The letter must be converted first with ord(), as in chr(ord("A") ^ 5).
plain = chr(ord(c) ^ 7) # key was 5 → Decrypting with a different key (7) does not restore the original. XOR cipher needs the same key (5) for both steps.

Key Term

XOR cipher
Encrypts by XOR-ing (^ in Python) each character code with a key number. XOR-ing the result with the same key again restores the original, so one key encrypts and decrypts.

Fill in the Blanks

1. print(5 ^ 3)
Hint: Type the Python operator that performs XOR on two numbers.
2. scrambled = ord("A") ^ 7
Hint: Type the function that turns a character into its numeric code.
3. key = 7 scrambled = ord("H") ^ key back = scrambled ^ key
Hint: Type the variable holding the same number used to encrypt, because one key reverses the scramble.
4. key = 7 letter = chr(72 ^ key)
Hint: Type the function that turns a character code back into a character.
5. key = 9 for ch in "HI": print(chr(ord(ch) ^ key))
Hint: Type the keyword that starts a loop over every character in the message.

Spot the Error

Question 1
A key = 42
B text = "Hi"
C out = ""
D for ch in text
E out += chr(ord(ch) ^ key)
Line D — A for loop header must end with a colon. It should be: for ch in text:
Question 2
A key = 7
B code = ord("A") & key
C print(code)
Line B — & is the AND operator, not XOR. The XOR cipher uses ^, so it should be: code = ord("A") ^ key
Question 3
A key = 5
B msg = "Hello"
C for ch in msg:
D n = ord(ch) ** key
E print(chr(n))
Line D — ** raises a number to a power, which is not XOR. The character code must be XOR-ed with the key using ^.
Question 4
A key = "42"
B msg = "Hi"
C for ch in msg:
D print(ord(ch) ^ key)
Line A — The key must be a number so ^ can XOR it with the character code. "42" is a string and causes an error. It should be: key = 42
Question 5
A def xor_text(text, key):
B out = 0
C for ch in text:
D out += chr(ord(ch) ^ key)
E return out
Line B — out collects characters, so it must start as an empty string. Adding a character to the number 0 causes an error. It should be: out = ""

Student with a class code? Join your class →