Нужна помощь в правке кода дешифровщика цезаря
Я написал код, который должен шифровать и дешифровать методом цезаря. Код работает, но не все буквы дешифрует корректно. А так же метод статического подбора дешифрует полную ерунду. Прошу помочь найди ошибки.
public static void main(String[] args) {
char[] russianAlphabet = {'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я', '.', ',', ':', '!', '?', ' ', '-'};
int shift = getShiftFromConsole();
String inputFilename = "text.txt";
String outputFilename = "output.txt";
try {
String plaintext = readFromFile(inputFilename).toLowerCase();
String ciphertext = encrypt(plaintext, shift, russianAlphabet);
writeToFile(outputFilename, ciphertext);
System.out.println("Шифрованный текст успешно записан в файл " + outputFilename);
String decryptedText = decrypt(ciphertext, shift, russianAlphabet);
System.out.println("Расшифрованный текст: " + decryptedText);
char mostFrequentLetter = findMostFrequentLetter(decryptedText, russianAlphabet);
int mostFrequentIndex = indexOf(russianAlphabet, mostFrequentLetter);
int statisticalShift = (mostFrequentIndex - indexOf(russianAlphabet, 'о') + russianAlphabet.length) % russianAlphabet.length;
String statisticalDecryptedText = decrypt(ciphertext, statisticalShift, russianAlphabet);
System.out.println("Расшифрованный текст методом статистического анализа: " + statisticalDecryptedText);
} catch (IOException e) {
System.out.println("Ошибка: " + e.getMessage());
}
}
//функция getShiftFromConsole(), позволяет пользователю ввести размер сдвига с консоли.
public static int getShiftFromConsole() {
Scanner scanner = new Scanner(System.in);
System.out.print("Введите размер сдвига: ");
int shift = scanner.nextInt();
scanner.close();
return shift;
}
//Для работы с файлами используются методы readFromFile и writeToFile, которые считывают данные из файла и записывают данные в файл соответственно.
public static String readFromFile(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line.toLowerCase());
}
reader.close();
return stringBuilder.toString();
}
public static void writeToFile(String filename, String content) throws IOException {
FileWriter writer = new FileWriter(filename);
writer.write(content);
writer.close();
}
public static String encrypt(String plaintext, int shift, char[] alphabet) {
StringBuilder ciphertext = new StringBuilder();
for (int i = 0; i < plaintext.length(); i++) {
char currentChar = plaintext.charAt(i);
if (Character.isLetter(currentChar)) {
char encryptedChar = alphabet[(indexOf(alphabet, currentChar) + shift) % alphabet.length];
ciphertext.append(encryptedChar);
} else {
ciphertext.append(currentChar);
}
}
return ciphertext.toString();
}
public static String decrypt(String ciphertext, int shift, char[] alphabet) {
StringBuilder plaintext = new StringBuilder();
for (int i = 0; i < ciphertext.length(); i++) {
char currentChar = ciphertext.charAt(i);
if (Character.isLetter(currentChar)) {
char decryptedChar = alphabet[(indexOf(alphabet, currentChar) - shift + alphabet.length) % alphabet.length];
plaintext.append(decryptedChar);
} else {
plaintext.append(currentChar);
}
}
return plaintext.toString();
}
public static int indexOf(char[] array, char element) {
for (int i = 0; i < array.length; i++) {
if (array[i] == element) {
return i;
}
}
return -1;
}
//Функция findMostFrequentLetter(), находит наиболее часто встречающуюся букву в тексте.
//Затем использует эту информацию для вычисления сдвига методом статистического анализа и расшифровки текста.
public static char findMostFrequentLetter(String text, char[] alphabet) {
int[] frequency = new int[alphabet.length];
for (int i = 0; i < text.length(); i++) {
char currentChar = text.charAt(i);
int index = indexOf(alphabet, currentChar);
if (index != -1) {
frequency[index]++;
}
}
int maxFrequencyIndex = 0;
for (int i = 1; i < frequency.length; i++) {
if (frequency[i] > frequency[maxFrequencyIndex]) {
maxFrequencyIndex = i;
}
}
return alphabet[maxFrequencyIndex];
}
}````
Расшифрованный текст: част, перва?- -- ну что, кн?з,, гену? и лукка стали не бол,ше как помест,?, помест,? фамилии буонапарте. нет, ? вам вперед говор!, если в. мне не скажете, что у нас война, если в. позволите себе защищат, все гадости, все ужас. :того антихриста (право, ? вер!, что он антихрист), -- ? вас бол,ше не зна!, в. уже не друг мой, в. уже не мой верн.й раб, как в. говорите. ну, здравствуйте, здравствуйте. ? –
Никита Кузьмищев
2 минуты назад Править
а вот методом статистического подбора .хэх,а? вабх,.: ю а?!!.:. -- ч. ш?б?ч.? -- а -?аю, ?! ьъ!в, ?цч?щ? ъъ чэш,?щ?:. .!?э, ю ?,юб б?-? ,юч? !хщъ, ач?я -ъщю!ш?б, .?б?-.я в !ъш? ?-!?ч?:в ц., щ,ю!!ъъ ?б?., ю, вбх?а, ч !ъ:, ?цъьх, !х .-.,,е? эх .!?шю!ъя, .?б?-в! ,х.ъя ?щахьючх, чх " х-хэч?,,бъ, авщх-," -?эчвёх,? бх.ю: д?,?щ!.: -ъэ-ъ!юъ:, ёб? .!?э, ю ?,юб ё-ъэч.ёхя!? б?-? ,юч? ?аб?-?!ю,а?, абх, юэчю!?б,а? ю !ъ-чюёъа.ю ъ-ъ.хёючхб,а? а !?шю !х !?шв, .х. цвщб? ?б