-
Notifications
You must be signed in to change notification settings - Fork 16
P1 Antonina #8
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?
P1 Antonina #8
Changes from all commits
d06592b
6f773d2
30d5369
8c0bb9f
62adeac
d9a5e03
53265b3
3beed0a
5349ecf
d0c0075
2c0ec20
dacc7a5
89a41b9
2552880
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,3 @@ | ||
| Manifest-Version: 1.0 | ||
| Main-Class: ua.com.javarush.gnew.Main | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| package ua.com.javarush.gnew; | ||
|
|
||
| import ua.com.javarush.gnew.crypto.Cypher; | ||
| import ua.com.javarush.gnew.file.FileManager; | ||
| import ua.com.javarush.gnew.cypher.Cypher; | ||
| import ua.com.javarush.gnew.fileManager.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.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.*; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
|
|
@@ -15,16 +16,39 @@ public static void main(String[] args) { | |
| ArgumentsParser argumentsParser = new ArgumentsParser(); | ||
| RunOptions runOptions = argumentsParser.parse(args); | ||
|
|
||
| List<String> linesRead; | ||
| List<String> linesWritten = new ArrayList<>(); | ||
| Path resultFile = null; | ||
|
|
||
| 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"; | ||
| linesRead = fileManager.read(runOptions.getFilePath()); | ||
| switch (runOptions.getCommand()) { | ||
| case ENCRYPT: | ||
| for (String lineRead : linesRead) { | ||
| linesWritten.add(cypher.encrypt(lineRead, runOptions.getKey())); | ||
| } | ||
| resultFile = fileManager.getNewPath(runOptions.getFilePath(), runOptions.getCommand()); | ||
| break; | ||
| case DECRYPT: | ||
| for (String lineRead : linesRead) { | ||
| linesWritten.add(cypher.decrypt(lineRead, runOptions.getKey())); | ||
| } | ||
| resultFile = fileManager.getNewPath(runOptions.getFilePath(), runOptions.getCommand()); | ||
| break; | ||
| case BRUTEFORCE: | ||
| int key = cypher.bruteForceKey(linesRead); | ||
| for (String lineRead : linesRead) { | ||
| linesWritten.add(cypher.decrypt(lineRead, key)); | ||
| } | ||
| resultFile = fileManager.getNewPath(runOptions.getFilePath(), key); | ||
| break; | ||
| } | ||
|
Comment on lines
+24
to
+45
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 better to keep the logic out of the main method. |
||
|
|
||
| Path newFilePath = runOptions.getFilePath().resolveSibling(newFileName); | ||
| fileManager.write(newFilePath, encryptedContent); | ||
| if (Files.notExists(resultFile)) { | ||
| Files.createFile(resultFile); | ||
| } | ||
| fileManager.write(resultFile, linesWritten); | ||
|
|
||
| } catch (Exception e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package ua.com.javarush.gnew.cypher; | ||
|
|
||
| import ua.com.javarush.gnew.language.Alphabet; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Cypher { | ||
|
|
||
| public String encrypt(String text, int key) { | ||
|
|
||
| ArrayList<Character> rotatedUppercase = Alphabet.rotateAlphabet(Alphabet.ENGLISH_UPPERCASE, key); | ||
| ArrayList<Character> rotatedLowercase = Alphabet.rotateAlphabet(Alphabet.ENGLISH_LOWERCASE, key); | ||
| StringBuilder result = new StringBuilder(); | ||
|
|
||
| for (int i = 0; i < text.length(); i++) { | ||
| char oneChar = text.charAt(i); // беремо символ з тексту | ||
| int index = Alphabet.ENGLISH_LOWERCASE.indexOf(oneChar); // шукаємо його в алфавіті lowercase | ||
| if (index != -1) { // якщо знайшли | ||
| result.append(rotatedLowercase.get(index)); // шифруємо і додаємо до результату | ||
| } else { // якщо немає | ||
| index = Alphabet.ENGLISH_UPPERCASE.indexOf(oneChar); // шукаємо його в алфавіті uppercase | ||
| if (index != -1) { // якщо знайшли | ||
| result.append(rotatedUppercase.get(index)); // шифруємо і додаємо до результату | ||
| } else { // якщо не знайшли в жодному з двох алфавітів, це спеціальний символ | ||
| result.append(text.charAt(i)); // НЕ шифруємо і просто додаємо | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+16
to
+29
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. This might be simplified. |
||
| return result.toString(); | ||
| } | ||
|
|
||
| public String decrypt(String text, int key) { | ||
| // decrypt робить те саме, що й encrypt, тільки з протилежним значенням ключа | ||
| return encrypt(text, -key); | ||
| } | ||
|
|
||
| public int bruteForceKey(List<String> text) { | ||
| String encrypted = ""; | ||
| String decrypted = ""; | ||
|
|
||
| for (String line : text) { | ||
| encrypted = line; | ||
| for (int i = 0; i < 26; i++) { | ||
| decrypted = decrypt(line, i); | ||
| if (checkForCommonWords(decrypted)) { | ||
| break; | ||
| } | ||
| } | ||
| break; | ||
| } | ||
|
Comment on lines
+42
to
+51
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. I like that you decrypt it using lines. In some cases, it's very effective, but there are also some cases when it might not work as expected. Maybe it's good idea to take more than 1 line if it contains not enough letters a.e. always use the text > 256 symbols. |
||
|
|
||
| return calculateKey(encrypted, decrypted); | ||
| } | ||
|
|
||
| private int calculateKey(String encrypted, String decrypted) { | ||
| int firstLetterIndex = 0; | ||
| for (int i = 0; i < decrypted.length(); i++) { | ||
| if (Character.isLetter(decrypted.charAt(i))) { | ||
| firstLetterIndex = i; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| int indexEncrypted = Alphabet.ENGLISH_LOWERCASE.indexOf(Character.toLowerCase(encrypted.charAt(firstLetterIndex))); | ||
| int indexDecrypted = Alphabet.ENGLISH_LOWERCASE.indexOf(Character.toLowerCase(decrypted.charAt(firstLetterIndex))); | ||
|
|
||
| if (indexEncrypted < indexDecrypted) { | ||
| indexEncrypted = indexEncrypted + 26; | ||
| } | ||
| return indexEncrypted - indexDecrypted; | ||
| } | ||
|
|
||
| private boolean checkForCommonWords(String text) { | ||
| ArrayList<String> words = getWords(text); | ||
| boolean containsCommonWords = false; | ||
|
|
||
| for (String word : words) { | ||
| if (Alphabet.ENGLISH_COMMON_WORDS.contains(word.toLowerCase())) { | ||
| containsCommonWords = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return containsCommonWords; | ||
| } | ||
|
|
||
| private ArrayList<String> getWords(String text) { | ||
| String[] words = text.replaceAll("[^a-zA-Z ]", "").split("\\s+"); | ||
|
|
||
| ArrayList<String> wordList = new ArrayList<>(); | ||
| for (String word : words) { | ||
| if (!word.isEmpty()) { | ||
| wordList.add(word); | ||
| } | ||
| } | ||
|
|
||
| return wordList; | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package ua.com.javarush.gnew.fileManager; | ||
|
|
||
| import ua.com.javarush.gnew.runner.Command; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
|
|
||
| public class FileManager { | ||
|
|
||
| public List<String> read(Path path) throws IOException { | ||
| return Files.readAllLines(path); | ||
| } | ||
|
|
||
| public void write(Path path, List<String> lines) throws IOException { | ||
| String content = String.join("\n", lines); | ||
| Files.writeString(path, content); | ||
| } | ||
|
|
||
| public Path getNewPath(Path path, Command command) { | ||
| String markerOfAction = switch (command) { | ||
| case DECRYPT: | ||
| yield " [DECRYPTED].txt"; | ||
| case ENCRYPT: | ||
| yield " [ENCRYPTED].txt"; | ||
| default: | ||
| yield ".txt"; | ||
| }; | ||
|
|
||
| String originalPath = path.toString(); | ||
| String newPath = originalPath.substring(0, originalPath.length() - 4) + markerOfAction; | ||
| return Path.of(newPath); | ||
| } | ||
|
|
||
| public Path getNewPath(Path path, int key) { | ||
| String originalPath = path.toString(); | ||
| String newPath = originalPath.substring(0, originalPath.length() - 4) + " [BRUTE_FORCE] " + key + ".txt"; | ||
| return Path.of(newPath); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package ua.com.javarush.gnew.language; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
|
|
||
| public abstract class Alphabet { | ||
|
|
||
| public static final ArrayList<Character> ENGLISH_UPPERCASE = new ArrayList<>(Arrays.asList( | ||
| 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | ||
| 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z')); | ||
| public static final ArrayList<Character> ENGLISH_LOWERCASE = new ArrayList<>(Arrays.asList( | ||
| 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | ||
| 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')); | ||
|
Comment on lines
+9
to
+14
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. You could get rid of two lists and use only one by processing uppercase letters dynamically. |
||
| public static final ArrayList<String> ENGLISH_COMMON_WORDS = new ArrayList<>(Arrays.asList( | ||
| "the", "be", "to", "of", "and", "in", "that", "have", "for", "not", | ||
| "with", "he", "as", "you", "do", "this", "but", "his", "by", "from", | ||
| "they", "we", "say", "her", "she", "or", "will", "my", "one", "all", | ||
| "would", "there", "their", "what", "so", "up", "out", "if", "about", | ||
| "who", "get", "which", "go", "me", "it", "can", "has", "had", "are", "hello" | ||
| )); | ||
|
|
||
| public static ArrayList<Character> rotateAlphabet(ArrayList<Character> originalAlphabet, int key) { | ||
|
|
||
| ArrayList<Character> rotatedAlphabet = new ArrayList<>(originalAlphabet); | ||
|
|
||
| Collections.rotate(rotatedAlphabet, -key); | ||
|
|
||
| return rotatedAlphabet; | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,4 +51,4 @@ public String toString() { | |
| ", filePath=" + filePath + | ||
| '}'; | ||
| } | ||
| } | ||
| } | ||
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.
.jar file shouldn't be committed. It should be added as a release executable.