-
Notifications
You must be signed in to change notification settings - Fork 16
Finished project of the first module #10
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
DevEvge
wants to merge
14
commits into
JavaRush-GNEW:main
Choose a base branch
from
DevEvge: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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7a7f32f
Improve alphabet
67c5495
Create and implementation alphabetDetector in Cypher
4ba7e5e
Create Decrypt method. Bug with ukr language
3ea3d94
Created method BruteForce.
efc830b
Improved ArgumentParser and some other fixes
c01b413
Improved method main
06cc481
Improve detector
851d523
StringBuilder bugFixed
db38886
Add GUI without disable fields
1eaecd8
Add Disabled fields
63cf3a3
Add css stylesheet
40f1812
Refactor controller, add Filo not found exception
831163a
Add readme.md
87a7374
Add readme.md
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
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
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,80 @@ | ||
| package ua.com.javarush.gnew; | ||
|
|
||
| import javafx.application.Application; | ||
| import javafx.fxml.FXMLLoader; | ||
| import javafx.scene.Parent; | ||
| import javafx.scene.Scene; | ||
| import javafx.stage.Stage; | ||
| import ua.com.javarush.gnew.crypto.BruteForceResult; | ||
| import ua.com.javarush.gnew.crypto.Cypher; | ||
| import ua.com.javarush.gnew.exeptions.FileNotFoundException; | ||
| import ua.com.javarush.gnew.file.ChangeFileName; | ||
| 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.io.IOException; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class Main { | ||
| public class Main extends Application { | ||
|
|
||
|
|
||
| @Override | ||
| public void start(Stage primaryStage) throws IOException { | ||
| Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("UI.fxml")); | ||
| primaryStage.setTitle("Caesar Cipher"); | ||
| primaryStage.setScene(new Scene(root)); | ||
| primaryStage.show(); | ||
| } | ||
|
|
||
| public static Cypher cypher = new Cypher(); | ||
| public static FileManager fileManager = new FileManager(); | ||
| public static ArgumentsParser argumentsParser = new ArgumentsParser(); | ||
| public static ChangeFileName changeFileName = new ChangeFileName(); | ||
|
|
||
| public static void main(String[] args) { | ||
| Cypher cypher = new Cypher(); | ||
| FileManager fileManager = new FileManager(); | ||
| ArgumentsParser argumentsParser = new ArgumentsParser(); | ||
| RunOptions runOptions = argumentsParser.parse(args); | ||
|
|
||
| if (args.length == 0) { | ||
| launch(); | ||
| } | ||
| try { | ||
| RunOptions runOptions = argumentsParser.parse(args); | ||
| String content = fileManager.read(runOptions.getFilePath()); | ||
|
|
||
| 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"; | ||
|
|
||
| Path newFilePath = runOptions.getFilePath().resolveSibling(newFileName); | ||
| fileManager.write(newFilePath, encryptedContent); | ||
| Path newFilePath = changeFileName.newFileName(runOptions, "ENCRYPTED"); | ||
| fileManager.write(newFilePath, cypher.encrypt(content, runOptions.getKey())); | ||
| } else if (runOptions.getCommand() == Command.DECRYPT) { | ||
|
|
||
| Path newFilePath = changeFileName.newFileName(runOptions, "DECRYPTED"); | ||
| fileManager.write(newFilePath, cypher.decrypt(content, runOptions.getKey())); | ||
| } else if (runOptions.getCommand() == Command.BRUTEFORCE) { | ||
|
|
||
| BruteForceResult result = cypher.bruteforce(content); | ||
| Path newFilePath = changeFileName.newFileNameWithKey(runOptions, "BRUTEFORCED", result.getKey()); | ||
| fileManager.write(newFilePath, result.getDecryptedContent()); | ||
| } | ||
| } catch (FileNotFoundException e) { | ||
| System.out.println("File not found"); | ||
| } catch (Exception e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
|
Comment on lines
36
to
64
Contributor
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. It's always a good idea to keep the logic out of the main method. |
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
185 changes: 185 additions & 0 deletions
185
src/main/java/ua/com/javarush/gnew/controller/Controller.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,185 @@ | ||
| package ua.com.javarush.gnew.controller; | ||
|
|
||
| import javafx.event.ActionEvent; | ||
| import javafx.fxml.FXML; | ||
| import javafx.fxml.Initializable; | ||
| import javafx.scene.control.Button; | ||
| import javafx.scene.control.ComboBox; | ||
| import javafx.scene.control.Label; | ||
| import javafx.scene.control.TextField; | ||
| import javafx.scene.input.KeyEvent; | ||
| import javafx.scene.input.MouseEvent; | ||
| import javafx.scene.layout.AnchorPane; | ||
| import javafx.stage.FileChooser; | ||
| import javafx.stage.Stage; | ||
| import ua.com.javarush.gnew.crypto.BruteForceResult; | ||
| import ua.com.javarush.gnew.crypto.Cypher; | ||
| import ua.com.javarush.gnew.file.ChangeFileName; | ||
| import ua.com.javarush.gnew.file.FileManager; | ||
| import ua.com.javarush.gnew.runner.Command; | ||
| import ua.com.javarush.gnew.runner.RunOptions; | ||
|
|
||
| import java.awt.Desktop; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.nio.file.Path; | ||
| import java.util.ResourceBundle; | ||
|
|
||
| public class Controller implements Initializable { | ||
|
Contributor
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. Very good job here |
||
|
|
||
| private final FileChooser fileChooser = new FileChooser(); | ||
| private final ChangeFileName changeFileName = new ChangeFileName(); | ||
| private final FileManager fileManager = new FileManager(); | ||
| private final Cypher cypher = new Cypher(); | ||
| private String selectedPath; | ||
|
|
||
| @FXML | ||
| private Label actualPathLabel; | ||
|
|
||
| @FXML | ||
| private AnchorPane pane; | ||
|
|
||
| @FXML | ||
| private ComboBox<String> dropdown; | ||
|
|
||
| @FXML | ||
| private Button openNewFileDir; | ||
|
|
||
| @FXML | ||
| private Button choesePathButton; | ||
|
|
||
| @FXML | ||
| private TextField inputKey; | ||
|
|
||
| @FXML | ||
| private Button doCypherButton; | ||
|
|
||
| @FXML | ||
| private Label newFilePath; | ||
|
|
||
| @FXML | ||
| private Label status; | ||
|
|
||
| @FXML | ||
| void choeseButtonCliced(ActionEvent event) { | ||
| File selectedFile = fileChooser.showOpenDialog(new Stage()); | ||
| if (selectedFile != null) { | ||
| updatePathLabel(selectedFile.toString()); | ||
| updateUIForSelectedCommand(); | ||
| openNewFileDir.setDisable(true); | ||
| } | ||
| } | ||
|
|
||
| private void updatePathLabel(String path) { | ||
| actualPathLabel.setText(path); | ||
| } | ||
|
|
||
| private void updateUIForSelectedCommand() { | ||
| String selectedCommand = dropdown.getValue(); | ||
| if ("Encrypt".equals(selectedCommand) || "Decrypt".equals(selectedCommand)) { | ||
| inputKey.setDisable(false); | ||
| } else if ("Bruteforce".equals(selectedCommand)) { | ||
| doCypherButton.setDisable(false); | ||
| } | ||
| } | ||
|
|
||
| @FXML | ||
| void keyInputSelected(KeyEvent event) { | ||
| if (inputKey.isFocused()) { | ||
| doCypherButton.setDisable(false); | ||
| } | ||
| } | ||
|
|
||
| @FXML | ||
| void doCypher(ActionEvent event) { | ||
| if (isFormValid()) { | ||
| try { | ||
| processCommand(); | ||
| resetUIAfterProcessing(); | ||
| } catch (IOException e) { | ||
| status.setText("Произошла ошибка: " + e.getMessage()); | ||
| } | ||
| } else { | ||
| status.setText("Поля не заполнены"); | ||
| } | ||
| } | ||
|
|
||
| private boolean isFormValid() { | ||
| return !actualPathLabel.getText().isEmpty() && !dropdown.getSelectionModel().isEmpty(); | ||
| } | ||
|
|
||
| private void processCommand() throws IOException { | ||
| String selectedCommand = dropdown.getValue(); | ||
| Integer key = getKey(); | ||
| Path filePath = Path.of(actualPathLabel.getText()); | ||
| selectedPath = actualPathLabel.getText(); | ||
| RunOptions runOptions = new RunOptions(getCommand(selectedCommand), key, filePath); | ||
| String content = fileManager.read(runOptions.getFilePath()); | ||
|
|
||
| if ("Encrypt".equals(selectedCommand)) { | ||
| writeToFile(changeFileName.newFileName(runOptions, "ENCRYPTED"), cypher.encrypt(content, runOptions.getKey())); | ||
| } else if ("Decrypt".equals(selectedCommand)) { | ||
| writeToFile(changeFileName.newFileName(runOptions, "DECRYPTED"), cypher.decrypt(content, runOptions.getKey())); | ||
| } else if ("Bruteforce".equals(selectedCommand)) { | ||
| BruteForceResult result = cypher.bruteforce(content); | ||
| writeToFile(changeFileName.newFileNameWithKey(runOptions, "DECRYPTED", result.getKey()), result.getDecryptedContent()); | ||
| } | ||
| } | ||
|
|
||
| private Command getCommand(String command) { | ||
| return switch (command) { | ||
| case "Encrypt" -> Command.ENCRYPT; | ||
| case "Decrypt" -> Command.DECRYPT; | ||
| case "Bruteforce" -> Command.BRUTEFORCE; | ||
| default -> throw new IllegalArgumentException("Unknown command: " + command); | ||
| }; | ||
| } | ||
|
|
||
| private Integer getKey() { | ||
| return "Bruteforce".equals(dropdown.getValue()) ? 0 : Integer.parseInt(inputKey.getText()); | ||
| } | ||
|
|
||
| private void writeToFile(Path filePath, String content) throws IOException { | ||
| fileManager.write(filePath, content); | ||
| } | ||
|
|
||
| private void resetUIAfterProcessing() { | ||
| inputKey.clear(); | ||
| inputKey.setDisable(true); | ||
| doCypherButton.setDisable(true); | ||
| openNewFileDir.setDisable(false); | ||
| actualPathLabel.setText(""); | ||
| status.setText("Done"); | ||
| } | ||
|
|
||
| @FXML | ||
| void methodChoesed (ActionEvent event) { | ||
| choesePathButton.setDisable(false); | ||
| inputKey.setVisible(!"Bruteforce".equals(dropdown.getSelectionModel().getSelectedItem())); | ||
| } | ||
|
|
||
| @FXML | ||
| void newFileOpenPath(ActionEvent event) { | ||
| File file = new File(selectedPath); | ||
| try { | ||
| Desktop.getDesktop().open(file.getParentFile()); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| @FXML | ||
| void paneMouseClicked(MouseEvent event) { | ||
| if (inputKey.isFocused()) { | ||
| pane.requestFocus(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void initialize(URL url, ResourceBundle resourceBundle) { | ||
| dropdown.getItems().addAll("Encrypt", "Decrypt", "Bruteforce"); | ||
| fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Текстовые файлы", "*.txt")); | ||
| fileChooser.setInitialDirectory(new File("C:\\Users\\evgen\\Desktop")); | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
src/main/java/ua/com/javarush/gnew/crypto/BruteForceResult.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,19 @@ | ||
| package ua.com.javarush.gnew.crypto; | ||
|
|
||
| public class BruteForceResult { | ||
| private String key; | ||
| private String decryptedContent; | ||
|
|
||
| public BruteForceResult(String decryptedContent, String key) { | ||
| this.decryptedContent = decryptedContent; | ||
| this.key = key; | ||
| } | ||
|
|
||
| public String getDecryptedContent() { | ||
| return decryptedContent; | ||
| } | ||
|
|
||
| public String getKey() { | ||
| return key; | ||
| } | ||
| } |
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.
Having a UI interface is really cool.