From 53f89d4b4f70d538121fc7365d7505443823b870 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 29 Oct 2023 19:05:12 +0200 Subject: [PATCH] Add a function to decrypt a message with the gamma cipher. --- src/cipher_analytical_machine/ciphers/gamma.clj | 11 +++++++++++ test/cipher_analytical_machine/ciphers/gamma_test.clj | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/src/cipher_analytical_machine/ciphers/gamma.clj b/src/cipher_analytical_machine/ciphers/gamma.clj index 1e5c422..53cf42c 100644 --- a/src/cipher_analytical_machine/ciphers/gamma.clj +++ b/src/cipher_analytical_machine/ciphers/gamma.clj @@ -65,3 +65,14 @@ (map vector array) (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)))) + diff --git a/test/cipher_analytical_machine/ciphers/gamma_test.clj b/test/cipher_analytical_machine/ciphers/gamma_test.clj index e9975bc..0aaa809 100644 --- a/test/cipher_analytical_machine/ciphers/gamma_test.clj +++ b/test/cipher_analytical_machine/ciphers/gamma_test.clj @@ -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] [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 ] "абв" "абввавбба" "авббабвва"))) +