(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))