Rail Fence Cipher

Zig-zag transposition over N rails - visual rail display shows the diagonal pattern.

Last reviewed: April 2026

New to this tool? Click here for instructions

3
Plaintext
Ciphertext
Enter plaintext and adjust the number of rails.

How to Use the Rail Fence Cipher Tool

The Rail Fence Cipher Tool is a simple and effective way to encrypt and decrypt text using a zig-zag pattern. Here's how you can use it:

1. Choose Encrypt or Decrypt: Select the direction you want to perform. You can choose to encrypt or decrypt your text. Select Encrypt or Decrypt

2. Set the number of rails (2-10): Enter the number of rails you want to use. The number of rails is the key. With 2 rails, odd and even positions alternate. More rails create a longer zig-zag period. The maximum useful rails is half the plaintext length.

3. Enter your text: Type the text you want to encrypt or decrypt.

4. View the result: The tool will display the ciphertext or plaintext based on your selection.

5. Visualize the zig-zag pattern: See the visual representation of the zig-zag pattern used for encryption.

  • Select Encrypt or Decrypt
  • Set the number of rails (2-10)
  • Enter your text
  • View the result
  • Visualize the zig-zag pattern

When to Use the Rail Fence Cipher Tool

The Rail Fence Cipher Tool is ideal for educational purposes and as a component in more complex cipher systems. It's not suitable for actual security needs due to its minimal security. However, it can be a great way to understand the basics of transposition ciphers.

How It Works

The Rail Fence cipher works by writing the plaintext in a zig-zag pattern across a specified number of rails (rows), then reading off the rails one at a time to produce the ciphertext. For example, with 3 rails and the message 'HELLOWORLD', the zig-zag pattern would look like this:

1. Rail 1: H, O, L

2. Rail 2: E, L, W, R, D

3. Rail 3: L, O

Read each rail left to right and concatenate them. For 3 rails and 'HELLOWORLD': rail 1 = H, O, L; rail 2 = E, L, W, R, D; rail 3 = L, O. Ciphertext: HOLEL WRDLO.

Frequently Asked Questions

The Rail Fence cipher is a transposition cipher where plaintext is written diagonally in a zig-zag pattern across N rails (rows), then read off row by row. It gets its name from the visual resemblance to a fence made of rails.
The number of rails is the key. With 2 rails, odd and even positions alternate. More rails create a longer zig-zag period. The maximum useful rails is half the plaintext length.
Write the plaintext in a zig-zag across N rails, then read each rail left to right and concatenate them. For 3 rails and 'HELLOWORLD': rail 1 = H, O, L; rail 2 = E, L, W, R, D; rail 3 = L, O ? ciphertext: HOLEL WRDLO.
No. It's a pure transposition cipher - letter frequencies are preserved. An attacker can try all possible rail counts quickly. It's primarily used as an educational example, not for real security.
Rail Fence uses a fixed zig-zag pattern with only the number of rails as the key. Columnar transposition uses a keyword to determine column reading order, offering a much larger key space and greater security.

Quick reference

Rail Fence Cipher Quick Reference
Parameter Description Example Notes
Number of Rails Defines the zigzag pattern rows (minimum 2) Typical values: 3-5 Higher rails increase security
Direction Change Alternates between downward and upward movement Example: ↓ → ↑ → ↓ Key component of encryption
Plaintext Example "HELLO WORLD" Split into 3 rails Letters fill the grid diagonally
Ciphertext Example "HLOOLWRD" Result of zigzag reading Depends on rail count
Encryption Process Write message in zigzag pattern Write → read row by row Requires rail count
Decryption Process Reconstruct grid and read diagonally Fill rails → read in reverse pattern Requires rail count