You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.1 KiB
35 lines
1.1 KiB
(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))
|
|
|