From e80e3a6cf0a6f07331db4e528044eb389d9fddff Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 27 Oct 2023 21:40:47 +0300 Subject: [PATCH] Add the functions to encrypt and decrypt with the gamma cipher. --- .../ciphers/gamma.clj | 21 +++++++++++++++++++ .../ciphers/gamma_test.clj | 14 +++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/cipher_analytical_machine/ciphers/gamma.clj b/src/cipher_analytical_machine/ciphers/gamma.clj index 4222ff5..c6c0e76 100644 --- a/src/cipher_analytical_machine/ciphers/gamma.clj +++ b/src/cipher_analytical_machine/ciphers/gamma.clj @@ -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)))))) + diff --git a/test/cipher_analytical_machine/ciphers/gamma_test.clj b/test/cipher_analytical_machine/ciphers/gamma_test.clj index ee034f1..f12757c 100644 --- a/test/cipher_analytical_machine/ciphers/gamma_test.clj +++ b/test/cipher_analytical_machine/ciphers/gamma_test.clj @@ -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]))) +