 # mzuther's solution

## to Rotational Cipher in the Clojure Track

Published at Jun 12 2020 · 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.`

## Submitting Incomplete Solutions

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

### rotational_cipher_test.clj

``````(ns rotational-cipher-test
(:require  [clojure.test :refer [deftest is testing]]
rotational-cipher))

(deftest rotational-cipher-test
(testing "rotate a by 1"
(is (= (rotational-cipher/rotate "a" 1) "b")))

(testing "rotate a by 26, same output as input"
(is (= (rotational-cipher/rotate "a" 26) "a")))

(testing "rotate a by 0, same output as input"
(is (= (rotational-cipher/rotate "a" 0) "a")))

(testing "rotate m by 13"
(is (= (rotational-cipher/rotate "m" 13) "z")))

(testing "rotate n by 13 with wrap around alphabet"
(is (= (rotational-cipher/rotate "n" 13) "a")))

(testing "rotate capital letters"
(is (= (rotational-cipher/rotate "OMG" 5) "TRL")))

(testing "rotate spaces"
(is (= (rotational-cipher/rotate "O M G" 5) "T R L")))

(testing "rotate numbers"
(is (= (rotational-cipher/rotate "Testing 1 2 3 testing" 4) "Xiwxmrk 1 2 3 xiwxmrk")))

(testing "rotate punctuation"
(is (= (rotational-cipher/rotate "Let's eat, Grandma!" 21) "Gzo'n zvo, Bmviyhv!")))

(testing "rotate in the opposite direction"
(is (= (rotational-cipher/rotate "b" -1) "a")))

(testing "rotate in the opposite direction past first letter"
(is (= (rotational-cipher/rotate "B" -2) "Z")))

(testing "rotate in the opposite direction past letter count"
(is (= (rotational-cipher/rotate "B" -28) "Z")))

(testing "rotate forward then backwards the same number of steps"
(is (=  (rotational-cipher/rotate
(rotational-cipher/rotate "B" 28) -28) "B")))

(testing "rotate all letters"
(is (= (rotational-cipher/rotate "The quick brown fox jumps over the lazy dog." 13) "Gur dhvpx oebja sbk whzcf bire gur ynml qbt."))))``````
``````(ns rotational-cipher
(:require [clojure.string :as str]))

(defn range-char [start end]
(map char (range (int start)
(inc (int end)))))

;; this extremely weak cipher was broken in the 9th century CE
;; -- at the latest -- and probably only worked for illiterate
;; opponents (https://en.wikipedia.org/wiki/Caesar_cipher)
(defn create-very-weak-cipher [n]
(let [n-normalized   (mod n 26)

alphabet-lower (range-char \a \z)
alphabet-upper (range-char \A \Z)

cipher-lower   (zipmap alphabet-lower
(drop n-normalized
(cycle alphabet-lower)))
cipher-upper   (zipmap alphabet-upper
(drop n-normalized
(cycle alphabet-upper)))]
(into cipher-lower cipher-upper)))

;; I know that this is only a coding exercise, but still --
;; leaving case alont and passing punctuation and digits
;; through is just as bad as not encrypting at all
(defn almost-plain-text [message n]
(let [pseudo-cipher (create-very-weak-cipher n)]
(->> message
(map #(get pseudo-cipher %1 %1))
(apply str))))

;; this is something that at least *resembles* an encrypted
;; message; it will keep your message secure unless someone
;; with a brain comes along
(defn slightly-better-pseudo-encryption [message n]
(let [pseudo-cipher (create-very-weak-cipher n)]
(->> message
(map #(get pseudo-cipher %1))
(remove nil?)
(map str/lower-case)
(apply str))))

(defn rotate [message n]
(almost-plain-text message n))``````