Add the functions to encrypt and decrypt with the gamma cipher.
continuous-integration/drone/push Build is passing Details

dev 0.9.0
KKlochko 2 years ago
parent e2e49ec392
commit e80e3a6cf0

@ -30,3 +30,24 @@
(take (inc size)))
module))
(defn encrypt-array
"Encrypt an array 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 module array]
(let [size (count array)
gamma (->> (apply conj [key module size])
(apply generate-gamma))]
(->> gamma
(map vector array)
(map (fn [[a g]] (add-mod a g module))))))
(defn decrypt-array
"Decrypt an array 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 module array]
(let [size (count array)
gamma (->> (apply conj [key module size])
(apply generate-gamma))]
(->> gamma
(map -)
(map vector array)
(map (fn [[a g]] (add-mod a g module))))))

@ -32,3 +32,17 @@
4 32 15 33 10 [3 14 1 4 18 19 23 8 27 17]
1 2 3 3 9 [0 2 1 1 0 1 2 2 0])))
(deftest encrypt-array-test
(testing "Checking that the message is encrypted as expected: (msg+gamma)%module."
(are [key module array expected]
(= expected (encrypt-array key module array))
[4 32 15] 33 [3 14 1 4 18 19 23 8 27 17] [6 28 2 8 3 5 13 16 21 1]
[1 2 3 ] 3 [0 2 1 1 0 1 2 2 0] [0 1 2 2 0 2 1 1 0])))
(deftest decrypt-array-test
(testing "Checking that the message is decrypted as expected."
(are [key module array expected]
(= expected (decrypt-array key module array))
[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])))

Loading…
Cancel
Save