diff --git a/src/cipher_analytical_machine/ciphers/simple_substitution.clj b/src/cipher_analytical_machine/ciphers/simple_substitution.clj index 365f405..95e1d6b 100644 --- a/src/cipher_analytical_machine/ciphers/simple_substitution.clj +++ b/src/cipher_analytical_machine/ciphers/simple_substitution.clj @@ -44,15 +44,26 @@ (caesar/encrypt-message key symbols) (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 - "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] (let [message (cs/split message #",")] (->> message (map #(Integer/parseInt %)) - (map (fn [char] (find-value-in-table char substitution-table))) - (remove nil?) - (cs/join)))) + (decrypt-message-by-symbol-table substitution-table)))) (defn decrypt-message-with-caesar "Decrypt a message using the simple substitution cipher and the Caesar cipher. The function is case-insensitive." diff --git a/test/cipher_analytical_machine/ciphers/simple_substitution_test.clj b/test/cipher_analytical_machine/ciphers/simple_substitution_test.clj index 6e6495c..5eac042 100644 --- a/test/cipher_analytical_machine/ciphers/simple_substitution_test.clj +++ b/test/cipher_analytical_machine/ciphers/simple_substitution_test.clj @@ -48,6 +48,23 @@ "abc" 1 "2,3,1" "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 (let [table {1 \a 2 \b 3 \c}] (testing "The function must decrypt the message and remove unknown numbers."