From a7ccad8e1eb5a15ca27a6bb5344ec4487cf63b19 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 15 Sep 2023 21:30:37 +0300 Subject: [PATCH] Add the caesar-analyzers to get the plaintext from a ciphertext. --- .../caesar_analyzers.clj | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/cipher_analytical_machine/caesar_analyzers.clj diff --git a/src/cipher_analytical_machine/caesar_analyzers.clj b/src/cipher_analytical_machine/caesar_analyzers.clj new file mode 100644 index 0000000..7b6d572 --- /dev/null +++ b/src/cipher_analytical_machine/caesar_analyzers.clj @@ -0,0 +1,34 @@ +(ns cipher-analytical-machine.caesar-analyzers + (:require [cipher-analytical-machine.caesar :as caesar] + [cipher-analytical-machine.cipher_analyzers :as ca]) + (:gen-class)) + +(defn get-all-texts + "Return a list of pairs which have a key and a second posible plaintext." + [ciphertext symbols] + (let [keys (range (count symbols))] + (reduce + (fn [acc key] + (conj acc [key (caesar/decrypt-message ciphertext key symbols)])) + '() keys))) + +(defn get-key + "To find the key with frequencies of letters." + [ciphertext symbols letter-frequences] + (->> (get-all-texts ciphertext symbols) + (map (fn [[key text]] + [key (ca/chi-squared-statistic text letter-frequences)])) + (reduce + (fn [[key value] [new-key new-value]] + (if (> value new-value) [new-key new-value] + [key value])) + [0 Double/MAX_VALUE]) + (first))) + +(defn get-plaintext + "Return the plaintext from a ciphertext." + [ciphertext symbols letter-frequences] + (caesar/decrypt-message ciphertext + (get-key ciphertext symbols letter-frequences) + symbols)) +