Sha256: 5fe9e9c34028bc74626a4b779157bc061519967674acd21e76fc60dc505a0e6c

Contents?: true

Size: 1.67 KB

Versions: 139

Compression:

Stored size: 1.67 KB

Contents

Implement encoding and decoding for the rail fence cipher.

The Rail Fence cipher is a form of transposition cipher that gets its name from
the way in which it's encoded. It was already used by the ancient Greeks.

In the Rail Fence cipher, the message is written downwards on successive "rails"
of an imaginary fence, then moving up when we get to the bottom (like a zig-zag).
Finally the message is then read off in rows.

For example, using three "rails" and the message "WE ARE DISCOVERED FLEE AT ONCE",
the cipherer writes out:

```text
W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .
```

Then reads off:

```text
WECRLTEERDSOEEFEAOCAIVDEN
```

To decrypt a message you take the zig-zag shape and fill the ciphertext along the rows.

```text
? . . . ? . . . ? . . . ? . . . ? . . . ? . . . ?
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
```

The first row has seven spots that can be filled with "WECRLTE".

```text
W . . . E . . . C . . . R . . . L . . . T . . . E
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
```

Now the 2nd row takes "ERDSOEEFEAOC".

```text
W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
```

Leaving "AIVDEN" for the last row.

```text
W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .
```

If you now read along the zig-zag shape you can read the original message.

Version data entries

139 entries across 139 versions & 1 rubygems

Version Path
trackler-2.2.1.180 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.179 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.178 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.177 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.176 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.175 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.174 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.173 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.172 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.171 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.170 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.169 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.167 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.166 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.165 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.164 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.163 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.162 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.161 problem-specifications/exercises/rail-fence-cipher/description.md
trackler-2.2.1.160 problem-specifications/exercises/rail-fence-cipher/description.md