Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e70b72f0ec | ||
|
|
172f85ea6f | ||
|
|
6f70417b78 | ||
|
|
d357e82b54 | ||
|
|
553f4788e4 |
@@ -36,3 +36,22 @@
|
|||||||
0
|
0
|
||||||
letter-counts)))
|
letter-counts)))
|
||||||
|
|
||||||
|
(defn sort-map-by-count
|
||||||
|
"Return a list of pairs from a map of pairs where a pair is a character and its count. The order is descending."
|
||||||
|
[count-map]
|
||||||
|
(->> count-map
|
||||||
|
(reduce (fn [acc [char count]] (conj acc [char count])) [])
|
||||||
|
(sort-by #(- (second %)))))
|
||||||
|
|
||||||
|
(defn table-to-string
|
||||||
|
"Return the string from a frequency table."
|
||||||
|
[table]
|
||||||
|
(->> (map first table)
|
||||||
|
(apply str)))
|
||||||
|
|
||||||
|
(defn symbol-frequency-table
|
||||||
|
"Return a table where pairs is a symbol from a cipher text and its value which is a symbol from frequency table."
|
||||||
|
[cipher-table frequency-table]
|
||||||
|
(zipmap (table-to-string (sort-map-by-count cipher-table))
|
||||||
|
(table-to-string (sort-map-by-count frequency-table))))
|
||||||
|
|
||||||
|
|||||||
@@ -44,15 +44,26 @@
|
|||||||
(caesar/encrypt-message key symbols)
|
(caesar/encrypt-message key symbols)
|
||||||
(encrypt-message substitution-table)))
|
(encrypt-message substitution-table)))
|
||||||
|
|
||||||
|
(defn decrypt-by-table
|
||||||
|
"Decrypt a message by the substitution-table"
|
||||||
|
[substitution-table message]
|
||||||
|
(map (fn [char] (find-value-in-table char substitution-table)) message))
|
||||||
|
|
||||||
|
(defn decrypt-message-by-symbol-table
|
||||||
|
"Decrypt a message using the simple substitution cipher. The function is case-insensitive. The substitution-table must be (symbols, symbols)."
|
||||||
|
[substitution-table message ]
|
||||||
|
(->> message
|
||||||
|
(decrypt-by-table substitution-table)
|
||||||
|
(remove nil?)
|
||||||
|
(cs/join)))
|
||||||
|
|
||||||
(defn decrypt-message
|
(defn decrypt-message
|
||||||
"Decrypt a message using the simple substitution cipher. The function is case-insensitive. The substitution-table must be "
|
"Decrypt a message using the simple substitution cipher. The function is case-insensitive. The substitution-table must be (int, symbols)."
|
||||||
[message substitution-table]
|
[message substitution-table]
|
||||||
(let [message (cs/split message #",")]
|
(let [message (cs/split message #",")]
|
||||||
(->> message
|
(->> message
|
||||||
(map #(Integer/parseInt %))
|
(map #(Integer/parseInt %))
|
||||||
(map (fn [char] (find-value-in-table char substitution-table)))
|
(decrypt-message-by-symbol-table substitution-table))))
|
||||||
(remove nil?)
|
|
||||||
(cs/join))))
|
|
||||||
|
|
||||||
(defn decrypt-message-with-caesar
|
(defn decrypt-message-with-caesar
|
||||||
"Decrypt a message using the simple substitution cipher and the Caesar cipher. The function is case-insensitive."
|
"Decrypt a message using the simple substitution cipher and the Caesar cipher. The function is case-insensitive."
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
(ns cipher-analytical-machine.cli.actions.cryptography
|
(ns cipher-analytical-machine.cli.actions.cryptography
|
||||||
(:require
|
(:require
|
||||||
[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.parsers.parsers :as ps]
|
||||||
|
[cipher-analytical-machine.parsers.simple-substitution :as pss])
|
||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
(defn caesar-actions
|
(defn caesar-actions
|
||||||
@@ -15,10 +18,33 @@
|
|||||||
(= action-type :decrypt)
|
(= action-type :decrypt)
|
||||||
(caesar/decrypt-message message key symbols))))
|
(caesar/decrypt-message message key symbols))))
|
||||||
|
|
||||||
|
(defn simple-substitution-with-caesar-actions
|
||||||
|
[options arguments action-type]
|
||||||
|
(let [message (:message options)
|
||||||
|
key (:key options)
|
||||||
|
symbols (:symbols options)]
|
||||||
|
(cond
|
||||||
|
(= action-type :encrypt)
|
||||||
|
(let [data (pss/generate-table-or-decode-json key symbols)
|
||||||
|
key (ps/parse-unsigned-int (get data "key"))
|
||||||
|
substitution-table (get data "table")]
|
||||||
|
(println (pss/encode-key-and-substitution-table-to-json key substitution-table))
|
||||||
|
(ss/encrypt-message-with-caesar message key substitution-table symbols))
|
||||||
|
|
||||||
|
(= action-type :decrypt)
|
||||||
|
(let [data (-> (pss/decode-key-and-substitution-table-from-json key)
|
||||||
|
(pss/invert-table-in-map))
|
||||||
|
key (get data "key")
|
||||||
|
substitution-table (get data "table")]
|
||||||
|
(ss/decrypt-message-with-caesar message key substitution-table symbols)))))
|
||||||
|
|
||||||
(defn cryptography-actions
|
(defn cryptography-actions
|
||||||
[options arguments action-type]
|
[options arguments action-type]
|
||||||
(let [cipher (:cipher options)]
|
(let [cipher (:cipher options)]
|
||||||
(cond
|
(cond
|
||||||
(= cipher "Caesar")
|
(= cipher "Caesar")
|
||||||
(caesar-actions options arguments action-type))))
|
(caesar-actions options arguments action-type)
|
||||||
|
|
||||||
|
(= cipher "Simple substitution and Caesar")
|
||||||
|
(simple-substitution-with-caesar-actions options arguments action-type))))
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
(option-values-as-list option values)))
|
(option-values-as-list option values)))
|
||||||
|
|
||||||
(def cipher-options
|
(def cipher-options
|
||||||
#{"Caesar"})
|
#{"Caesar" "Simple substitution and Caesar"})
|
||||||
|
|
||||||
(def default-cipher "Caesar")
|
(def default-cipher "Caesar")
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,21 @@
|
|||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
(defn unsigned-int?
|
(defn unsigned-int?
|
||||||
|
"Return true if the string is an unsigned integer."
|
||||||
[str]
|
[str]
|
||||||
(if (re-matches #"\d+" str)
|
(if (re-matches #"\d+" str)
|
||||||
true false))
|
true false))
|
||||||
|
|
||||||
|
(defn parse-unsigned-int
|
||||||
|
"Return an integer if the argument is an integer."
|
||||||
|
[str-or-int]
|
||||||
|
(cond
|
||||||
|
(int? str-or-int)
|
||||||
|
str-or-int
|
||||||
|
|
||||||
|
(unsigned-int? str-or-int)
|
||||||
|
(Integer/parseInt str-or-int)
|
||||||
|
|
||||||
|
:else
|
||||||
|
nil))
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
(:require
|
(:require
|
||||||
[cipher-analytical-machine.ciphers.simple-substitution :as ss]
|
[cipher-analytical-machine.ciphers.simple-substitution :as ss]
|
||||||
[cipher-analytical-machine.parsers.parsers :as ps]
|
[cipher-analytical-machine.parsers.parsers :as ps]
|
||||||
|
[clojure.set :as set]
|
||||||
[cheshire.core :as cc])
|
[cheshire.core :as cc])
|
||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
@@ -42,3 +43,12 @@
|
|||||||
(assoc "table" (ss/generate-substitution-table symbols)))
|
(assoc "table" (ss/generate-substitution-table symbols)))
|
||||||
(decode-key-and-substitution-table-from-json key-or-json)))
|
(decode-key-and-substitution-table-from-json key-or-json)))
|
||||||
|
|
||||||
|
(defn invert-table-in-map
|
||||||
|
"Invert the table in a decoded json (key, table)"
|
||||||
|
[decoded-json]
|
||||||
|
(->>
|
||||||
|
(-> decoded-json
|
||||||
|
(get "table")
|
||||||
|
(set/map-invert))
|
||||||
|
(assoc decoded-json "table")))
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
(ns cipher-analytical-machine.analyzers.analyzers-test
|
||||||
|
(:require
|
||||||
|
[clojure.test :refer :all]
|
||||||
|
[cipher-analytical-machine.analyzers.analyzers :refer :all]))
|
||||||
|
|
||||||
|
(deftest count-characters-test
|
||||||
|
(testing "Count two dublicates and one uniq character from a string."
|
||||||
|
(is (= {\a 2 \b 1}
|
||||||
|
(count-characters "aba")))))
|
||||||
|
|
||||||
|
(deftest chi-squared-letter-statistic-test
|
||||||
|
(testing "If the frequence of A is 0.1, the text length is 100 and contains 20 times, then the chi-squared is 10."
|
||||||
|
(is (= 10.0
|
||||||
|
(chi-squared-letter-statistic 20 0.1 100))))
|
||||||
|
|
||||||
|
(testing "If the frequence of A is 0.1, the text length is 10 and contains 5 times, then the chi-squared is 16.0."
|
||||||
|
(is (= 16.0
|
||||||
|
(chi-squared-letter-statistic 5 0.1 10)))))
|
||||||
|
|
||||||
|
(deftest chi-squared-statistic-test
|
||||||
|
(let [text "aaaaabbbbb"]
|
||||||
|
(testing "If the frequencies of a and b is 0.1, the text length is 10 and contains them 5 times, then score is 32."
|
||||||
|
(is (= 32.0
|
||||||
|
(chi-squared-statistic text {\a 0.1 \b 0.1}))))))
|
||||||
|
|
||||||
|
(deftest sort-map-by-count-test
|
||||||
|
(testing "Test the sort in descending order."
|
||||||
|
(is (= [[\b 3] [\a 2] [\c 1]]
|
||||||
|
(sort-map-by-count {\a 2 \b 3 \c 1} )))))
|
||||||
|
|
||||||
|
(deftest table-to-string-test
|
||||||
|
(testing "Test the sort in descending order."
|
||||||
|
(is (= "bac"
|
||||||
|
(table-to-string [[\b 3] [\a 2] [\c 1]])))))
|
||||||
|
|
||||||
|
(deftest symbol-frequency-table-test
|
||||||
|
(testing "Test the sort in descending order."
|
||||||
|
(is (= {\d \b, \e \a, \f \c}
|
||||||
|
(symbol-frequency-table {\d 3 \e 2 \f 1} {\b 3 \a 2 \c 1})))))
|
||||||
|
|
||||||
@@ -48,6 +48,23 @@
|
|||||||
"abc" 1 "2,3,1"
|
"abc" 1 "2,3,1"
|
||||||
"aDbdc" 0 "1,2,3"))))
|
"aDbdc" 0 "1,2,3"))))
|
||||||
|
|
||||||
|
(deftest decrypt-by-table-test
|
||||||
|
(let [table {\d \a \e \b \f \c}]
|
||||||
|
(testing "The function must decrypt the message by the table."
|
||||||
|
(are [message expected]
|
||||||
|
(= expected (decrypt-by-table table message))
|
||||||
|
"def" '(\a \b \c)
|
||||||
|
"abf" '(nil nil \c)
|
||||||
|
"fed" '(\c \b \a)))))
|
||||||
|
|
||||||
|
(deftest decrypt-message-by-symbol-table-test
|
||||||
|
(let [table {\d \a \e \b \f \c}]
|
||||||
|
(testing "The function must decrypt the message and remove unknown numbers."
|
||||||
|
(are [message expected]
|
||||||
|
(= expected (decrypt-message-by-symbol-table table message))
|
||||||
|
"def" "abc"
|
||||||
|
"daef" "abc"))))
|
||||||
|
|
||||||
(deftest decrypt-message-test
|
(deftest decrypt-message-test
|
||||||
(let [table {1 \a 2 \b 3 \c}]
|
(let [table {1 \a 2 \b 3 \c}]
|
||||||
(testing "The function must decrypt the message and remove unknown numbers."
|
(testing "The function must decrypt the message and remove unknown numbers."
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
[cipher-analytical-machine.parsers.parsers :refer :all]))
|
[cipher-analytical-machine.parsers.parsers :refer :all]))
|
||||||
|
|
||||||
(deftest unsigned-int?-test
|
(deftest unsigned-int?-test
|
||||||
(testing ""
|
(testing "The function return true only for an unsingned integer as a string."
|
||||||
(are [str expected]
|
(are [str expected]
|
||||||
(= expected
|
(= expected
|
||||||
(unsigned-int? str))
|
(unsigned-int? str))
|
||||||
@@ -13,3 +13,13 @@
|
|||||||
"-10" false
|
"-10" false
|
||||||
"abc" false)))
|
"abc" false)))
|
||||||
|
|
||||||
|
(deftest parse-unsigned-int-test
|
||||||
|
(testing "The function parse the integer if the string is an integer."
|
||||||
|
(are [str-or-int expected]
|
||||||
|
(= expected
|
||||||
|
(parse-unsigned-int str-or-int))
|
||||||
|
"9" 9
|
||||||
|
9 9
|
||||||
|
"10" 10
|
||||||
|
"-10" nil
|
||||||
|
"abc" nil)))
|
||||||
|
|||||||
@@ -39,3 +39,11 @@
|
|||||||
"1" {"key" 1 "table" {0 \a 1 \b 2 \c}}
|
"1" {"key" 1 "table" {0 \a 1 \b 2 \c}}
|
||||||
"2" {"key" 2 "table" {\a 0 \b 1 \c 2}}))))
|
"2" {"key" 2 "table" {\a 0 \b 1 \c 2}}))))
|
||||||
|
|
||||||
|
(deftest invert-table-in-map-test
|
||||||
|
(testing "The function invert the table in a decoded json."
|
||||||
|
(are [decoded-json expected-data]
|
||||||
|
(= expected-data
|
||||||
|
(invert-table-in-map decoded-json))
|
||||||
|
{"key" 1 "table" {0 \a 1 \b 2 \c}} {"key" 1 "table" {\a 0 \b 1 \c 2}}
|
||||||
|
{"key" 2 "table" {\a 0 \b 1 \c 2}} {"key" 2 "table" {0 \a 1 \b 2 \c}})))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user