-
Notifications
You must be signed in to change notification settings - Fork 16
File manager #6
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?
File manager #6
Changes from all commits
1e9fc76
73acc24
669718e
a5e3655
f5bae0d
0f84392
3f70aa7
4a9bb0a
f1d2963
1fcb116
394dc8e
7cbaed9
11f85e1
8e8802b
22e85c0
a38b283
088a0ad
a0b615e
0b55418
ff5bc0f
5fdc378
e09162c
09c3e3f
1f0d4ce
c149462
3de15ef
4b98ac2
b394db7
a889546
5159e5f
c338abd
d901259
470fc65
2d962af
93e7b3e
9787aa0
0c572b1
1f39f43
b4e9b9f
bfc585a
90fb29c
fd3a038
2f77de2
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 |
|---|---|---|
| @@ -1,28 +1,60 @@ | ||
| # CAESAR CIPHER | ||
| author Serhii Lahuta | ||
|
|
||
| ## Run the program | ||
| # ENGLISH | ||
| ### Run the program | ||
|
|
||
| ### Commands: | ||
| #### Commands: | ||
|
|
||
| ``` | ||
| -e Encrypt | ||
| -d Decrypt | ||
| -bf Brute force | ||
| ``` | ||
|
|
||
| ### Arguments: | ||
| #### Arguments: | ||
| ``` | ||
| -k Key | ||
| -f File path | ||
| ``` | ||
|
|
||
| ### Example: | ||
| #### 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 | ||
| ``` | ||
|
|
||
| ### Argument could be in any order | ||
| #### Argument could be in any order | ||
| ``` | ||
| -e -f "/path/to/file.txt" -k 1 | ||
| ``` | ||
|
|
||
| # УКРАЇНСЬКА | ||
| ### Запуск програми | ||
|
|
||
| #### Команди: | ||
|
|
||
| ``` | ||
| -e Зашифрувати | ||
| -d Розшифрувати | ||
| -bf Підбір коду | ||
| ``` | ||
|
|
||
| #### Аргуметни: | ||
| ``` | ||
| -k Ключ | ||
| -f Шлях до файлу | ||
| ``` | ||
|
|
||
| #### Приклад: | ||
| ``` | ||
| -e -k 1 -f "/path/to/file.txt" - Зашифрувати файл з ключем 1 | ||
| -d -k 5 -f "/path/to/file [ENCRYPTED].txt" - Розшифрувати файл з ключем 5 | ||
| -bf -f "/path/to/file [ENCRYPTED].txt" - Розшифрувати файл методом підбору ключа | ||
| ``` | ||
|
|
||
| #### Аргументи можуть бути у будь-якому порядку | ||
| ``` | ||
| -e -f "/path/to/file.txt" -k 1 | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,39 @@ | ||
| package ua.com.javarush.gnew; | ||
|
|
||
| import ua.com.javarush.gnew.crypto.Cypher; | ||
| import ua.com.javarush.gnew.crypto.Cryptor; | ||
| import ua.com.javarush.gnew.crypto.KeyManager; | ||
| import ua.com.javarush.gnew.file.FileManager; | ||
| import ua.com.javarush.gnew.file.NewFileNamePath; | ||
| 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; | ||
|
|
||
| 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); | ||
| KeyManager keyManager = new KeyManager(); | ||
|
|
||
| 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"; | ||
|
|
||
| Path newFilePath = runOptions.getFilePath().resolveSibling(newFileName); | ||
| fileManager.write(newFilePath, encryptedContent); | ||
| String content = fileManager.read(runOptions.getFilePath()); | ||
| Cryptor cryptor; | ||
| if(runOptions.getCommand() == Command.ENCRYPT || runOptions.getCommand() == Command.DECRYPT){ | ||
| int key = keyManager.getKey(runOptions); | ||
| cryptor = new Cryptor(content, key); | ||
| String cryptoContent = cryptor.cypher(); | ||
| NewFileNamePath path = new NewFileNamePath(); | ||
| fileManager.write(path.newPath(runOptions) , cryptoContent); | ||
| } else if (runOptions.getCommand() == Command.BRUTEFORCE) { | ||
| cryptor = new Cryptor(content, keyManager.keySelection(runOptions)); | ||
| String cryptoContent = cryptor.cypher(); | ||
| NewFileNamePath path = new NewFileNamePath(); | ||
| fileManager.write(path.newPath(runOptions) , cryptoContent); | ||
| } | ||
| } catch (Exception e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package ua.com.javarush.gnew.crypto; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
|
|
||
| public enum ConstantsForCryptor { | ||
| ALPHABET(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'))), | ||
| PUNCTUATION (new ArrayList<>(Arrays.asList(' ', ',', '!', '?', ':', ';', '-', | ||
| '(', ')', '[', ']', '{', '}','#', '.', '@', '*', '+', '=', '_', '&', | ||
| '\\', '/', '₴', '~', '`'))); | ||
|
|
||
| private final ArrayList<Character> CHARS_ARRAY_CONSTANT; | ||
|
|
||
| ConstantsForCryptor(ArrayList<Character> CHARS_ARRAY_CONSTANS) { | ||
| this.CHARS_ARRAY_CONSTANT = CHARS_ARRAY_CONSTANS; | ||
| } | ||
| public ArrayList<Character> getCharsArrayConstant() { | ||
| return this.CHARS_ARRAY_CONSTANT; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package ua.com.javarush.gnew.crypto; | ||
|
|
||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class Cryptor { | ||
|
|
||
| private String input ; | ||
| private int key; | ||
|
|
||
| public Cryptor(String input, int key){ | ||
| this.input =input ; | ||
| this.key = key; | ||
| } | ||
|
|
||
|
|
||
| public Cryptor(String input){ | ||
| this.input = input; | ||
| } | ||
|
|
||
| public String cypher() { | ||
|
|
||
| Rotator rotator = new Rotator(); | ||
|
|
||
| char[] charsArray = input.toCharArray(); | ||
|
|
||
| StringBuilder sb = new StringBuilder(); | ||
| SymbolsBelonging symBel = new SymbolsBelonging(); | ||
| for(char symbol : charsArray) { | ||
| if(ConstantsForCryptor.ALPHABET.getCharsArrayConstant().contains(Character.toUpperCase(symbol))){ | ||
| sb.append(symBel.symbolsBelongingABC(symbol, rotator.rotateAlphabet(key))); | ||
| }else if (ConstantsForCryptor.PUNCTUATION.getCharsArrayConstant().contains(symbol)) { | ||
| sb.append(symBel.symbolsBelongingPUNCTUATION(symbol, rotator.rotatePunctuation(key))); | ||
| }else { | ||
| sb.append(symbol); | ||
| } | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| public char MostFrequentChar(){ | ||
|
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 the approach |
||
|
|
||
| char[] charsArray = input.toCharArray(); | ||
|
|
||
| Map<Character, Integer> frequencyMap = new HashMap<>(); | ||
| for(char symbol : charsArray){ | ||
| frequencyMap.put(symbol, frequencyMap.getOrDefault(symbol, 0) + 1); | ||
| } | ||
|
|
||
| char mostFrequentChar = '\0'; | ||
| int maxFrequency = 0; | ||
|
|
||
| for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) { | ||
| if (entry.getValue() > maxFrequency) { | ||
| mostFrequentChar = entry.getKey(); | ||
| maxFrequency = entry.getValue(); | ||
| } | ||
| } | ||
| return mostFrequentChar; | ||
| } | ||
|
|
||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package ua.com.javarush.gnew.crypto; | ||
|
|
||
| import ua.com.javarush.gnew.file.FileManager; | ||
| import ua.com.javarush.gnew.runner.RunOptions; | ||
| import ua.com.javarush.gnew.runner.Command; | ||
|
|
||
|
|
||
| public class KeyManager { | ||
|
|
||
| public int getKey(RunOptions runOptions){ | ||
| if (runOptions.getCommand() == Command.ENCRYPT) { | ||
| return (runOptions.getKey() % 26) * -1; | ||
| }else if (runOptions.getCommand() == Command.DECRYPT) { | ||
| return runOptions.getKey() % 26; | ||
| } else if (runOptions.getCommand() == Command.BRUTEFORCE) { | ||
| return keySelection(runOptions); | ||
| }else { | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| public int keySelection(RunOptions runOptions) { | ||
| FileManager fileManager = new FileManager(); | ||
| String input =fileManager.read(runOptions.getFilePath()); | ||
| Cryptor cryptor = new Cryptor(input); | ||
| return ConstantsForCryptor.PUNCTUATION.getCharsArrayConstant().indexOf(cryptor.MostFrequentChar()); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package ua.com.javarush.gnew.crypto; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
|
|
||
| public class Rotator { | ||
|
|
||
| protected ArrayList<Character> rotateAlphabet(int key){ | ||
|
|
||
|
|
||
| ArrayList<Character> rotateAlphabet = new ArrayList<>(ConstantsForCryptor.ALPHABET.getCharsArrayConstant()); | ||
| Collections.rotate(rotateAlphabet, key); | ||
| return rotateAlphabet; | ||
| } | ||
|
|
||
| protected ArrayList<Character> rotatePunctuation(Integer key){ | ||
| ArrayList<Character> rotatePunctuation = new ArrayList<>(ConstantsForCryptor.PUNCTUATION.getCharsArrayConstant()); | ||
| Collections.rotate(rotatePunctuation, key); | ||
| return rotatePunctuation; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package ua.com.javarush.gnew.crypto; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class SymbolsBelonging { | ||
| protected Character symbolsBelongingABC(char symbol, ArrayList<Character> rotateAlphabet) { | ||
| int index = ConstantsForCryptor.ALPHABET.getCharsArrayConstant().indexOf(Character.toUpperCase(symbol)); | ||
| if(Character.isLowerCase(symbol)){ | ||
| return Character.toLowerCase(rotateAlphabet.get(index)); | ||
| } | ||
| return rotateAlphabet.get(index); | ||
| } | ||
|
|
||
| protected Character symbolsBelongingPUNCTUATION(char symbol, ArrayList<Character> rotatePunctuation){ | ||
| int index = ConstantsForCryptor.PUNCTUATION.getCharsArrayConstant().indexOf(symbol); | ||
| return rotatePunctuation.get(index); | ||
| } | ||
| } | ||
|
Comment on lines
+5
to
+18
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. Actually we already have similar method. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package ua.com.javarush.gnew.file; | ||
|
|
||
| import ua.com.javarush.gnew.crypto.KeyManager; | ||
| import ua.com.javarush.gnew.runner.Command; | ||
| import ua.com.javarush.gnew.runner.RunOptions; | ||
|
|
||
| public class FileNameTag { | ||
|
|
||
| protected String fileNameNotExtension(RunOptions runOptions){ | ||
| String fileName = runOptions.getFilePath().getFileName().toString(); | ||
| int lastDotIndex = fileName.lastIndexOf('.'); | ||
| return fileName.substring(0, lastDotIndex); | ||
| } | ||
|
|
||
| protected String nameTag(RunOptions runOptions){ | ||
| StringBuilder tag = new StringBuilder(); | ||
| if(runOptions.getCommand() == Command.ENCRYPT){ | ||
| tag.append("[ENCRYPTED]"); | ||
| } else if (runOptions.getCommand() == Command.DECRYPT) { | ||
| tag.append("[DECRYPTED]"); | ||
| } else if (runOptions.getCommand() == Command.BRUTEFORCE) { | ||
| tag.append("[DECRYPTED] [WITH KEY "); | ||
| KeyManager keyManager = new KeyManager(); | ||
| tag.append(keyManager.getKey(runOptions)); | ||
| tag.append("]"); | ||
| } | ||
| return tag.toString(); | ||
| } | ||
|
|
||
| protected String fileNameTag(RunOptions runOptions){ | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append(fileNameNotExtension(runOptions)); | ||
| sb.append(nameTag(runOptions)); | ||
| return sb.toString() + ".txt"; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package ua.com.javarush.gnew.file; | ||
|
|
||
| import ua.com.javarush.gnew.runner.RunOptions; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class NewFileNamePath { | ||
|
|
||
| public Path newPath(RunOptions runOptions) { | ||
| FileNameTag tag = new FileNameTag(); | ||
| return Path.of(tag.fileNameTag(runOptions)); | ||
| } | ||
| } |
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.
Contains duplicates. See here how to get rid of it.