parent
ef87a719a3
commit
b53559030f
@ -1,25 +1,50 @@
|
||||
package cipher_analytical_machine.analyzers.caesar;
|
||||
|
||||
import cipher_analytical_machine.ciphers.caesar.Decrypted;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class FrequencyAnalyzer {
|
||||
private String ciphertext;
|
||||
private int key;
|
||||
private String symbols;
|
||||
private String symbol_frequences;
|
||||
private Decrypted decryptor;
|
||||
|
||||
public FrequencyAnalyzer(String ciphertext, String symbols, String symbol_frequences, Decrypted decryptor) {
|
||||
this.ciphertext = ciphertext;
|
||||
this.key = 1;
|
||||
this.symbols = symbols;
|
||||
this.symbol_frequences = symbol_frequences;
|
||||
this.decryptor = decryptor;
|
||||
this.ciphertext = ciphertext;
|
||||
this.symbol_frequences = symbol_frequences;
|
||||
this.symbols = symbols;
|
||||
this.decryptor = decryptor;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in new issue