|
| 1 | +# Rail Fence Cipher |
| 2 | + |
| 3 | +Implement encoding and decoding for the rail fence cipher. |
| 4 | + |
| 5 | +The Rail Fence cipher is a form of transposition cipher that gets its name from |
| 6 | +the way in which it's encoded. It was already used by the ancient Greeks. |
| 7 | + |
| 8 | +In the Rail Fence cipher, the message is written downwards on successive "rails" |
| 9 | +of an imaginary fence, then moving up when we get to the bottom (like a zig-zag). |
| 10 | +Finally the message is then read off in rows. |
| 11 | + |
| 12 | +For example, using three "rails" and the message "WE ARE DISCOVERED FLEE AT ONCE", |
| 13 | +the cipherer writes out: |
| 14 | + |
| 15 | +```text |
| 16 | +W . . . E . . . C . . . R . . . L . . . T . . . E |
| 17 | +. E . R . D . S . O . E . E . F . E . A . O . C . |
| 18 | +. . A . . . I . . . V . . . D . . . E . . . N . . |
| 19 | +``` |
| 20 | + |
| 21 | +Then reads off: |
| 22 | + |
| 23 | +```text |
| 24 | +WECRLTEERDSOEEFEAOCAIVDEN |
| 25 | +``` |
| 26 | + |
| 27 | +To decrypt a message you take the zig-zag shape and fill the ciphertext along the rows. |
| 28 | + |
| 29 | +```text |
| 30 | +? . . . ? . . . ? . . . ? . . . ? . . . ? . . . ? |
| 31 | +. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . |
| 32 | +. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . . |
| 33 | +``` |
| 34 | + |
| 35 | +The first row has seven spots that can be filled with "WECRLTE". |
| 36 | + |
| 37 | +```text |
| 38 | +W . . . E . . . C . . . R . . . L . . . T . . . E |
| 39 | +. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . |
| 40 | +. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . . |
| 41 | +``` |
| 42 | + |
| 43 | +Now the 2nd row takes "ERDSOEEFEAOC". |
| 44 | + |
| 45 | +```text |
| 46 | +W . . . E . . . C . . . R . . . L . . . T . . . E |
| 47 | +. E . R . D . S . O . E . E . F . E . A . O . C . |
| 48 | +. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . . |
| 49 | +``` |
| 50 | + |
| 51 | +Leaving "AIVDEN" for the last row. |
| 52 | + |
| 53 | +```text |
| 54 | +W . . . E . . . C . . . R . . . L . . . T . . . E |
| 55 | +. E . R . D . S . O . E . E . F . E . A . O . C . |
| 56 | +. . A . . . I . . . V . . . D . . . E . . . N . . |
| 57 | +``` |
| 58 | + |
| 59 | +If you now read along the zig-zag shape you can read the original message. |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +To run the tests: |
| 64 | + |
| 65 | +```sh |
| 66 | +$ gradle test |
| 67 | +``` |
| 68 | + |
| 69 | +For more detailed info about the Java track see the [help page](http://exercism.io/languages/java). |
| 70 | + |
| 71 | + |
| 72 | +## Source |
| 73 | + |
| 74 | +[Wikipedia](https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher) |
| 75 | + |
| 76 | +## Submitting Incomplete Solutions |
| 77 | +It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
| 78 | +i |
0 commit comments