-
Notifications
You must be signed in to change notification settings - Fork 47
Finish #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Finish #38
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 { | ||
|
|
||
| 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"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо было создать объект а затем вызывать какой-то его метод |
||
| } catch (IOException e) { | ||
| System.err.println("Ошибка: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| 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(); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } |
| 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"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| } | ||
| 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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Интересно если класс публичный то почему конструктор не публичный |
||
| System.out.println("Введите путь до файла:"); | ||
| Scanner scanner = new Scanner(System.in); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вот именно этот сканер и нужно передавать везде где он требуется |
||
| int numberOfMenu = consnole.nextInt(); | ||
|
|
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Почему-то приложение не разбито на пакеты. Даже для маленьких программ всегда используют пакеты для того чтобы понимать где логика где ввод где вывод