Add a function to encrypt a message with the gamma cipher.
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-10-28 17:16:04 +03:00
parent 124133a407
commit bcb347a8d9
2 changed files with 22 additions and 1 deletions
@@ -1,5 +1,8 @@
(ns cipher-analytical-machine.ciphers.gamma
(:require [clojure.string :as cs])
(:require
[clojure.set :as set]
[clojure.string :as cs]
[cipher-analytical-machine.ciphers.simple-substitution :as ss])
(:gen-class))
(defn add-mod
@@ -40,6 +43,17 @@
(map vector array)
(map (fn [[a g]] (add-mod a g module))))))
(defn encrypt-message
"Encrypt 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))
(encrypt-array key module)
(ss/decrypt-by-table table)
(cs/join))))
(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]