Sha256: 7c7b801110d0f2c9b764971b8c13d29a973c0b53914b6cf82fd87a345072ed99

Contents?: true

Size: 1.96 KB

Versions: 183

Compression:

Stored size: 1.96 KB

Contents

# Rail Fence Cipher

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.
## Source

Wikipedia [https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher](https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Version data entries

183 entries across 183 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.138 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.137 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.136 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.135 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.134 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.133 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.132 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.131 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.130 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.129 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.128 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.127 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.126 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.125 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.124 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.123 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.122 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.121 tracks/fsharp/exercises/rail-fence-cipher/README.md
trackler-2.2.1.120 tracks/fsharp/exercises/rail-fence-cipher/README.md