90 lines
2.6 KiB
Markdown
90 lines
2.6 KiB
Markdown
|
# 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:
|
||
|
|
||
|
```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.
|
||
|
|
||
|
* * * *
|
||
|
|
||
|
For installation and learning resources, refer to the
|
||
|
[Ruby resources page](http://exercism.io/languages/ruby/resources).
|
||
|
|
||
|
For running the tests provided, you will need the Minitest gem. Open a
|
||
|
terminal window and run the following command to install minitest:
|
||
|
|
||
|
gem install minitest
|
||
|
|
||
|
If you would like color output, you can `require 'minitest/pride'` in
|
||
|
the test file, or note the alternative instruction, below, for running
|
||
|
the test file.
|
||
|
|
||
|
Run the tests from the exercise directory using the following command:
|
||
|
|
||
|
ruby rail_fence_cipher_test.rb
|
||
|
|
||
|
To include color from the command line:
|
||
|
|
||
|
ruby -r minitest/pride rail_fence_cipher_test.rb
|
||
|
|
||
|
|
||
|
## 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.
|