Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a56704226b | ||
|
|
b53559030f |
@@ -1,8 +1,10 @@
|
|||||||
(ns cipher-analytical-machine.analyzers.caesar
|
(ns cipher-analytical-machine.analyzers.caesar
|
||||||
(:require [cipher-analytical-machine.ciphers.caesar :as caesar]
|
(:require [cipher-analytical-machine.ciphers.caesar :as caesar]
|
||||||
[clojure.string :as cs]
|
[clojure.string :as cs]
|
||||||
|
[cipher-analytical-machine.analyzers.language :as language]
|
||||||
[cipher-analytical-machine.analyzers.analyzers :as ca])
|
[cipher-analytical-machine.analyzers.analyzers :as ca])
|
||||||
(:import [cipher_analytical_machine.ciphers.caesar Decrypted]
|
(:import [cipher_analytical_machine.ciphers.caesar Decrypted]
|
||||||
|
[cipher_analytical_machine.analyzers.caesar IsNonsense]
|
||||||
[cipher_analytical_machine.analyzers.caesar FrequencyAnalyzer])
|
[cipher_analytical_machine.analyzers.caesar FrequencyAnalyzer])
|
||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
@@ -50,10 +52,13 @@
|
|||||||
|
|
||||||
(defn frequency-analizer-get-plaintext
|
(defn frequency-analizer-get-plaintext
|
||||||
"Return the plaintext from a ciphertext using simple analizer. The function is case-insensitive."
|
"Return the plaintext from a ciphertext using simple analizer. The function is case-insensitive."
|
||||||
[ciphertext symbols letter-frequencies-string]
|
[ciphertext symbols letter-frequencies-string language-code]
|
||||||
(let [decrypt (reify Decrypted
|
(let [decrypt (reify Decrypted
|
||||||
(decrypt [this message key symbols]
|
(decrypt [this message key symbols]
|
||||||
(caesar/decrypt-message message key symbols)))]
|
(caesar/decrypt-message message key symbols)))
|
||||||
(-> (new FrequencyAnalyzer ciphertext symbols letter-frequencies-string decrypt)
|
is-nonsense (reify IsNonsense
|
||||||
|
(isNonsense [this message]
|
||||||
|
(language/is-nonsense? message language-code)))]
|
||||||
|
(-> (new FrequencyAnalyzer ciphertext symbols letter-frequencies-string decrypt is-nonsense)
|
||||||
.crack)))
|
.crack)))
|
||||||
|
|
||||||
|
|||||||
@@ -160,7 +160,8 @@
|
|||||||
|
|
||||||
(= analyzer "Frequency")
|
(= analyzer "Frequency")
|
||||||
(caesar-analyzers/frequency-analizer-get-plaintext message symbols
|
(caesar-analyzers/frequency-analizer-get-plaintext message symbols
|
||||||
(symbol-frequencies/map-to-string frequencies)))))
|
(symbol-frequencies/map-to-string frequencies)
|
||||||
|
(:language options "en")))))
|
||||||
|
|
||||||
(defn cracking-actions
|
(defn cracking-actions
|
||||||
[options arguments action-type]
|
[options arguments action-type]
|
||||||
|
|||||||
@@ -1,25 +1,53 @@
|
|||||||
package cipher_analytical_machine.analyzers.caesar;
|
package cipher_analytical_machine.analyzers.caesar;
|
||||||
|
|
||||||
|
import cipher_analytical_machine.analyzers.caesar.IsNonsense;
|
||||||
import cipher_analytical_machine.ciphers.caesar.Decrypted;
|
import cipher_analytical_machine.ciphers.caesar.Decrypted;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
public class FrequencyAnalyzer {
|
public class FrequencyAnalyzer {
|
||||||
private String ciphertext;
|
private String ciphertext;
|
||||||
private int key;
|
|
||||||
private String symbols;
|
private String symbols;
|
||||||
private String symbol_frequences;
|
private String symbol_frequences;
|
||||||
private Decrypted decryptor;
|
private Decrypted decryptor;
|
||||||
|
private IsNonsense isNonsense;
|
||||||
|
|
||||||
public FrequencyAnalyzer(String ciphertext, String symbols, String symbol_frequences, Decrypted decryptor) {
|
public FrequencyAnalyzer(String ciphertext, String symbols, String symbol_frequences, Decrypted decryptor, IsNonsense isNonsense) {
|
||||||
this.ciphertext = ciphertext;
|
this.ciphertext = ciphertext;
|
||||||
this.key = 1;
|
|
||||||
this.symbols = symbols;
|
|
||||||
this.symbol_frequences = symbol_frequences;
|
this.symbol_frequences = symbol_frequences;
|
||||||
|
this.symbols = symbols;
|
||||||
this.decryptor = decryptor;
|
this.decryptor = decryptor;
|
||||||
|
this.isNonsense = isNonsense;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String crack() {
|
public String crack() {
|
||||||
return decryptor.decrypt(ciphertext, key, symbols);
|
// Знаходимо кількість літер
|
||||||
|
Map<Character, Integer> encryptedLetterCounts = new HashMap<>();
|
||||||
|
|
||||||
|
for (char c : ciphertext.toLowerCase().toCharArray()) {
|
||||||
|
encryptedLetterCounts.put(c, encryptedLetterCounts.getOrDefault(c, 0) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Знаходимо букву, яка зустрічається найчастіше
|
||||||
|
char mostFrequentEncryptedLetter = ' ';
|
||||||
|
int mostFrequentEncryptedLetterCount = 0;
|
||||||
|
|
||||||
|
for (Entry<Character, Integer> entry : encryptedLetterCounts.entrySet()) {
|
||||||
|
if (entry.getValue() > mostFrequentEncryptedLetterCount) {
|
||||||
|
mostFrequentEncryptedLetter = entry.getKey();
|
||||||
|
mostFrequentEncryptedLetterCount = entry.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Обчислимо зміщення
|
||||||
|
int size = symbols.length();
|
||||||
|
int indexOfMostFrequentEncryptedLetter = symbols.indexOf(mostFrequentEncryptedLetter);
|
||||||
|
int indexOfMostFrequentLetter = symbols.indexOf(symbol_frequences.charAt(0));
|
||||||
|
int shift = (indexOfMostFrequentEncryptedLetter - indexOfMostFrequentLetter) % size;
|
||||||
|
|
||||||
|
return decryptor.decrypt(ciphertext, shift, symbols);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package cipher_analytical_machine.analyzers.caesar;
|
||||||
|
|
||||||
|
public interface IsNonsense {
|
||||||
|
boolean isNonsense(String message);
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user