Rail
Fence
CTF's Author: Will Hong
Description
A type of transposition cipher is the rail fence cipher,
which is described here.
Here is one such cipher encrypted using the rail fence with 4 rails. Can you
decrypt it?Download the message here.Put
the decoded message in the picoCTF flag format, picoCTF{decoded_message}.
Hint is-Once you've understood how the cipher works, it's
best to draw it out yourself on paper.
The challenge gives us a type of transposition cipher
called a rail fence cipher. The description reads:
"A type of transposition cipher is the rail fence
cipher, which is described here.
Here is one such cipher encrypted using the rail fence with 4 rails. Can you
decrypt it?"
We will download the file and get the encrypted text.
The Rail Fence Cipher is a form of transposition
cipher. Instead of replacing characters with others (like in substitution
ciphers), it reorders the characters in a zig-zag pattern across several
"rails" (rows), then concatenates the letters row-by-row to create
the ciphertext.
To decrypt a rail fence cipher:
- Determine
the zig-zag path for 4 rails.
- Mark
positions where each character will go.
- Place
characters from the cipher into the zig-zag positions.
- Read
off the characters in zig-zag order to reconstruct the plaintext.
We’ll write a Python script to automate this decryption.
def rail_fence_decrypt(ciphertext, num_rails):
# Create the rail
matrix
rail = [['\n' for
_ in range(len(ciphertext))] for _ in range(num_rails)]
# Step 1: Mark
zig-zag path
dir_down = None
row, col = 0, 0
for _ in
range(len(ciphertext)):
if row == 0:
dir_down =
True
elif row ==
num_rails - 1:
dir_down =
False
rail[row][col]
= '*'
col += 1
row += 1 if
dir_down else -1
# Step 2: Fill the
rails with the ciphertext
index = 0
for i in
range(num_rails):
for j in
range(len(ciphertext)):
if
rail[i][j] == '*' and index < len(ciphertext):
rail[i][j] = ciphertext[index]
index
+= 1
# Step 3: Read the
message in zig-zag order
result = []
row, col = 0, 0
for _ in
range(len(ciphertext)):
if row == 0:
dir_down =
True
elif row ==
num_rails - 1:
dir_down =
False
if
rail[row][col] != '\n':
result.append(rail[row][col])
col += 1
row += 1 if
dir_down else -1
return
''.join(result)
Flag is -picoCTF{WH3R3_D035_7H3_F3NC3_8361N_4ND_3ND_D00AFDD3}


No comments:
Post a Comment