warnerp's solution

to Rail Fence Cipher in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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"));
}
}``````
``````import java.util.ArrayList;

public class RailFenceCipher {

public static final int ACTION_INIT_EMPTY = 0;
public static final int ACTION_INIT_TEXT = 1;
public static final int ACTION_POPULATE = 2;
public static final int ACTION_ENCRYPT = 3;
public static final int ACTION_DECRYPT = 4;
private int railCount;
private char[][] railFence;

RailFenceCipher(int railCount)
{
this.railCount = railCount;
}

public String getDecryptedData(String text)
{
setup(text, RailFenceCipher.ACTION_INIT_EMPTY);

traverseRows(text, RailFenceCipher.ACTION_POPULATE);

return traverseZigZag(text, RailFenceCipher.ACTION_DECRYPT);
}

public String getEncryptedData(String text)
{
setup(text, RailFenceCipher.ACTION_INIT_TEXT);

return traverseRows(text, RailFenceCipher.ACTION_ENCRYPT);
}

private void setup(String text, int action)
{
railFence = new char[railCount][text.length()];
traverseZigZag(text, action);
}

private String traverseZigZag(String text, int action)
{
int railIndex = 0;
int delta = 1;
StringBuilder decryptedText = new StringBuilder();

for (int charIndex = 0; charIndex < text.length(); charIndex++) {
if (action == RailFenceCipher.ACTION_INIT_EMPTY) railFence[railIndex][charIndex] = '?';
else if (action == RailFenceCipher.ACTION_INIT_TEXT) railFence[railIndex][charIndex] = text.charAt(charIndex);
else if (action == RailFenceCipher.ACTION_DECRYPT) decryptedText.append(railFence[railIndex][charIndex]);

if (railIndex == 0) delta = 1;
else if (railIndex == (railCount - 1)) delta = -1;
railIndex += delta;
}

return decryptedText.toString();
}

private String traverseRows(String text, int action)
{
int textCharIndex = 0;
StringBuilder encryptedText = new StringBuilder();

for (int railIndex = 0; railIndex < railCount; railIndex++) {
for (int railCharIndex = 0; railCharIndex < text.length(); railCharIndex++) {
if (railFence[railIndex][railCharIndex] != 0) {
if (action == RailFenceCipher.ACTION_POPULATE) railFence[railIndex][railCharIndex] = text.charAt(textCharIndex);
else if (action == RailFenceCipher.ACTION_ENCRYPT) encryptedText.append(railFence[railIndex][railCharIndex]);
textCharIndex++;
}
}
}

return encryptedText.toString();
}
}``````