Text and cipher guides

Decode a Rail Fence Cipher With Three Rails

·

A rail fence exercise gives you a message with its units out of order. Recover it by using the original rail count and the same rules for what occupies each position.

Rail Fence Cipher

Check the three rails of a classical message

Select Load example. The source is WEAREDISCOVEREDFLEEATONCE. With Rail count set to 3 and Ciphertext format set to Plain text, select Encode rail fence. The output reads WECRLTEERDSOEEFEAOCAIVDEN.

Read the rails top to bottom. The top rail contains WECRLTE, the middle rail ERDSOEEFEAOC and the bottom rail AIVDEN. The pattern starts at the top and heads down before returning up.

Uttarakhand Open University notes, rail fence pattern and worked example

Decode with the original count and spacing

Choose Use result as input and Decode rail fence. The original text returns. Change Rail count to 4 and decode the same ciphertext again to see a different order. The tool uses the count you give it.

This workspace moves punctuation, spaces and line endings as units. A textbook might remove spaces and punctuation before encoding or add grouping spaces after encoding. Remove those added groups when decoding that exercise.

Uttarakhand Open University notes, rail fence pattern and worked exampleUnicode Text Segmentation, grapheme cluster boundaries

Use a JSON array when new neighbors form a flag

With two rails, try encoding A🇦B🇧 as Plain text. The two standalone regional indicators would become adjacent and form a single flag in the joined ciphertext. The tool reports a grapheme boundary change and offers no plain download.

Choose JSON grapheme array and encode again. The four entries are A, B, 🇦 and 🇧 in that order. Download and reopen the array using Decode, two rails and the same format. The output is A🇦B🇧. Keep the array and rail count together to reproduce the original.

Unicode Text Segmentation, grapheme cluster boundariesRFC 8259, JSON arrays and strings

Common questions

Does the JSON file record the rail count?

The JSON file stores the reordered units as an array of strings. Keep the rail count with it so the decoder can reverse the same zigzag.

Why does a short message sometimes produce the same text?

When the source has no more units than the rail count, the zigzag reaches each rail at most once and reading the rails leaves the order unchanged.

Sources

Put it into practice.

Related guides