Transposition cipher: rail fence

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

Transposition cipher: rail fence

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Two-rail fence: what does HELLO become?
A ELHLO
B 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
4. Three-rail fence: what does ATTACK become?
A ACTAKT
B ACTKAT
5. What is the key in a rail fence cipher?
A The number of rails
B The number of places each letter is shifted

Match the Pairs

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

Left columnRight column
1rail_fence("HELLO", 2) -> "HLOEL" Asorted("HELLO") == sorted("HLOEL")
2rail_fence("ATTACK", 3) -> "ACTAKT" BThe top rail of a 5-letter message holds 3 letters, not 2. The correct split is top HLO and bottom EL, which rebuilds HELLO.
3A transposition cipher keeps exactly the same letters and only changes their order CIFMMP 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.
4Columnar transposition writes the message in rows, then reads it down each column DWrites HELLO in a zigzag over 2 rails, then reads the top rail (HLO) followed by the bottom rail (EL)
5Rail fence of HELLO (2 rails) = IFMMP EATT / ACK -> read columns: AATCTK
6Decrypt HLOEL on 2 rails: top rail = HL, bottom rail = OEL FZigzags ATTACK over 3 rails, then reads the top rail (AC), the middle rail (TAK) and the bottom rail (T)

Definitions

transposition cipher

Fill in the Blanks

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

1.
top = text[0::_]
2.
bottom = text[_::2]
3.
top = text[0::2] bottom = text[1::2] cipher = top + ______
4.
rail1 = text[0::_]
5.
top = cipher[:(len(cipher) + 1) // _]

Spot the Error

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

Question 1
A text = 'MEETATNOON'
B top = text[0::2]
C bottom = text[2::2]
D print(top + bottom)
Question 2
A text = MEETATNOON
B top = text[0::2]
C bottom = text[1::2]
D print(top + bottom)
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)
Question 4
A cipher = 'MEANOETTON'
B half = len(cipher) / 2
C top = cipher[:half]
D bottom = cipher[half:]
E print(top, bottom)
Question 5
A key = '3'
B text = 'MEETATNOON'
C for i in range(key):
D print(text[i::key])

Answer Key

Transposition cipher: rail fence
Section A: Quick Questions
Q1
B — It keeps the same letters but changes their order
Q2
B — HLOEL
Q3
A — It has exactly the same letters as the message, in a new order
Q4
A — ACTAKT
Q5
A — The number of rails
Section B: Match the Pairs
Q1
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)
Q2
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)
Q3
A transposition cipher keeps exactly the same letters and only changes their order → sorted("HELLO") == sorted("HLOEL")
Q4
Columnar transposition writes the message in rows, then reads it down each column → ATT / ACK -> read columns: AATCTK
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
top = text[0::2]
Q2
bottom = text[1::2]
Q3
top = text[0::2] bottom = text[1::2] cipher = top + bottom
Q4
rail1 = text[0::4]
Q5
top = cipher[:(len(cipher) + 1) // 2]
Section E: Spot the Error
Q1
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].
Q2
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'.
Q3
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].
Q4
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.
Q5
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.