|
|
|
@ -14,21 +14,28 @@
|
|
|
|
|
(+ key)
|
|
|
|
|
(mod max-index)))
|
|
|
|
|
|
|
|
|
|
(defn encrypt-or-skip
|
|
|
|
|
"If symbols do not have a character, then it will be skipped."
|
|
|
|
|
[index character key symbols max-index]
|
|
|
|
|
(if (nil? index) character
|
|
|
|
|
(get symbols
|
|
|
|
|
(encrypt-index index key max-index))))
|
|
|
|
|
|
|
|
|
|
(defn encrypt-char
|
|
|
|
|
"Calculates new index of a character. Uses a char from symbols, the key and max-index (exclusive)."
|
|
|
|
|
[char key symbols max-index]
|
|
|
|
|
(get symbols
|
|
|
|
|
(-> char
|
|
|
|
|
(calculate-char-index symbols)
|
|
|
|
|
(encrypt-index key max-index))))
|
|
|
|
|
(-> char
|
|
|
|
|
(calculate-char-index symbols)
|
|
|
|
|
(encrypt-or-skip char key symbols max-index)))
|
|
|
|
|
|
|
|
|
|
(defn encrypt-message
|
|
|
|
|
"Encrypt a message using the Caesar cipher."
|
|
|
|
|
"Encrypt a message using the Caesar cipher. The function is case-insensitive."
|
|
|
|
|
[message key symbols]
|
|
|
|
|
(let [max-index (count symbols)]
|
|
|
|
|
(cs/join
|
|
|
|
|
(map (fn [char] (encrypt-char char key symbols max-index))
|
|
|
|
|
message))))
|
|
|
|
|
(->> message
|
|
|
|
|
(cs/lower-case)
|
|
|
|
|
(map (fn [char] (encrypt-char char key symbols max-index)))
|
|
|
|
|
(cs/join))))
|
|
|
|
|
|
|
|
|
|
(defn decrypt-message
|
|
|
|
|
"Decrypt the ciphtext using the Caesar cipher."
|
|
|
|
|