rail-fence picoCTF 2022 Solution

Published: July 20, 2023

Description

A message was encrypted with a rail fence cipher using 4 rails. Decode it and wrap the plaintext in picoCTF{...}.

Read the ciphertext from message.txt.

In CyberChef, load Rail Fence Cipher Decode with Key = 4 (the rail count) and Offset = 0. Paste the text and read the output.

bash
less message.txt
bash
# CyberChef: Rail Fence Cipher Decode → Key 4, Offset 0
  1. Step 1Apply Rail Fence decoding
    The challenge states 4 rails. In CyberChef, the Rail Fence Cipher Decode operation takes Key = rail count (4 here) and Offset = 0 unless the challenge specifies otherwise.

    A worked example on the 5-character plaintext "WECRE" with 3 rails illustrates the pattern. Writing it across the encryption fence looks like:

    rail 0:  W . . . E
    rail 1:  . E . R .
    rail 2:  . . C . .
    
    read row by row -> "WE" + "ER" + "C" = "WEERC"

    To decode "WEERC", count how many characters land on each rail (2, 2, 1), slice the ciphertext into those chunks ("WE", "ER", "C"), then read down the diagonals: position 0 is rail 0 → W, position 1 is rail 1 → E, position 2 is rail 2 → C, position 3 is rail 1 → R, position 4 is rail 0 → E. Output: "WECRE" (the original plaintext, recovered). The full picoCTF flag is the same operation with Key = 4 across the entire ciphertext.

    For more on classical ciphers and how to spot which encoding is in play, see the CTF Encodings guide.

    Learn more

    The Rail Fence cipher is a classical transposition cipher - it rearranges the letters of the plaintext rather than replacing them. The message is written diagonally down and up across a set number of "rails" (rows), then read off row by row. For example, with 3 rails, "WEAREDISCOVERED" becomes three rows read in a zigzag pattern.

    Unlike substitution ciphers, every letter in a transposition cipher is present in the ciphertext - just in a different position. This means letter frequency analysis won't help; you need to know (or guess) the structure. With only a small key space (number of rails is usually 2-10), brute force is trivial.

    CyberChef is a browser-based Swiss army knife for encoding, decoding, and transforming data. It supports dozens of classical ciphers, encoding schemes, and data format conversions - all without installing anything. It's an essential tool for CTF challenges and real-world forensics work.

    To decode manually without a tool: divide the ciphertext into four rows based on the zigzag pattern, then read the characters diagonally. For a 4-rail fence, the top and bottom rails contain the fewest characters (they only touch the zigzag at peaks and valleys), while the middle rails contain more. The exact character count per rail depends on the message length - computing this by hand for short messages is feasible, which is why understanding the algorithm is valuable even when you have CyberChef available.

    Classical ciphers like Rail Fence, Caesar, Vigenere, and Playfair are frequently tested in CTF competitions because they require pattern recognition and algorithmic thinking rather than brute computation. Recognizing which cipher was used from ciphertext characteristics (letter frequency preserved but positions scrambled for transposition; frequency distribution shifted for substitution; unusual character sets for encoding schemes) is a key skill for the "crypto" category in CTFs.

  2. Step 2Wrap the flag
    The decoded sentence is already in the picoCTF format; copy it as-is.
    Learn more

    Once a transposition cipher is reversed, the plaintext is fully recovered with no ambiguity - every letter returns to its original position. This is in contrast to substitution ciphers, where frequency analysis gives probabilistic guesses that may need manual correction.

    The Rail Fence cipher was historically used during the American Civil War as a simple field cipher. While trivially broken today (even by hand with the right number of rails), it illustrates the fundamental principle that rearranging data is not the same as hiding it. Modern transposition techniques form one component of block cipher modes like CBC, but are always combined with substitution for security.

    In modern symmetric encryption, the AES block cipher combines both substitution (the SubBytes step applies a non-linear S-box to each byte) and permutation/transposition (the ShiftRows and MixColumns steps move bytes between positions). This combination - known as a substitution-permutation network - is what makes AES resistant to both frequency analysis and positional attacks. Neither substitution nor transposition alone is secure; the power comes from iterating both together across multiple rounds.

    If you encounter a ciphertext in a CTF and are unsure which classical cipher was used, the Index of Coincidence (IC) can help distinguish transposition from substitution. Transposition ciphers preserve the original letter frequency distribution (IC matches plaintext language), while monoalphabetic substitution ciphers also preserve frequencies but shift them. Polyalphabetic ciphers like Vigenere produce a lower, flatter IC. These statistical tests guide cipher identification before you attempt decryption.

Flag

picoCTF{WH3R3_D035_7H3_F3NC3_8361N_4ND_3ND_4A76...}

Rail fence is a simple transposition cipher; once you know the number of rails, decoding is straightforward.

Want more picoCTF 2022 writeups?

Tools used in this challenge

What to try next