Sha256: cb15886f2b7131b5a7e08ab5042899ba0481403f8efae246dd116676c9504d37

Contents?: true

Size: 1.64 KB

Versions: 83

Compression:

Stored size: 1.64 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:
```
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:
```
WECRLTEERDSOEEFEAOCAIVDEN
```


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

The first row has seven spots that can be filled with "WECRLTE".
```
W . . . E . . . C . . . R . . . L . . . T . . . E
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
```

Now the 2nd row takes "ERDSOEEFEAOC".
```
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.
```
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

83 entries across 83 versions & 1 rubygems

Version Path
trackler-2.1.0.42 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.41 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.40 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.39 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.38 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.37 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.36 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.34 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.33 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.32 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.31 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.30 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.29 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.28 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.27 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.26 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.25 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.24 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.23 common/exercises/rail-fence-cipher/description.md
trackler-2.1.0.22 common/exercises/rail-fence-cipher/description.md