Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/main/java/com/javarush/bereznyakov/Brutforce.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.javarush.bereznyakov;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Brutforce {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему-то приложение не разбито на пакеты. Даже для маленьких программ всегда используют пакеты для того чтобы понимать где логика где ввод где вывод


public static final String RU = "мои|твои|они|что-то|еще|что|это|куда|либо|нибудь|кто-то";

Brutforce() throws IOException {
System.out.println("Введите путь до файла:");
Scanner scanner = new Scanner(System.in);
String path = scanner.nextLine();
String temporaryPath = "C:\\Users\\berstix\\IdeaProjects\\CryptoAnalyzerPantera\\text\\outDecode.txt";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Так никогда не делают, например у меня даже диска Ц нет


int bestKey = -1;
int maxCount = 0;
String bestText = "";


String encryptedText = new String(Files.readAllBytes(Paths.get(path)), "UTF-8");


for (int key = 0; key < 200; key++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Полностью вся логика в конструкторе это точно неправильно

StringBuilder decodedText = new StringBuilder();


for (char c : encryptedText.toCharArray()) {
decodedText.append((char)(c - key));
}

String currentText = decodedText.toString();
int count = 0;


Pattern pattern = Pattern.compile(RU);
Matcher matcher = pattern.matcher(currentText);
while (matcher.find()) {
count++;
}


if (count > maxCount) {
maxCount = count;
bestKey = key;
bestText = currentText;
}
}


if (bestKey != -1 && maxCount > 10) { // уменьшил порог для большей гибкости
try (BufferedWriter writer = new BufferedWriter(new FileWriter(temporaryPath))) {
writer.write(bestText);
}
System.out.println("Файл раскодирован ключом: " + bestKey);
System.out.println("Количество совпадений: " + maxCount);
System.out.println("Файл \"out\" раскодирован в \"outDecode.txt\"");
} else {
System.out.println("Не удалось найти подходящий ключ. Лучший ключ: " + bestKey +
", совпадений: " + maxCount);
}

scanner.close();
}

public static void main(String[] args) {
try {
new Brutforce();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо было создать объект а затем вызывать какой-то его метод

} catch (IOException e) {
System.err.println("Ошибка: " + e.getMessage());
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/javarush/bereznyakov/ConsoleRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.javarush.bereznyakov;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;


public class ConsoleRunner {
public static void main(String[] args) throws IOException {

// String text = "C:\\Users\\berstix\\IdeaProjects\\CryptoAnalyzerPantera\\text\\dict.txt";
//C:\Users\berstix\IdeaProjects\CryptoAnalyzerPantera\text\dict.txt

System.out.println(Menu.menuChoise);
Menu menu = new Menu();


int numberOfMenu = menu.numberOfMenu;
if (numberOfMenu == 1){
Encode encode = new Encode();

} else if (numberOfMenu == 2) {
Decode decode = new Decode();
} else if (numberOfMenu ==3) {
Brutforce brutforce = new Brutforce();
}


}
}
36 changes: 36 additions & 0 deletions src/main/java/com/javarush/bereznyakov/Decode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.javarush.bereznyakov;

import java.io.*;
import java.util.Scanner;

public class Decode {
Decode () throws IOException {
System.out.println("Введите путь до файла:");
Scanner scanner = new Scanner(System.in);
String path = scanner.next();
System.out.println(path);

try {
FileInputStream fileInputStream = new FileInputStream(path);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader, 200);
System.out.println("Введите ключ для декодирования");
int numberOfCod = scanner.nextInt();
int i;

File file = new File("C:\\Users\\berstix\\IdeaProjects\\CryptoAnalyzerPantera\\text\\outDecode.txt");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

То же самое


BufferedWriter writer = new BufferedWriter(new FileWriter(file));

while ((i = bufferedReader.read())!= -1){
i= i - numberOfCod;
System.out.print((char)i);


writer.write((char)i);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/javarush/bereznyakov/Encode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.javarush.bereznyakov;


import java.io.*;
import java.util.Scanner;

public class Encode {



Encode() throws IOException {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Интересно если класс публичный то почему конструктор не публичный

System.out.println("Введите путь до файла:");
Scanner scanner = new Scanner(System.in);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сканер клавиатуры это один объект их не должно быть много по всей программе

String path = scanner.next();
System.out.println(path);

try { FileInputStream fileInputStream = new FileInputStream(path);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader, 200);
System.out.println("Введите ключ для кодирования");
int numberOfCod = scanner.nextInt();
int i;


File file = new File("C:\\Users\\berstix\\IdeaProjects\\CryptoAnalyzerPantera\\text\\out.txt");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Опять абсолютный путь это всегда ошибка


BufferedWriter writer = new BufferedWriter(new FileWriter(file));

while ((i = bufferedReader.read())!= -1){
i= i + numberOfCod;
System.out.print((char)i);


writer.write((char)i);

}
} catch (IOException e) {
throw new RuntimeException(e);
}

}


}
17 changes: 17 additions & 0 deletions src/main/java/com/javarush/bereznyakov/Menu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.javarush.bereznyakov;

import java.io.IOException;
import java.util.Scanner;

public class Menu {
static final String menuChoise ="Выберите что хотите сделать с файлом:\n" +
"1) закодировать файл выберите цифру 1\n" +
"2) раскодировать файл зная ключ кодировки, выберите цифру 2\n"+
"3) раскодировать файл спомощью Brute force, выберите цифру 3";


Scanner consnole = new Scanner(System.in);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вот именно этот сканер и нужно передавать везде где он требуется

int numberOfMenu = consnole.nextInt();


}