The XOR cipher

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

The XOR cipher

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which Python operator performs XOR on two numbers?
A ^
B **
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
3. What is the result of 1 ^ 1 in Python?
A 1
B 0
4. What is 65 ^ 5? (65 is 01000001 in binary and 5 is 00000101.)
A 68
B 70
5. What does 65 ^ 20 ^ 20 equal?
A 20
B 65

Match the Pairs

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

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

Definitions

XOR cipher

Fill in the Blanks

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

1.
print(5 _ 3)
2.
scrambled = ___("A") ^ 7
3.
key = 7 scrambled = ord("H") ^ key back = scrambled ^ ___
4.
key = 7 letter = ___(72 ^ key)
5.
key = 9 ___ ch in "HI": print(chr(ord(ch) ^ key))

Spot the Error

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

Question 1
A key = 42
B text = "Hi"
C out = ""
D for ch in text
E out += chr(ord(ch) ^ key)
Question 2
A key = 7
B code = ord("A") & key
C print(code)
Question 3
A key = 5
B msg = "Hello"
C for ch in msg:
D n = ord(ch) ** key
E print(chr(n))
Question 4
A key = "42"
B msg = "Hi"
C for ch in msg:
D print(ord(ch) ^ key)
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

Answer Key

The XOR cipher
Section A: Quick Questions
Q1
A — ^
Q2
A — When the two input bits are different
Q3
B — 0
Q4
A — 68
Q5
B — 65
Section B: Match the Pairs
Q1
65 ^ 5 → XORs the numbers 65 and 5 to give 68, the character code for D
Q2
chr(ord(ch) ^ key) → Turns a letter into its code, XORs it with the key, then turns the result back into a character
Q3
XOR-ing a number with the same key twice gives back the original number → (72 ^ 9) ^ 9
Q4
To encrypt a whole message, XOR every character code in it with the key → for c in msg: out += chr(ord(c) ^ key)
Q5
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).
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
print(5 ^ 3)
Q2
scrambled = ord("A") ^ 7
Q3
key = 7 scrambled = ord("H") ^ key back = scrambled ^ key
Q4
key = 7 letter = chr(72 ^ key)
Q5
key = 9 for ch in "HI": print(chr(ord(ch) ^ key))
Section E: Spot the Error
Q1
D — A for loop header must end with a colon. It should be: for ch in text:
Q2
B — & is the AND operator, not XOR. The XOR cipher uses ^, so it should be: code = ord("A") ^ key
Q3
D — ** raises a number to a power, which is not XOR. The character code must be XOR-ed with the key using ^.
Q4
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
Q5
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 = ""