Add a function to decrypt a message with the gamma cipher.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -65,3 +65,14 @@
|
|||||||
(map vector array)
|
(map vector array)
|
||||||
(map (fn [[a g]] (add-mod a g module))))))
|
(map (fn [[a g]] (add-mod a g module))))))
|
||||||
|
|
||||||
|
(defn decrypt-message
|
||||||
|
"Decrypt a message using the gamma cipher. The function is case-insensitive. The key is an array of three elements [a b c]. The array contains integer in range [0, module)."
|
||||||
|
[key symbols message]
|
||||||
|
(let [module (count symbols)
|
||||||
|
table (ss/generate-sorted-substitution-table symbols)]
|
||||||
|
(->> message
|
||||||
|
(ss/decrypt-by-table (set/map-invert table))
|
||||||
|
(decrypt-array key module)
|
||||||
|
(ss/decrypt-by-table table)
|
||||||
|
(cs/join))))
|
||||||
|
|
||||||
|
|||||||
@@ -53,3 +53,10 @@
|
|||||||
[4 32 15] 33 [6 28 2 8 3 5 13 16 21 1] [3 14 1 4 18 19 23 8 27 17]
|
[4 32 15] 33 [6 28 2 8 3 5 13 16 21 1] [3 14 1 4 18 19 23 8 27 17]
|
||||||
[1 2 3 ] 3 [0 1 2 2 0 2 1 1 0] [0 2 1 1 0 1 2 2 0])))
|
[1 2 3 ] 3 [0 1 2 2 0 2 1 1 0] [0 2 1 1 0 1 2 2 0])))
|
||||||
|
|
||||||
|
(deftest decrypt-message-test
|
||||||
|
(testing "Checking that the message is decrypted as expected"
|
||||||
|
(are [key symbols message expected]
|
||||||
|
(= expected (decrypt-message key symbols message))
|
||||||
|
[4 32 15] "абвгґдеєжзиіїйклмнопрстуфхцчшщьюя" "ешвжгдймсб" "гкбґопужчн"
|
||||||
|
[1 2 3 ] "абв" "абввавбба" "авббабвва")))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user