-
Notifications
You must be signed in to change notification settings - Fork 16
Іван Бевзюк #11
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
Open
BevzyukIvan
wants to merge
20
commits into
JavaRush-GNEW:main
Choose a base branch
from
BevzyukIvan:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Іван Бевзюк #11
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0dca3fd
Moved the classes from my project to the fork for testing
BevzyukIvan 90fe252
Split the BruteForce class and replaced some static methods with non-…
BevzyukIvan b7610ec
Added Scanner to main, there will be changes in the future
BevzyukIvan 3424bec
Added the Runner class, removed the extra code in Main, the program i…
BevzyukIvan 2118495
Added a command for frequency analysis
BevzyukIvan 8711801
Created variable frequencyAnalysisFilePath
BevzyukIvan 3b91879
Added frequency analysis
BevzyukIvan 9aef289
Simplified Main
BevzyukIvan d250c6e
Moved the functionality from Main to another class
BevzyukIvan b2c7eaa
The language is determined automatically
BevzyukIvan 8ceea6b
Update readme.md
BevzyukIvan ff4c20a
Added the "Clear" button and a window that informs about successful e…
BevzyukIvan 83c4cb2
Added a jar file (JavaFX)
BevzyukIvan 420f632
Merge remote-tracking branch 'origin/main'
BevzyukIvan ee053c6
Project 1.0v
BevzyukIvan aeb6e27
Commit
BevzyukIvan e69c1c8
Update readme.md
BevzyukIvan ef67dfa
Trigger the pipeline
oleksandr-jr dbe686e
Закоментував класи Controller та JavaFXApp
BevzyukIvan 14a37c0
Merge remote-tracking branch 'origin/main'
BevzyukIvan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| //package ua.com.javarush.gnew.JavaFX; | ||
| // | ||
| //import javafx.fxml.FXML; | ||
| //import javafx.scene.control.Button; | ||
| //import javafx.scene.control.TextField; | ||
| //import javafx.scene.control.Alert; | ||
| //import javafx.scene.control.Alert.AlertType; | ||
| //import ua.com.javarush.gnew.runner.CipherApplication; | ||
| // | ||
| //public class Controller { | ||
| // @FXML | ||
| // private TextField filePathField; | ||
| // | ||
| // @FXML | ||
| // private TextField helperFilePathField; | ||
| // | ||
| // @FXML | ||
| // private TextField keyField; | ||
| // | ||
| // @FXML | ||
| // private Button encryptButton, decryptButton, bruteForceButton, clearButton; | ||
| // | ||
| // @FXML | ||
| // private void handleEncrypt() { | ||
| // processAction("encrypt"); | ||
| // } | ||
| // | ||
| // @FXML | ||
| // private void handleDecrypt() { | ||
| // processAction("decrypt"); | ||
| // } | ||
| // | ||
| // @FXML | ||
| // private void handleBruteForce() { | ||
| // processAction("bruteforce"); | ||
| // } | ||
| // | ||
| // @FXML | ||
| // private void handleClear() { | ||
| // filePathField.setText(""); | ||
| // helperFilePathField.setText(""); | ||
| // keyField.setText(""); | ||
| // } | ||
| // | ||
| // private void processAction(String mode) { | ||
| // String filePath = filePathField.getText(); | ||
| // String helperFilePath = helperFilePathField.getText(); | ||
| // String key = keyField.getText(); | ||
| // | ||
| // if (filePath.isEmpty()) { | ||
| // showError("Please specify the file path."); | ||
| // return; | ||
| // } | ||
| // | ||
| // try { | ||
| // CipherApplication app = CipherApplication.getInstance(); | ||
| // if (mode.equals("encrypt")) { | ||
| // app.run(new String[]{"-e", "-f", filePath, "-k", key}); | ||
| // showSuccess("File successfully encrypted."); | ||
| // } else if (mode.equals("decrypt")) { | ||
| // app.run(new String[]{"-d", "-f", filePath, "-k", key}); | ||
| // showSuccess("File successfully decrypted."); | ||
| // } else if (mode.equals("bruteforce")) { | ||
| // if (helperFilePath.isEmpty()) { | ||
| // app.run(new String[]{"-bf", "-f", filePath}); | ||
| // } else { | ||
| // app.run(new String[]{"-bf", "-f", filePath, "-fa", helperFilePath}); | ||
| // } | ||
| // showSuccess("Brute force completed successfully."); | ||
| // } | ||
| // } catch (Exception e) { | ||
| // showError("An error occurred: " + e.getMessage()); | ||
| // } | ||
| // } | ||
| // | ||
| // private void showError(String message) { | ||
| // Alert alert = new Alert(AlertType.ERROR); | ||
| // alert.setTitle("Error"); | ||
| // alert.setHeaderText(null); | ||
| // alert.setContentText(message); | ||
| // alert.showAndWait(); | ||
| // } | ||
| // | ||
| // private void showSuccess(String message) { | ||
| // Alert alert = new Alert(AlertType.INFORMATION); | ||
| // alert.setTitle("Success"); | ||
| // alert.setHeaderText(null); | ||
| // alert.setContentText(message); | ||
| // alert.showAndWait(); | ||
| // } | ||
| //} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| //package ua.com.javarush.gnew.JavaFX; | ||
| // | ||
| //import javafx.application.Application; | ||
| //import javafx.fxml.FXMLLoader; | ||
| //import javafx.scene.Parent; | ||
| //import javafx.scene.Scene; | ||
| //import javafx.stage.Stage; | ||
| // | ||
| //public class JavaFXApp extends Application { | ||
| // | ||
| // @Override | ||
| // public void start(Stage primaryStage) throws Exception { | ||
| // FXMLLoader loader = new FXMLLoader(getClass().getResource("/view.fxml")); | ||
| // Parent root = loader.load(); | ||
| // | ||
| // Scene scene = new Scene(root); | ||
| // | ||
| // primaryStage.setTitle("Cipher Application"); | ||
| // primaryStage.setScene(scene); | ||
| // | ||
| // primaryStage.setOnCloseRequest(event -> { | ||
| // System.out.println("Application closed"); | ||
| // }); | ||
| // | ||
| // primaryStage.show(); | ||
| // } | ||
| // | ||
| // public static void launchApp(String[] args) { | ||
| // Application.launch(args); | ||
| // } | ||
| //} | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,18 @@ | ||
| package ua.com.javarush.gnew; | ||
|
|
||
| import ua.com.javarush.gnew.crypto.Cypher; | ||
| import ua.com.javarush.gnew.file.FileManager; | ||
| import ua.com.javarush.gnew.runner.ArgumentsParser; | ||
| import ua.com.javarush.gnew.runner.Command; | ||
| import ua.com.javarush.gnew.runner.RunOptions; | ||
|
|
||
| import java.nio.file.Path; | ||
| import ua.com.javarush.gnew.runner.CipherApplication; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Cypher cypher = new Cypher(); | ||
| FileManager fileManager = new FileManager(); | ||
| ArgumentsParser argumentsParser = new ArgumentsParser(); | ||
| RunOptions runOptions = argumentsParser.parse(args); | ||
|
|
||
| try { | ||
| if (runOptions.getCommand() == Command.ENCRYPT) { | ||
| String content = fileManager.read(runOptions.getFilePath()); | ||
| String encryptedContent = cypher.encrypt(content, runOptions.getKey()); | ||
| String fileName = runOptions.getFilePath().getFileName().toString(); | ||
| String newFileName = fileName.substring(0, fileName.length() - 4) + " [ENCRYPTED].txt"; | ||
| public class Main { | ||
|
|
||
| Path newFilePath = runOptions.getFilePath().resolveSibling(newFileName); | ||
| fileManager.write(newFilePath, encryptedContent); | ||
| } | ||
| } catch (Exception e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| public static void main(String[] args) { | ||
| // if (args.length == 0) { | ||
| // JavaFXApp.launchApp(args);//Графічний інтерфес | ||
| // } else { | ||
| CipherApplication app = CipherApplication.getInstance(); | ||
| app.run(args); | ||
| // app.runInteractive();//Відповідає за консоль | ||
| // } | ||
| } | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
src/main/java/ua/com/javarush/gnew/arg/ArgumentParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package ua.com.javarush.gnew.arg; | ||
|
|
||
| public class ArgumentParser { | ||
|
|
||
| public static Arguments parse(String[] args) { | ||
| String mode = null; | ||
| String file = null; | ||
| int key = 0; | ||
| String frequencyAnalysisFile = null; | ||
|
|
||
| for (int i = 0; i < args.length; i++) { | ||
| switch (args[i]) { | ||
| case "-e": | ||
| mode = "-e"; | ||
| break; | ||
| case "-d": | ||
| mode = "-d"; | ||
| break; | ||
| case "-bf": | ||
| mode = "-bf"; | ||
| break; | ||
| case "-k": | ||
| key = Integer.parseInt(args[++i]); | ||
| break; | ||
| case "-f": | ||
| file = args[++i]; | ||
| break; | ||
| case "-fa": | ||
| frequencyAnalysisFile = args[++i]; | ||
| break; | ||
| default: | ||
| throw new IllegalArgumentException("Unknown argument: " + args[i]); | ||
| } | ||
| } | ||
|
|
||
| if (file == null || mode == null) { | ||
| throw new IllegalArgumentException("File or mode not specified. Use '-e' for encryption, '-d' for decryption, or '-bf' for brute force."); | ||
| } | ||
|
|
||
| return new Arguments(mode, file, key, frequencyAnalysisFile); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package ua.com.javarush.gnew.arg; | ||
|
|
||
| public class Arguments { | ||
| private final String mode; | ||
| private final String filePath; | ||
| private final int key; | ||
| private final String frequencyAnalysisFilePath; | ||
|
|
||
| public Arguments(String mode, String filePath, int key, String frequencyAnalysisFilePath) { | ||
| this.mode = mode; | ||
| this.filePath = filePath; | ||
| this.key = key; | ||
| this.frequencyAnalysisFilePath = frequencyAnalysisFilePath; | ||
| } | ||
|
|
||
| public String getMode() { | ||
| return mode; | ||
| } | ||
|
|
||
| public String getFilePath() { | ||
| return filePath; | ||
| } | ||
|
|
||
| public int getKey() { | ||
| return key; | ||
| } | ||
| public String getFrequencyAnalysisFilePath() { | ||
| return frequencyAnalysisFilePath; | ||
| } | ||
|
|
||
| public boolean isEncryptionMode() { | ||
| return "-e".equals(mode); | ||
| } | ||
|
|
||
| public boolean isDecryptionMode() { | ||
| return "-d".equals(mode); | ||
| } | ||
|
|
||
| public boolean isBruteForce() { | ||
| return "-bf".equals(mode); | ||
| } | ||
| public boolean hasFrequencyAnalysisFile() { | ||
| return frequencyAnalysisFilePath != null; | ||
| } | ||
| } | ||
|
|
37 changes: 37 additions & 0 deletions
37
src/main/java/ua/com/javarush/gnew/bruteForce/BruteForce.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package ua.com.javarush.gnew.bruteForce; | ||
|
|
||
| import ua.com.javarush.gnew.languageDetector.CreateLanguage; | ||
| import ua.com.javarush.gnew.languageDetector.EnglishLanguageDetector; | ||
| import ua.com.javarush.gnew.languageDetector.LanguageDetector; | ||
| import ua.com.javarush.gnew.crypto.EncryptionUtil; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class BruteForce { | ||
| private final EncryptionUtil encryptionUtil = new EncryptionUtil(); | ||
| private final DecryptionHelper decryptionHelper = new DecryptionHelper(); | ||
|
|
||
| public int brute_force(String s) { | ||
| int result = 0; | ||
| int maxPopular = 0; | ||
|
|
||
| LanguageDetector languageStrategy = new CreateLanguage().createLanguageStrategy(s); | ||
|
|
||
| int alphabetSize = languageStrategy.getAlphabetSize(); | ||
|
|
||
| for (int i = 0; i < alphabetSize; i++) { | ||
| String decrypted = encryptionUtil.decrypt(s, i); | ||
| int popular = decryptionHelper.evaluateText(decrypted, | ||
| languageStrategy.getCommonWords(), | ||
| languageStrategy.getCommonEndings(), | ||
| languageStrategy.getRareCombinations()); | ||
| if (popular > maxPopular) { | ||
| maxPopular = popular; | ||
| result = i; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
36 changes: 36 additions & 0 deletions
36
src/main/java/ua/com/javarush/gnew/bruteForce/DecryptionHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package ua.com.javarush.gnew.bruteForce; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| public class DecryptionHelper { | ||
|
|
||
| public int evaluateText(String s, Set<String> commonWords, List<String> commonEndings, List<String> rareCombinations) { | ||
| String[] words = s.split("\\s+"); | ||
| int popular = 0; | ||
|
|
||
| for (String word : words) { | ||
| word = word.replaceAll("[^a-zA-Zа-яА-ЯіІїЇєЄґҐ]", "").toLowerCase(); | ||
|
|
||
| for (String rare : rareCombinations) { | ||
| if (word.contains(rare)) { | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| if (commonWords.contains(word)) { | ||
| popular++; | ||
| } | ||
|
|
||
| for (String ending : commonEndings) { | ||
| if (word.endsWith(ending)) { | ||
| popular++; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return popular; | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Calling this method using whole text might be uneffective.