# johnnylau's solution

## to Rail Fence Cipher in the Java Track

Published at Mar 16 2019 · 0 comments
Instructions
Test suite
Solution

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 . .
``````

``````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.

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

## Submitting Incomplete Solutions

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

### RailFenceCipherTest.java

``````import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

public class RailFenceCipherTest {

private RailFenceCipher railFenceCipher;

@Test
public void encodeWithTwoRails() {
railFenceCipher = new RailFenceCipher(2);
Assert.assertEquals("XXXXXXXXXOOOOOOOOO",
railFenceCipher.getEncryptedData("XOXOXOXOXOXOXOXOXO"));
}

@Ignore("Remove to run test")
@Test
public void encodeWithThreeRails() {
railFenceCipher = new RailFenceCipher(3);
Assert.assertEquals("WECRLTEERDSOEEFEAOCAIVDEN",
railFenceCipher.getEncryptedData("WEAREDISCOVEREDFLEEATONCE"));
}

@Ignore("Remove to run test")
@Test
public void encodeWithEndingInTheMiddle() {
railFenceCipher = new RailFenceCipher(4);
Assert.assertEquals("ESXIEECSR",
railFenceCipher.getEncryptedData("EXERCISES"));
}

@Ignore("Remove to run test")
@Test
public void decodeWithThreeRails() {
railFenceCipher = new RailFenceCipher(3);
Assert.assertEquals("THEDEVILISINTHEDETAILS",
railFenceCipher.getDecryptedData("TEITELHDVLSNHDTISEIIEA"));
}

@Ignore("Remove to run test")
@Test
public void decodeWithFiveRails() {
railFenceCipher = new RailFenceCipher(5);
Assert.assertEquals("EXERCISMISAWESOME",
railFenceCipher.getDecryptedData("EIEXMSMESAORIWSCE"));
}

@Ignore("Remove to run test")
@Test
public void decodeWithSixRails() {
railFenceCipher = new RailFenceCipher(6);
Assert.assertEquals("112358132134558914423337761098715972584418167651094617711286",
railFenceCipher.getDecryptedData("133714114238148966225439541018335470986172518171757571896261"));
}
}``````
``````/**
* RailFenceCipher
*/
public class RailFenceCipher {
int colCount;
char[][] layout;

RailFenceCipher(int i) {
colCount = i;
}

String getEncryptedData(String str) {
layout = new char[colCount][str.length()];
boolean reverseFlag = true;
char[] ch = str.toCharArray();
for (int i = 0, x = 0, j = 0; x < ch.length; i = (reverseFlag) ? ++i : --i, j++, x++) {
if (i == colCount - 1) {
reverseFlag = false;
} else if (i == 0) {
reverseFlag = true;
}
layout[i][j] = ch[x];
}

String s = "";
for (int i = 0; i < colCount; i++) {
for (char var : layout[i]) {
if (var != '\u0000') {
s += Character.toString(var);
}
}
}
return s;
}

String getDecryptedData(String str) {
layout = new char[colCount][str.length()];
char[] ch = str.toCharArray();
boolean reverseFlag = false;
for (int i = 0, x = 0, j = 0; x < ch.length; i = (reverseFlag) ? ++i : --i, j++, x++) {
if (i == colCount - 1) {
reverseFlag = false;
} else if (i == 0) {
reverseFlag = true;
}
layout[i][j] = ' ';
}

outer: for (char var : ch) {
for (int i = 0; i < colCount; i++) {
for (int j = 0; j < layout[0].length; j++) {
if (layout[i][j] == ' ') {
layout[i][j] = var;
continue outer;
}
}
}
}

String s = "";
reverseFlag = false;
for (int i = 0, x = 0, j = 0; x < ch.length; i = (reverseFlag) ? ++i : --i, j++, x++) {
if (i == colCount - 1) {
reverseFlag = false;
} else if (i == 0) {
reverseFlag = true;
}
s += Character.toString(layout[i][j]);
}

return s;

}

}``````