# rootulp's solution

## to Rotational Cipher in the Java Track

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

Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.

The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`. Using a key of `0` or `26` will always yield the same output due to modular arithmetic. The letter is shifted for as many values as the value of the key.

The general notation for rotational ciphers is `ROT + <key>`. The most commonly used rotational cipher is `ROT13`.

A `ROT13` on the Latin alphabet would be as follows:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
``````

It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.

Ciphertext is written out in the same formatting as the input including spaces and punctuation.

## Examples

• ROT5 `omg` gives `trl`
• ROT0 `c` gives `c`
• ROT26 `Cool` gives `Cool`
• ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
• ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`

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

### RotationalCipherTest.java

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

public class RotationalCipherTest {

private RotationalCipher rotationalCipher;

@Test
public void rotateSingleCharacterBy0() {
rotationalCipher = new RotationalCipher(0);
Assert.assertEquals("a", rotationalCipher.rotate("a"));
}

@Ignore("Remove to run test")
@Test
public void rotateSingleCharacterBy1() {
rotationalCipher = new RotationalCipher(1);
Assert.assertEquals("b", rotationalCipher.rotate("a"));
}

@Ignore("Remove to run test")
@Test
public void rotateSingleCharacterBy26() {
rotationalCipher = new RotationalCipher(26);
Assert.assertEquals("a", rotationalCipher.rotate("a"));
}

@Ignore("Remove to run test")
@Test
public void rotateSingleCharacterBy13() {
rotationalCipher = new RotationalCipher(13);
Assert.assertEquals("z", rotationalCipher.rotate("m"));
}

@Ignore("Remove to run test")
@Test
public void rotateSingleCharacterWithWrapAround() {
rotationalCipher = new RotationalCipher(13);
Assert.assertEquals("a", rotationalCipher.rotate("n"));
}

@Ignore("Remove to run test")
@Test
public void rotateCapitalLetters() {
rotationalCipher = new RotationalCipher(5);
Assert.assertEquals("TRL", rotationalCipher.rotate("OMG"));
}

@Ignore("Remove to run test")
@Test
public void rotateSpaces() {
rotationalCipher = new RotationalCipher(5);
Assert.assertEquals("T R L", rotationalCipher.rotate("O M G"));
}

@Ignore("Remove to run test")
@Test
public void rotateNumbers() {
rotationalCipher = new RotationalCipher(4);
Assert.assertEquals("Xiwxmrk 1 2 3 xiwxmrk", rotationalCipher.rotate("Testing 1 2 3 testing"));
}

@Ignore("Remove to run test")
@Test
public void rotatePunctuation() {
rotationalCipher = new RotationalCipher(21);
Assert.assertEquals("Gzo'n zvo, Bmviyhv!", rotationalCipher.rotate("Let's eat, Grandma!"));
}

@Ignore("Remove to run test")
@Test
public void rotateAllLetters() {
rotationalCipher = new RotationalCipher(13);
Assert.assertEquals("The quick brown fox jumps over the lazy dog.",
rotationalCipher.rotate("Gur dhvpx oebja sbk whzcf bire gur ynml qbt."));
}
}``````
``````import java.util.HashMap;
import java.util.Map;

public class RotationalCipher {

private int shift;
private Map<Character, Character> cipher;

private static int ALPHABET_LENGTH = 26;
private static char[] LOWERCASE_ALPHABET = "abcdefghijklmnopqrstuvwxyz".toCharArray();
private static char[] UPPERCASE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

public RotationalCipher(int shift) {
this.shift = shift;
this.cipher = generateCipher();
}

public String rotate(String text) {
StringBuilder encrypted = new StringBuilder(text.length());

for(char c: text.toCharArray()) {
encrypted.append(encryptChar(c));
}

return encrypted.toString();
}

private Map<Character, Character> generateCipher() {
char[] lowercaseRotatedAlphabet = rotateAlphabet(LOWERCASE_ALPHABET);
char[] uppercaseRotatedAlphabet = rotateAlphabet(UPPERCASE_ALPHABET);

Map<Character, Character> cipher = new HashMap<>();

for (int i = 0; i < ALPHABET_LENGTH; i++) {
cipher.put(lowercaseRotatedAlphabet[i], LOWERCASE_ALPHABET[i]);
cipher.put(uppercaseRotatedAlphabet[i], UPPERCASE_ALPHABET[i]);
}

return cipher;
}

private char[] rotateAlphabet(char[] alphabet) {
char[] rotated = new char[alphabet.length];

for (int i = 0; i < alphabet.length; i++) {
rotated[(i + shift) % alphabet.length] = alphabet[i];
}

return rotated;
}

private Character encryptChar(Character c) {
return cipher.containsKey(c) ? this.cipher.get(c) : c;
}

}``````