-
Notifications
You must be signed in to change notification settings - Fork 16
Roman Tsaruk #12
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
RomanTsaruk
wants to merge
9
commits into
JavaRush-GNEW:main
Choose a base branch
from
RomanTsaruk: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
Roman Tsaruk #12
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d434216
test
RomanTsaruk e82114b
start a project
RomanTsaruk 5580191
added decrypt method
RomanTsaruk e807016
added bruteForce method
RomanTsaruk a68162b
added PathManager
RomanTsaruk 6799897
added readme
RomanTsaruk ec36d7b
added jar
RomanTsaruk c74dff6
Update readme.md
RomanTsaruk 2372f92
removed unused lines
RomanTsaruk 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
| 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 | ||
|
|
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,15 @@ | ||
| The sun was setting over the quiet village, | ||
| casting a warm golden glow on the cobblestone streets. | ||
| Birds chirped softly, preparing for the night as the first stars appeared in the sky. | ||
| People were slowly closing their shops, exchanging friendly smiles and goodbyes. | ||
| Children played in the park, their laughter echoing through the air. In the distance, | ||
| the gentle sound of a river flowing added to the serene atmosphere. | ||
| A couple walked hand in hand, enjoying the cool evening breeze. | ||
| Life in the village was peaceful, with a comforting sense of community that made everyone feel at home. | ||
| In a quiet village nestled between rolling hills and dense forests, life moved at a gentle pace. | ||
| Every morning, the sun would rise slowly over the horizon, casting a warm golden glow across the fields. | ||
| The villagers, friendly and hardworking, went about their daily routines with a sense of contentment. | ||
| Children played near the stream, their laughter echoing through the trees. | ||
| Elderly folks shared stories of olden days on the benches under the large oak tree. | ||
| As evening approached, the village would gather around the bonfire, enjoying the simple pleasures of life, | ||
| and the stars would shine brightly in the clear night sky. |
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
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,15 +1,14 @@ | ||
| package ua.com.javarush.gnew.crypto; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.*; | ||
|
|
||
| public class Cypher { | ||
| public static int key; | ||
| private final ArrayList<Character> originalAlphabet = 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', '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 String encrypt(String input, int key) { | ||
| key = Math.negateExact(key); | ||
| key = -key; | ||
|
|
||
| ArrayList<Character> rotatedAlphabet = new ArrayList<>(originalAlphabet); | ||
| Collections.rotate(rotatedAlphabet, key); | ||
|
|
@@ -22,6 +21,46 @@ public String encrypt(String input, int key) { | |
| return builder.toString(); | ||
| } | ||
|
|
||
| public String decrypt(String input, int key) { | ||
| return encrypt(input, -key); | ||
| } | ||
|
|
||
| public String bruteForce(String input) { | ||
| int maxCount = 0; | ||
| int key = 0; | ||
| String bestOutput =""; | ||
| HashSet<String> engWords = new HashSet<>(List.of("are", "not", "am", "get", "do", "the", "be", "and", "to", "of", "have", "you", "at", "for", "was")); | ||
|
|
||
|
|
||
| for (int i = 1; i <= 25; i++) { | ||
| ArrayList<Character> rotatedAlphabet = new ArrayList<>(originalAlphabet); | ||
| Collections.rotate(rotatedAlphabet, i); | ||
| char[] charArray = input.toCharArray(); | ||
|
|
||
| StringBuilder builder = new StringBuilder(); | ||
| for (char symbol : charArray) { | ||
| builder.append(processSymbol(symbol, rotatedAlphabet)); | ||
| } | ||
| String output = builder.toString(); | ||
|
|
||
| int count = 0; | ||
| String[] words = output.split("\\s+"); | ||
| for (String word : words) { | ||
| if (engWords.contains(word.toLowerCase())) { | ||
| count++; | ||
| } | ||
| } | ||
|
|
||
| if (count > maxCount) { | ||
| maxCount = count; | ||
| key = i; | ||
| bestOutput = output; | ||
| } | ||
| } | ||
| Cypher.key = key; | ||
| return bestOutput; | ||
|
Comment on lines
+35
to
+61
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 code looks a little too complex. It might be simplified and split into methods. See more here |
||
| } | ||
|
|
||
| private Character processSymbol(char symbol, ArrayList<Character> rotatedAlphabet) { | ||
| if (!originalAlphabet.contains(symbol)) { | ||
| return symbol; | ||
|
|
||
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,15 +1,26 @@ | ||
| package ua.com.javarush.gnew.file; | ||
|
|
||
| import ua.com.javarush.gnew.runner.RunOptions; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class FileManager { | ||
| public String read(Path filePath) throws IOException { | ||
| return Files.readString(filePath); | ||
|
|
||
| public String read(Path filePath) { | ||
| try { | ||
| return Files.readString(filePath); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| public void write(Path filePath, String content) throws IOException { | ||
| Files.writeString(filePath, content); | ||
| public void write(Path filePath, String content) { | ||
| try { | ||
| Files.writeString(filePath, content); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } |
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,26 @@ | ||
| package ua.com.javarush.gnew.file; | ||
|
|
||
| import ua.com.javarush.gnew.crypto.Cypher; | ||
| import ua.com.javarush.gnew.runner.RunOptions; | ||
|
|
||
| import java.nio.file.Path; | ||
|
|
||
| public class PathManager { | ||
| public Path newFilePath(RunOptions runOptions) { | ||
| String label = switch (runOptions.getCommand()) { | ||
| case ENCRYPT -> " [ENCRYPTED]"; | ||
| case DECRYPT -> " [DECRYPTED]"; | ||
| case BRUTEFORCE -> " [BRUTEFORCE]"; | ||
| default -> null; | ||
| }; | ||
|
Comment on lines
+10
to
+15
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 solution. |
||
|
|
||
| String fileName = runOptions.getFilePath().getFileName().toString(); | ||
| StringBuilder newFileName = new StringBuilder(fileName.substring(0, fileName.length() - 4)); | ||
| newFileName.append(label); | ||
| if (label.equals(" [BRUTEFORCE]")) { | ||
| newFileName.append(" key ").append(Cypher.key); | ||
| } | ||
| newFileName.append(".txt"); | ||
| return runOptions.getFilePath().resolveSibling(newFileName.toString()); | ||
| } | ||
| } | ||
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
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.
This code contains duplicates. See here how to fix it.