diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 51d57b7..8a46461 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -1,10 +1,103 @@ package crypto; -import java.util.Scanner; +import crypto.strategy.CryptContext; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; public class Crypto { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - // write your code here + + 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": + mode = args[i + 1]; + break; + case "-key": + key = Integer.valueOf(args[i + 1]); + break; + case "-data": + data = args[i + 1]; + break; + case "-in": + inputFile = args[i + 1]; + break; + case "-out": + outputFile = args[i + 1]; + break; + case "-alg": + algorithm = args[i + 1]; + break; + } + } + } + + public void start() { + fillParams(); + + if (!inputFile.isEmpty()) { + 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) { + case "enc": + result = cryptContext.encrypt(data); + break; + case "dec": + result = cryptContext.decrypt(data); + break; + default: + result = "Unknown operation!"; + } + } + + private void makeResult() { + if (!outputFile.isEmpty()) { + File file = new File("crypto/" + outputFile); + try (FileWriter fileWriter = new FileWriter(file)) { + fileWriter.write(result); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + System.out.println(result); + } } } 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 new file mode 100644 index 0000000..c7bc3e4 --- /dev/null +++ b/src/crypto/CypherTypes.java @@ -0,0 +1,15 @@ +package crypto; + +import crypto.strategy.CaesarCryptStrategy; +import crypto.strategy.CryptStrategy; + +public enum CypherTypes { + CAESAR { + @Override + public CryptStrategy createCryptStrategy(int key) { + return new CaesarCryptStrategy(key); + } + }; + + public abstract CryptStrategy createCryptStrategy(int key); +} 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/strategy/CaesarCryptStrategy.java b/src/crypto/strategy/CaesarCryptStrategy.java new file mode 100644 index 0000000..62764ea --- /dev/null +++ b/src/crypto/strategy/CaesarCryptStrategy.java @@ -0,0 +1,28 @@ +package crypto.strategy; + +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/strategy/CryptContext.java b/src/crypto/strategy/CryptContext.java new file mode 100644 index 0000000..5a28348 --- /dev/null +++ b/src/crypto/strategy/CryptContext.java @@ -0,0 +1,18 @@ +package crypto.strategy; + +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/strategy/CryptStrategy.java b/src/crypto/strategy/CryptStrategy.java new file mode 100644 index 0000000..3e8be78 --- /dev/null +++ b/src/crypto/strategy/CryptStrategy.java @@ -0,0 +1,6 @@ +package crypto.strategy; + +public interface CryptStrategy { + String encrypt(String input); + String decrypt(String input); +}