← ACARA Digital Technologies Years 9–10

Transposition cipher: rail fence

A cipher that keeps the same letters but changes their order, like writing a message in a zigzag across rails and then reading each rail in turn.

Understanding Transposition cipher: rail fence in ACARA Digital Technologies Years 9–10

What it does: Sorting both texts gives the same list of letters, which shows the ciphertext is just a reordering of the plaintext. No letters were swapped for different ones. A transposition cipher rearranges the letters. Swapping letters for other letters is called substitution, like the Caesar cipher. Zigzag the letters: H, L, O land on rail 1 and E, L on rail 2. Read rail 1 first, then rail 2, to get HLOEL. Transposition never changes the letters themselves. It only moves them around. Rail 1 gets A and C. Rail 2 gets T, A and K. Rail 3 gets T. Read the rails in order to get ACTAKT.

Common mistakes: In a two-rail fence the bottom rail holds the letters at positions 1, 3, 5 and so on. Starting at 2 repeats letters from the top rail and drops others. It should be text[1::2]. The message is text, so it needs quotes. Without them Python treats MEETATNOON as a variable name and raises an error. It should be text = 'MEETATNOON'.

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
Which describes a transposition cipher?
It swaps each letter for a different letterIt keeps the same letters but changes their order
Matches
Rail fence of HELLO (2 rails) = IFMMP
↓
IFMMP replaces each letter with the next letter, which is a substitution (Caesar) cipher. A rail fence only reorders the original letters, so the answer is HLOEL.
Applying
Meanings
Term
transposition cipher
Meaning
A cipher that keeps the same letters but changes their order, like writing a message in a zigzag across rails and then reading each rail in turn.
Practice
message = "HELLOWORLD"
Testing
Gaps
top = text[0::]
Check Answer
Spots
2top = text[0::2]
3bottom = text[2::2]

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

The 'Butterfly' badge — Branch Hopper, 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 describes a transposition cipher?
A. It swaps each letter for a different letter
B. It keeps the same letters but changes their order ✓
A transposition cipher rearranges the letters. Swapping letters for other letters is called substitution, like the Caesar cipher.
2. Two-rail fence: what does HELLO become?
A. ELHLO
B. HLOEL ✓
Zigzag the letters: H, L, O land on rail 1 and E, L on rail 2. Read rail 1 first, then rail 2, to get HLOEL.
3. Which is true about a rail fence ciphertext?
A. It has exactly the same letters as the message, in a new order ✓
B. It has new letters that were not in the message
Transposition never changes the letters themselves. It only moves them around.
4. Three-rail fence: what does ATTACK become?
A. ACTAKT ✓
B. ACTKAT
Rail 1 gets A and C. Rail 2 gets T, A and K. Rail 3 gets T. Read the rails in order to get ACTAKT.
5. What is the key in a rail fence cipher?
A. The number of rails ✓
B. The number of places each letter is shifted
A shift amount is the key for a Caesar cipher. A rail fence key is how many rails the zigzag uses.

Match the Pairs

rail_fence("HELLO", 2) -> "HLOEL" → Writes HELLO in a zigzag over 2 rails, then reads the top rail (HLO) followed by the bottom rail (EL)
rail_fence("ATTACK", 3) -> "ACTAKT" → Zigzags ATTACK over 3 rails, then reads the top rail (AC), the middle rail (TAK) and the bottom rail (T)
A transposition cipher keeps exactly the same letters and only changes their order → sorted("HELLO") == sorted("HLOEL")
Columnar transposition writes the message in rows, then reads it down each column → ATT / ACK -> read columns: AATCTK
Rail fence of HELLO (2 rails) = IFMMP → IFMMP replaces each letter with the next letter, which is a substitution (Caesar) cipher. A rail fence only reorders the original letters, so the answer is HLOEL.
Decrypt HLOEL on 2 rails: top rail = HL, bottom rail = OEL → The top rail of a 5-letter message holds 3 letters, not 2. The correct split is top HLO and bottom EL, which rebuilds HELLO.

Key Term

transposition cipher
A cipher that keeps the same letters but changes their order, like writing a message in a zigzag across rails and then reading each rail in turn.

Fill in the Blanks

1. top = text[0::2]
Hint: Type the step size that picks every other letter for the top rail of a two-rail zigzag.
2. bottom = text[1::2]
Hint: Type the starting index of the second letter, where the bottom rail begins.
3. top = text[0::2] bottom = text[1::2] cipher = top + bottom
Hint: Type the variable name for the rail that is read after the top rail.
4. rail1 = text[0::4]
Hint: Type the gap between top-rail letters in a three-rail zigzag. The pattern repeats every 2 x 3 - 2 letters.
5. top = cipher[:(len(cipher) + 1) // 2]
Hint: Type the number to divide by to find how many letters the top rail holds when rebuilding a two-rail zigzag.

Spot the Error

Question 1
A text = 'MEETATNOON'
B top = text[0::2]
C bottom = text[2::2]
D print(top + bottom)
Line C — In a two-rail fence the bottom rail holds the letters at positions 1, 3, 5 and so on. Starting at 2 repeats letters from the top rail and drops others. It should be text[1::2].
Question 2
A text = MEETATNOON
B top = text[0::2]
C bottom = text[1::2]
D print(top + bottom)
Line A — The message is text, so it needs quotes. Without them Python treats MEETATNOON as a variable name and raises an error. It should be text = 'MEETATNOON'.
Question 3
A msg = 'WEAREDISCOVERED'
B rail1 = msg[0::4]
C rail2 = msg[1::4]
D rail3 = msg[2::4]
E print(rail1 + rail2 + rail3)
Line C — In a three-rail zigzag the middle rail is touched on the way down and on the way up. It gets every second letter (positions 1, 3, 5...). A step of 4 skips half of them. It should be msg[1::2].
Question 4
A cipher = 'MEANOETTON'
B half = len(cipher) / 2
C top = cipher[:half]
D bottom = cipher[half:]
E print(top, bottom)
Line B — To rebuild the zigzag we split the cipher text into the top rail and bottom rail. A single slash gives a decimal (5.0), and slice positions must be whole numbers. It should be // 2.
Question 5
A key = '3'
B text = 'MEETATNOON'
C for i in range(key):
D print(text[i::key])
Line A — In columnar transposition the key is the number of columns, so it must be an integer. Quotes make it a string and range() fails. It should be key = 3.

Student with a class code? Join your class →