← Back to Writeups
CUSTOMMediumCrypto

Strange — Rail Fence Cipher

XESXOR9/2/20261 min read
#crypto#custom#medium#rail_fence_decryption

Strange — Rail Fence Cipher

Platform: CUSTOM | Category: Crypto | Type: Challenge | Difficulty: Medium | OS: N/A | **Author: XESXOR Techniques: rail_fence_decryption

Summary

Walkthrough of the CUSTOM Crypto Strange — Rail Fence Cipher. Task: Decrypt a scrambled text file with mixed Russian/Latin characters. Solution: Rail fence cipher decryption with 4 rails reveals the flag.

Description

"Что это за набор букв?" (What is this set of letters?) with mixed Russian/Latin text file.

Analysis

Rail fence cipher — text is written in a zigzag pattern across N rails, then read row by row.

Solution

python
#!/usr/bin/env python3
def rail_fence_decrypt(ciphertext, rails):
    n = len(ciphertext)
    # Create zigzag pattern
    pattern = []
    rail = 0
    direction = 1
    for i in range(n):
        pattern.append(rail)
        rail += direction
        if rail == 0 or rail == rails - 1:
            direction *= -1
    
    # Count characters on each rail
    counts = [pattern.count(r) for r in range(rails)]
    
    # Distribute characters across rails
    rail_chars = []
    idx = 0
    for r in range(rails):
        rail_chars.append(list(ciphertext[idx:idx + counts[r]]))
        idx += counts[r]
    
    # Read according to pattern
    result = []
    indices = [0] * rails
    for r in pattern:
        result.append(rail_chars[r][indices[r]])
        indices[r] += 1
    
    return ''.join(result)
‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍

# Try different number of rails
ciphertext = "..."  # encrypted text from file

for rails in range(2, 10):
    decrypted = rail_fence_decrypt(ciphertext, rails)
    if 'spbctf' in decrypted.lower() or 'flag' in decrypted.lower():
        print(f"Rails={rails}: {decrypted}")

# With 4 rails we get:
# "SPBCTF Лучшее сообщество... flag: spbctf{REDACTED}"
# vzlom = взлом (hack), relsa = рельса (rail)

‍​‌‌​​‌​​​‌‌​​‌​​​​‌‌​‌​​​​‌‌‌​​‌​​‌‌​​​‌​​‌‌‌​​​​​‌‌​‌‌​​​‌‌​​​​‍

Key Takeaways / Lessons

  • Mastering rail_fence_decryption was key to this challenge.
  • Always check for common misconfigurations and verify assumptions.

Crafted by XESXOR Research • 2026-03-08 • CUSTOM • Strange — Rail Fence Cipher • Difficulty: Medium