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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
XOR cipherFill in the Blanks
Spot the Error
Student with a class code? Join your class →