Compare commits

...
4 Commits
Author SHA1 Message Date
KKlochko 79b72fccca Fix the key parsing in the gamma actions. 2023-10-31 16:49:30 +02:00
KKlochko 5843695a40 Add the action of decryption for the gamma cipher. 2023-10-31 16:48:40 +02:00
KKlochko 4f89fede56 Add the action of encryption for the gamma cipher.
continuous-integration/drone/push Build is passing
2023-10-30 20:15:40 +02:00
KKlochko 53f89d4b4f Add a function to decrypt a message with the gamma cipher.
continuous-integration/drone/push Build is passing
2023-10-29 19:05:12 +02:00
3 changed files with 38 additions and 1 deletions
@@ -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))))
@@ -1,7 +1,9 @@
(ns cipher-analytical-machine.cli.actions.cryptography (ns cipher-analytical-machine.cli.actions.cryptography
(:require (:require
[clojure.string :as cs]
[cipher-analytical-machine.ciphers.caesar :as caesar] [cipher-analytical-machine.ciphers.caesar :as caesar]
[cipher-analytical-machine.ciphers.simple-substitution :as ss] [cipher-analytical-machine.ciphers.simple-substitution :as ss]
[cipher-analytical-machine.ciphers.gamma :as gamma]
[cipher-analytical-machine.parsers.parsers :as ps] [cipher-analytical-machine.parsers.parsers :as ps]
[cipher-analytical-machine.parsers.simple-substitution :as pss]) [cipher-analytical-machine.parsers.simple-substitution :as pss])
(:gen-class)) (:gen-class))
@@ -38,6 +40,20 @@
substitution-table (get data "table")] substitution-table (get data "table")]
(ss/decrypt-message-with-caesar message key substitution-table symbols))))) (ss/decrypt-message-with-caesar message key substitution-table symbols)))))
(defn gamma-actions
[options arguments action-type]
(let [message (:message options)
key (->> (cs/split (:key options) #",")
(map #(Integer/parseInt %))
(into []))
symbols (:symbols options)]
(cond
(= action-type :encrypt)
(gamma/encrypt-message key symbols message)
(= action-type :decrypt)
(gamma/decrypt-message key symbols message))))
(defn cryptography-actions (defn cryptography-actions
[options arguments action-type] [options arguments action-type]
(let [cipher (:cipher options)] (let [cipher (:cipher options)]
@@ -46,5 +62,8 @@
(caesar-actions options arguments action-type) (caesar-actions options arguments action-type)
(= cipher "Simple substitution and Caesar") (= cipher "Simple substitution and Caesar")
(simple-substitution-with-caesar-actions options arguments action-type)))) (simple-substitution-with-caesar-actions options arguments action-type)
(= cipher "Gamma")
(gamma-actions options arguments action-type))))
@@ -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 ] "абв" "абввавбба" "авббабвва")))