From 420db5a39977b917a1dd7dcc2dd1a9cbcceb3490 Mon Sep 17 00:00:00 2001 From: Damir Fatkulin Date: Sun, 30 Dec 2018 23:26:10 +0600 Subject: [PATCH 1/5] Implemented encrypt, decrypt methods. --- src/crypto/Crypto.java | 47 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 51d57b7..8bccdb2 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -1,10 +1,51 @@ package crypto; -import java.util.Scanner; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; public class Crypto { + public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - // write your code here + String operation = ""; + String message = ""; + int key = 0; + String result; + + try (BufferedReader bf = new BufferedReader(new InputStreamReader(System.in))) { + operation = bf.readLine(); + message = bf.readLine(); + key = Integer.valueOf(bf.readLine()); + } catch (IOException e) { + e.printStackTrace(); + } + + switch (operation) { + case "enc": + result = encrypt(message, key); + break; + case "dec": + result = decrypt(message, key); + break; + default: + result = "Unknown operation!"; + } + System.out.println(result); + } + + private static String encrypt(String s, int key) { + StringBuilder sb = new StringBuilder(s.length()); + for (char c : s.toCharArray()) { + sb.append((char) (c + key)); + } + return sb.toString(); + } + + private static String decrypt(String s, int key) { + StringBuilder sb = new StringBuilder(s.length()); + for (char c : s.toCharArray()) { + sb.append((char) (c - key)); + } + return sb.toString(); } } From e2b97a3e3c6ab648033b1f81a5214389d2bd36d6 Mon Sep 17 00:00:00 2001 From: Damir Fatkulin Date: Tue, 1 Jan 2019 14:38:57 +0600 Subject: [PATCH 2/5] Added command line args. --- src/crypto/Crypto.java | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 8bccdb2..9e8b162 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -7,15 +7,32 @@ public class Crypto { public static void main(String[] args) { - String operation = ""; + String operation = "enc"; String message = ""; int key = 0; String result; - try (BufferedReader bf = new BufferedReader(new InputStreamReader(System.in))) { - operation = bf.readLine(); - message = bf.readLine(); - key = Integer.valueOf(bf.readLine()); + for (int i = 0; i < args.length; i++) { + switch (args[i]) { + case "-mode": + operation = args[i + 1]; + break; + case "-key": + key = Integer.valueOf(args[i + 1]); + break; + case "-data": + message = args[i + 1]; + break; + } + } + + try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { + if (key == 0) { + key = Integer.valueOf(br.readLine()); + } + if (message.isEmpty()) { + message = br.readLine(); + } } catch (IOException e) { e.printStackTrace(); } From c68975ea3a7706a626dd7f1810f030dd1e29a77c Mon Sep 17 00:00:00 2001 From: Damir Fatkulin Date: Fri, 4 Jan 2019 01:25:27 +0600 Subject: [PATCH 3/5] Added input, output params. --- src/crypto/Crypto.java | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 9e8b162..11f46a0 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -1,8 +1,12 @@ package crypto; import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; +import java.util.Scanner; public class Crypto { @@ -11,6 +15,8 @@ public static void main(String[] args) { String message = ""; int key = 0; String result; + String inputFile = ""; + String outputFile = ""; for (int i = 0; i < args.length; i++) { switch (args[i]) { @@ -23,6 +29,12 @@ public static void main(String[] args) { case "-data": message = args[i + 1]; break; + case "-in": + inputFile = args[i + 1]; + break; + case "-out": + outputFile = args[i + 1]; + break; } } @@ -30,13 +42,27 @@ public static void main(String[] args) { if (key == 0) { key = Integer.valueOf(br.readLine()); } - if (message.isEmpty()) { + if (!inputFile.isEmpty() && !message.isEmpty()) { message = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } + if (!inputFile.isEmpty()) { + File file = new File("/mnt/hgfs/share_vm/IdeaProjects/encryption-decryption/src/crypto/" + + inputFile); + StringBuilder sb = new StringBuilder(); + try (Scanner scanner = new Scanner(file)) { + while (scanner.hasNextLine()) { + sb.append(scanner.nextLine()); + } + } catch (FileNotFoundException e) { + System.out.println("File not found!"); + } + message = sb.toString(); + } + switch (operation) { case "enc": result = encrypt(message, key); @@ -47,7 +73,18 @@ public static void main(String[] args) { default: result = "Unknown operation!"; } - System.out.println(result); + + if (!outputFile.isEmpty()) { + File file = new File("/mnt/hgfs/share_vm/IdeaProjects/encryption-decryption/src/crypto/" + + outputFile); + try (FileWriter fileWriter = new FileWriter(file)) { + fileWriter.write(result); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + System.out.println(result); + } } private static String encrypt(String s, int key) { From b7e7c9616038d98b7b93c37515c67878f79d04fe Mon Sep 17 00:00:00 2001 From: Damir Fatkulin Date: Mon, 7 Jan 2019 00:27:28 +0600 Subject: [PATCH 4/5] Refactored code, added design for new strategies. --- src/crypto/CaesarCryptStrategy.java | 28 +++++++++++++++ src/crypto/CryptContext.java | 18 ++++++++++ src/crypto/CryptStrategy.java | 6 ++++ src/crypto/Crypto.java | 56 ++++++++++++++--------------- src/crypto/CypherTypes.java | 12 +++++++ 5 files changed, 90 insertions(+), 30 deletions(-) create mode 100644 src/crypto/CaesarCryptStrategy.java create mode 100644 src/crypto/CryptContext.java create mode 100644 src/crypto/CryptStrategy.java create mode 100644 src/crypto/CypherTypes.java diff --git a/src/crypto/CaesarCryptStrategy.java b/src/crypto/CaesarCryptStrategy.java new file mode 100644 index 0000000..1816fd1 --- /dev/null +++ b/src/crypto/CaesarCryptStrategy.java @@ -0,0 +1,28 @@ +package crypto; + +public class CaesarCryptStrategy implements CryptStrategy { + + private int key; + + public CaesarCryptStrategy(int key) { + this.key = key; + } + + @Override + public String encrypt(String input) { + StringBuilder sb = new StringBuilder(input.length()); + for (char c : input.toCharArray()) { + sb.append((char) (c + key)); + } + return sb.toString(); + } + + @Override + public String decrypt(String input) { + StringBuilder sb = new StringBuilder(input.length()); + for (char c : input.toCharArray()) { + sb.append((char) (c - key)); + } + return sb.toString(); + } +} diff --git a/src/crypto/CryptContext.java b/src/crypto/CryptContext.java new file mode 100644 index 0000000..5bafc5c --- /dev/null +++ b/src/crypto/CryptContext.java @@ -0,0 +1,18 @@ +package crypto; + +public class CryptContext { + + private final CryptStrategy cryptStrategy; + + public CryptContext(CryptStrategy cryptStrategy) { + this.cryptStrategy = cryptStrategy; + } + + public String encrypt(String input) { + return this.cryptStrategy.encrypt(input); + } + + public String decrypt(String input) { + return this.cryptStrategy.decrypt(input); + } +} diff --git a/src/crypto/CryptStrategy.java b/src/crypto/CryptStrategy.java new file mode 100644 index 0000000..91d1925 --- /dev/null +++ b/src/crypto/CryptStrategy.java @@ -0,0 +1,6 @@ +package crypto; + +public interface CryptStrategy { + String encrypt(String input); + String decrypt(String input); +} diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 11f46a0..4ccda00 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -11,23 +11,24 @@ public class Crypto { public static void main(String[] args) { - String operation = "enc"; - String message = ""; + String mode = "enc"; + String data = ""; int key = 0; String result; String inputFile = ""; String outputFile = ""; + String algorithm = ""; for (int i = 0; i < args.length; i++) { switch (args[i]) { case "-mode": - operation = args[i + 1]; + mode = args[i + 1]; break; case "-key": key = Integer.valueOf(args[i + 1]); break; case "-data": - message = args[i + 1]; + data = args[i + 1]; break; case "-in": inputFile = args[i + 1]; @@ -35,6 +36,9 @@ public static void main(String[] args) { case "-out": outputFile = args[i + 1]; break; + case "-alg": + algorithm = args[i + 1]; + break; } } @@ -42,16 +46,17 @@ public static void main(String[] args) { if (key == 0) { key = Integer.valueOf(br.readLine()); } - if (!inputFile.isEmpty() && !message.isEmpty()) { - message = br.readLine(); + if (!inputFile.isEmpty() && !data.isEmpty()) { + data = br.readLine(); } } catch (IOException e) { e.printStackTrace(); } if (!inputFile.isEmpty()) { - File file = new File("/mnt/hgfs/share_vm/IdeaProjects/encryption-decryption/src/crypto/" - + inputFile); + File file = new File( + "/mnt/hgfs/share_vm/IdeaProjects/encryption-decryption/src/crypto/" + + inputFile); StringBuilder sb = new StringBuilder(); try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { @@ -60,23 +65,30 @@ public static void main(String[] args) { } catch (FileNotFoundException e) { System.out.println("File not found!"); } - message = sb.toString(); + data = sb.toString(); } - switch (operation) { + CryptContext cryptContext = new CryptContext( + CypherTypes.valueOf(algorithm.toUpperCase()).createCryptStrategy(key)); + switch (mode) { case "enc": - result = encrypt(message, key); + result = cryptContext.encrypt(data); break; case "dec": - result = decrypt(message, key); + result = cryptContext.decrypt(data); break; default: result = "Unknown operation!"; } + writeResult(outputFile, result); + } + + private static void writeResult(String outputFile, String result) { if (!outputFile.isEmpty()) { - File file = new File("/mnt/hgfs/share_vm/IdeaProjects/encryption-decryption/src/crypto/" - + outputFile); + File file = new File( + "/mnt/hgfs/share_vm/IdeaProjects/encryption-decryption/src/crypto/" + + outputFile); try (FileWriter fileWriter = new FileWriter(file)) { fileWriter.write(result); } catch (IOException e) { @@ -86,20 +98,4 @@ public static void main(String[] args) { System.out.println(result); } } - - private static String encrypt(String s, int key) { - StringBuilder sb = new StringBuilder(s.length()); - for (char c : s.toCharArray()) { - sb.append((char) (c + key)); - } - return sb.toString(); - } - - private static String decrypt(String s, int key) { - StringBuilder sb = new StringBuilder(s.length()); - for (char c : s.toCharArray()) { - sb.append((char) (c - key)); - } - return sb.toString(); - } } diff --git a/src/crypto/CypherTypes.java b/src/crypto/CypherTypes.java new file mode 100644 index 0000000..5dd2d10 --- /dev/null +++ b/src/crypto/CypherTypes.java @@ -0,0 +1,12 @@ +package crypto; + +public enum CypherTypes { + CAESAR { + @Override + public CryptStrategy createCryptStrategy(int key) { + return new CaesarCryptStrategy(key); + } + }; + + public abstract CryptStrategy createCryptStrategy(int key); +} From 508cb514aecabb240dc6085c86d857ef50f00627 Mon Sep 17 00:00:00 2001 From: Damir Fatkulin Date: Mon, 14 Jan 2019 11:27:55 +0600 Subject: [PATCH 5/5] Refactored code. --- src/crypto/Crypto.java | 82 ++++++++++--------- src/crypto/CryptoUtility.java | 33 ++++++++ src/crypto/CypherTypes.java | 3 + src/crypto/Main.java | 11 +++ .../{ => strategy}/CaesarCryptStrategy.java | 2 +- src/crypto/{ => strategy}/CryptContext.java | 2 +- src/crypto/{ => strategy}/CryptStrategy.java | 2 +- 7 files changed, 92 insertions(+), 43 deletions(-) create mode 100644 src/crypto/CryptoUtility.java create mode 100644 src/crypto/Main.java rename src/crypto/{ => strategy}/CaesarCryptStrategy.java (96%) rename src/crypto/{ => strategy}/CryptContext.java (93%) rename src/crypto/{ => strategy}/CryptStrategy.java (80%) diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 4ccda00..8a46461 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -1,24 +1,33 @@ package crypto; -import java.io.BufferedReader; +import crypto.strategy.CryptContext; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; -import java.io.InputStreamReader; -import java.util.Scanner; public class Crypto { - public static void main(String[] args) { - String mode = "enc"; - String data = ""; - int key = 0; - String result; - String inputFile = ""; - String outputFile = ""; - String algorithm = ""; + private String mode = "enc"; + private String data = ""; + private int key; + private String result; + private String outputFile = ""; + private String algorithm; + private String inputFile = ""; + public Crypto(String mode, String data, int key, String outputFile, String algorithm) { + this.mode = mode; + this.data = data; + this.key = key; + this.outputFile = outputFile; + this.algorithm = algorithm; + } + + public Crypto() { + + } + + public void invoke(String[] args) { for (int i = 0; i < args.length; i++) { switch (args[i]) { case "-mode": @@ -41,33 +50,30 @@ public static void main(String[] args) { break; } } + } - try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { - if (key == 0) { - key = Integer.valueOf(br.readLine()); - } - if (!inputFile.isEmpty() && !data.isEmpty()) { - data = br.readLine(); - } - } catch (IOException e) { - e.printStackTrace(); - } + public void start() { + fillParams(); if (!inputFile.isEmpty()) { - File file = new File( - "/mnt/hgfs/share_vm/IdeaProjects/encryption-decryption/src/crypto/" - + inputFile); - StringBuilder sb = new StringBuilder(); - try (Scanner scanner = new Scanner(file)) { - while (scanner.hasNextLine()) { - sb.append(scanner.nextLine()); - } - } catch (FileNotFoundException e) { - System.out.println("File not found!"); - } - data = sb.toString(); + data = CryptoUtility.readFromFile(inputFile); } + process(); + + makeResult(); + } + + private void fillParams() { + if (key == 0) { + key = Integer.valueOf(CryptoUtility.getUserInput("key")); + } + if (inputFile.isEmpty() && data.isEmpty()) { + data = CryptoUtility.getUserInput("message for encryption"); + } + } + + private void process() { CryptContext cryptContext = new CryptContext( CypherTypes.valueOf(algorithm.toUpperCase()).createCryptStrategy(key)); switch (mode) { @@ -80,15 +86,11 @@ public static void main(String[] args) { default: result = "Unknown operation!"; } - - writeResult(outputFile, result); } - private static void writeResult(String outputFile, String result) { + private void makeResult() { if (!outputFile.isEmpty()) { - File file = new File( - "/mnt/hgfs/share_vm/IdeaProjects/encryption-decryption/src/crypto/" - + outputFile); + File file = new File("crypto/" + outputFile); try (FileWriter fileWriter = new FileWriter(file)) { fileWriter.write(result); } catch (IOException e) { diff --git a/src/crypto/CryptoUtility.java b/src/crypto/CryptoUtility.java new file mode 100644 index 0000000..e34fbf9 --- /dev/null +++ b/src/crypto/CryptoUtility.java @@ -0,0 +1,33 @@ +package crypto; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Scanner; + +public final class CryptoUtility { + + private CryptoUtility() { + + } + + public static String readFromFile(final String inputFile) { + final File file = new File("crypto/" + inputFile); + final StringBuilder stringBuilder = new StringBuilder(); + try (Scanner scanner = new Scanner(file)) { + while (scanner.hasNextLine()) { + stringBuilder.append(scanner.nextLine()); + } + } catch (FileNotFoundException e) { + System.out.println("Input file not found!"); + } + return stringBuilder.toString(); + } + + public static String getUserInput(final String message) { + String userInput; + Scanner scanner = new Scanner(System.in); + System.out.printf("Input %s: ", message); + userInput = scanner.nextLine(); + return userInput; + } +} diff --git a/src/crypto/CypherTypes.java b/src/crypto/CypherTypes.java index 5dd2d10..c7bc3e4 100644 --- a/src/crypto/CypherTypes.java +++ b/src/crypto/CypherTypes.java @@ -1,5 +1,8 @@ package crypto; +import crypto.strategy.CaesarCryptStrategy; +import crypto.strategy.CryptStrategy; + public enum CypherTypes { CAESAR { @Override diff --git a/src/crypto/Main.java b/src/crypto/Main.java new file mode 100644 index 0000000..58a0b78 --- /dev/null +++ b/src/crypto/Main.java @@ -0,0 +1,11 @@ +package crypto; + +public class Main { + + public static void main(String[] args) { + + final Crypto crypto = new Crypto(); + crypto.invoke(args); + crypto.start(); + } +} diff --git a/src/crypto/CaesarCryptStrategy.java b/src/crypto/strategy/CaesarCryptStrategy.java similarity index 96% rename from src/crypto/CaesarCryptStrategy.java rename to src/crypto/strategy/CaesarCryptStrategy.java index 1816fd1..62764ea 100644 --- a/src/crypto/CaesarCryptStrategy.java +++ b/src/crypto/strategy/CaesarCryptStrategy.java @@ -1,4 +1,4 @@ -package crypto; +package crypto.strategy; public class CaesarCryptStrategy implements CryptStrategy { diff --git a/src/crypto/CryptContext.java b/src/crypto/strategy/CryptContext.java similarity index 93% rename from src/crypto/CryptContext.java rename to src/crypto/strategy/CryptContext.java index 5bafc5c..5a28348 100644 --- a/src/crypto/CryptContext.java +++ b/src/crypto/strategy/CryptContext.java @@ -1,4 +1,4 @@ -package crypto; +package crypto.strategy; public class CryptContext { diff --git a/src/crypto/CryptStrategy.java b/src/crypto/strategy/CryptStrategy.java similarity index 80% rename from src/crypto/CryptStrategy.java rename to src/crypto/strategy/CryptStrategy.java index 91d1925..3e8be78 100644 --- a/src/crypto/CryptStrategy.java +++ b/src/crypto/strategy/CryptStrategy.java @@ -1,4 +1,4 @@ -package crypto; +package crypto.strategy; public interface CryptStrategy { String encrypt(String input);