Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Binary file added out/artifacts/GNEW_M1_FP_jar/GNEW-M1-FP.jar
Binary file not shown.
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
```
-k Key
-f File path
-fa File path for frequency analysis (only used in brute force mode).
```

### Example:
```
-e -k 1 -f "/path/to/file.txt" - Encrypt file with key 1
-d -k 5 -f "/path/to/file [ENCRYPTED].txt" - Decrypt file with key 5
-bf -f "/path/to/file [ENCRYPTED].txt" - Brute force decrypt file
-bf -f "/path/to/file[ENCRYPTED].txt" -fa "/path/to/reference.txt"
```

### Argument could be in any order
```
-e -f "/path/to/file.txt" -k 1
```
-e -f "/path/to/file.txt" -k 1 (Чомусь коли я видаляв класи вони не видалялись на GitHab, довелось чистити кеш Git та відновляти інформацію з останнього коміту, поідеї такої проблеми вже не має бути)
```

91 changes: 91 additions & 0 deletions src/main/java/ua/com/javarush/gnew/JavaFX/Controller.java
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();
// }
//}
32 changes: 32 additions & 0 deletions src/main/java/ua/com/javarush/gnew/JavaFX/JavaFXApp.java
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);
// }
//}

36 changes: 11 additions & 25 deletions src/main/java/ua/com/javarush/gnew/Main.java
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 src/main/java/ua/com/javarush/gnew/arg/ArgumentParser.java
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);
}
}
46 changes: 46 additions & 0 deletions src/main/java/ua/com/javarush/gnew/arg/Arguments.java
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 src/main/java/ua/com/javarush/gnew/bruteForce/BruteForce.java
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);
Copy link
Contributor

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.


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;
}
}

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;
}
}

Loading