From b2162b5c28365f7f3d01e5d82d17d3ee201bcade Mon Sep 17 00:00:00 2001 From: Sensdoo Date: Fri, 28 Dec 2018 17:31:57 +0300 Subject: [PATCH 1/9] add method encryption --- .idea/misc.xml | 2 +- src/crypto/Crypto.java | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index a165cb3..0c4841a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 51d57b7..c1eb664 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -5,6 +5,41 @@ public class Crypto { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - // write your code here + + String state = scanner.nextLine(); + String message = scanner.nextLine(); + int key = scanner.nextInt(); + String result = ""; + + if ("enc".equals(state)) { + result = encryption(message, key); + } + + if ("dec".equals(state)) { + result = decryption(message, key); + } + + System.out.println(result); + } + + public static String encryption(String message, int key) { + String encrypt = ""; + for (int i = 0; i < message.length(); i++) { + int index = message.charAt(i); + + if (index != -1) { + int newIndex = index + key; + char c = (char) newIndex; + encrypt += c; + } else { + encrypt += String.valueOf(index);; + } + } + + return encrypt; + } + + public static String decryption(String message, int key) { + return ""; } } From aa30048d7ab3304ae0c24a94727706b35b078f0e Mon Sep 17 00:00:00 2001 From: Sensdoo Date: Fri, 28 Dec 2018 20:53:00 +0300 Subject: [PATCH 2/9] add method decryption --- src/crypto/Crypto.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index c1eb664..3523c63 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -40,6 +40,19 @@ public static String encryption(String message, int key) { } public static String decryption(String message, int key) { - return ""; + String encrypt = ""; + for (int i = 0; i < message.length(); i++) { + int index = message.charAt(i); + + if (index != -1) { + int newIndex = index - key; + char c = (char) newIndex; + encrypt += c; + } else { + encrypt += String.valueOf(index);; + } + } + + return encrypt; } } From 020586aed01a0b8b89bb6df5ffee7806b7d9a208 Mon Sep 17 00:00:00 2001 From: Sensdoo Date: Fri, 28 Dec 2018 20:58:40 +0300 Subject: [PATCH 3/9] change README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 10b8dd8..55be77a 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,4 @@ Project: Encryption-Decryption -Go to the *src* directory, change *Crypto.java* and put your *.java* file(s) in it. +Added two methods for encryption/description \ No newline at end of file From 4f674a1bc0207a9acbef327927b410ec8bf25685 Mon Sep 17 00:00:00 2001 From: Sensdoo Date: Fri, 28 Dec 2018 23:46:14 +0300 Subject: [PATCH 4/9] add command line args --- src/crypto/Crypto.java | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 3523c63..b4ec9a4 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -5,12 +5,38 @@ public class Crypto { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - - String state = scanner.nextLine(); - String message = scanner.nextLine(); - int key = scanner.nextInt(); + String state = ""; + String message = ""; + int key = 0; String result = ""; + for (int i = 0; i < args.length; i++) { + if ("-mode".equals(args[i])) { + state = args[i + 1]; + i++; + } else if ("-key".equals(args[i])) { + key = Integer.parseInt(args[i + 1]); + i++; + } else if ("-data".equals(args[i])) { + message = args[i + 1]; + i++; + } + } + + if ("".equals(message)) { + System.out.print("Enter text: "); + message = scanner.nextLine(); + } + + if ("".equals(state)) { + state = "enc"; + } + + if (key == 0) { + System.out.print("Enter key: "); + key = scanner.nextInt(); + } + if ("enc".equals(state)) { result = encryption(message, key); } From e2ae2bc35fd17c03eb83cda7f5fb4ec39c663c1c Mon Sep 17 00:00:00 2001 From: Sensdoo Date: Fri, 28 Dec 2018 23:49:24 +0300 Subject: [PATCH 5/9] change README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 55be77a..1214fd9 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,6 @@ Project: Encryption-Decryption -Added two methods for encryption/description \ No newline at end of file +Added two methods for encryption/description + +Command line support added \ No newline at end of file From ff8b6c99e5c3d1f20c5971e838894d87d1dcd1f1 Mon Sep 17 00:00:00 2001 From: Sensdoo Date: Sat, 5 Jan 2019 13:45:04 +0300 Subject: [PATCH 6/9] add work with files --- protected.txt | 1 + road_to_treasure.txt | 1 + src/crypto/Crypto.java | 63 +++++++++++++++++++++++++++++++++++------- 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 protected.txt create mode 100644 road_to_treasure.txt diff --git a/protected.txt b/protected.txt new file mode 100644 index 0000000..b5108eb --- /dev/null +++ b/protected.txt @@ -0,0 +1 @@ +\jqhtrj%yt%m~ujwxpnqq& \ No newline at end of file diff --git a/road_to_treasure.txt b/road_to_treasure.txt new file mode 100644 index 0000000..2e056e2 --- /dev/null +++ b/road_to_treasure.txt @@ -0,0 +1 @@ +Welcome to hyperskill! \ No newline at end of file diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index b4ec9a4..2f63845 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -1,14 +1,21 @@ package crypto; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Scanner; public class Crypto { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - String state = ""; + String state = "enc"; String message = ""; int key = 0; String result = ""; + String in = ""; + String out = ""; for (int i = 0; i < args.length; i++) { if ("-mode".equals(args[i])) { @@ -20,21 +27,34 @@ public static void main(String[] args) { } else if ("-data".equals(args[i])) { message = args[i + 1]; i++; + } else if ("-in".equals(args[i])) { + in = args[i + 1]; + i++; + } else if ("-out".equals(args[i])) { + out = args[i + 1]; + i++; } } - if ("".equals(message)) { - System.out.print("Enter text: "); - message = scanner.nextLine(); + if (key == 0) { + System.out.print("Enter key: "); + key = scanner.nextInt(); } - if ("".equals(state)) { - state = "enc"; + if (!in.equals("")) { + File input = new File(in); + if (input.exists()) { + try { + message = new String(Files.readAllBytes(Paths.get(input.getPath()))); + } catch (IOException e) { + e.printStackTrace(); + } + } } - if (key == 0) { - System.out.print("Enter key: "); - key = scanner.nextInt(); + if (message.equals("")) { + System.out.print("Enter text: "); + message = scanner.nextLine(); } if ("enc".equals(state)) { @@ -45,7 +65,30 @@ public static void main(String[] args) { result = decryption(message, key); } - System.out.println(result); + if (out.equals("")) { + System.out.println(result); + } else { + File output = new File(out); + boolean created = true; + if (!output.exists()) { + try { + created = output.createNewFile(); + } catch (IOException e) { + created = false; + System.out.println("Cannot create the file: " + output.getPath()); + } + } + if (created) { + try { + FileWriter writer = new FileWriter(output); + writer.write(result); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } public static String encryption(String message, int key) { From 846eb4a8e262a2d6f626461a75ca27ec92b71a63 Mon Sep 17 00:00:00 2001 From: Sensdoo Date: Sat, 5 Jan 2019 13:48:46 +0300 Subject: [PATCH 7/9] change README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1214fd9..bc19ad0 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,6 @@ Project: Encryption-Decryption Added two methods for encryption/description -Command line support added \ No newline at end of file +Command line support added + +Work with files added \ No newline at end of file From 4a2bf1a37c16b97e05a16d8cf4a4380cf8d01bd4 Mon Sep 17 00:00:00 2001 From: Sensdoo Date: Sun, 13 Jan 2019 13:35:53 +0300 Subject: [PATCH 8/9] add new aes algorithm --- protected.txt | 2 +- src/crypto/AesCryptoAlgorithm.java | 59 ++++++++++++++++++++++++++ src/crypto/Crypto.java | 53 ++++++----------------- src/crypto/CryptoAlgorithm.java | 4 ++ src/crypto/CryptoAlgorithmFactory.java | 9 ++++ src/crypto/SimpleCryptoAlgorithm.java | 47 ++++++++++++++++++++ 6 files changed, 132 insertions(+), 42 deletions(-) create mode 100644 src/crypto/AesCryptoAlgorithm.java create mode 100644 src/crypto/CryptoAlgorithm.java create mode 100644 src/crypto/CryptoAlgorithmFactory.java create mode 100644 src/crypto/SimpleCryptoAlgorithm.java diff --git a/protected.txt b/protected.txt index b5108eb..7ac36d3 100644 --- a/protected.txt +++ b/protected.txt @@ -1 +1 @@ -\jqhtrj%yt%m~ujwxpnqq& \ No newline at end of file +637E771EB77D5BA14AF2E60452B99CCF88D408880DE930CF81F2E7CD57F6C601 \ No newline at end of file diff --git a/src/crypto/AesCryptoAlgorithm.java b/src/crypto/AesCryptoAlgorithm.java new file mode 100644 index 0000000..9705fc2 --- /dev/null +++ b/src/crypto/AesCryptoAlgorithm.java @@ -0,0 +1,59 @@ +package crypto;import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin; + +import javax.crypto.Cipher; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +public class AesCryptoAlgorithm implements CryptoAlgorithm { + + private static final String SECRET_KEY = "JustSecretString"; + + @Override + public String encryption(String message, String key) { + String encryptedMessage = ""; + try { + if (!isValidKey(key)) { + System.out.println("Error: For the aes algorithm, key length must be 16, 24 or 32 symbols(bytes)"); + } + + IvParameterSpec ivParameter = new IvParameterSpec(SECRET_KEY.getBytes("UTF-8")); + SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); + + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameter); + + byte[] bytes = cipher.doFinal(message.getBytes("UTF-8")); + encryptedMessage = HexBin.encode(bytes); + } catch (Throwable cause) { + System.out.println("Error: " + cause.getMessage()); + } + return encryptedMessage; + } + + @Override + public String decryption(String encryptedMessage, String key) { + String decryptedMessage = ""; + try { + if (!isValidKey(key)) { + System.out.println("Error: For the aes algorithm, key length must be 16, 24 or 32 symbols(bytes)"); + } + + IvParameterSpec ivParameter = new IvParameterSpec(SECRET_KEY.getBytes("UTF-8")); + SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); + + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameter); + + byte[] bytes = cipher.doFinal(HexBin.decode(encryptedMessage)); + decryptedMessage = new String(bytes); + } catch (Throwable cause) { + System.out.println("Error: " + cause.getMessage()); + cause.printStackTrace(); + } + return decryptedMessage; + } + + private static boolean isValidKey(String key) { + return key.length() == 16 || key.length() == 24 || key.length() == 32; + } +} diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 2f63845..8ebcfd8 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -11,8 +11,9 @@ public class Crypto { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String state = "enc"; + String key = ""; + String algName = ""; String message = ""; - int key = 0; String result = ""; String in = ""; String out = ""; @@ -22,7 +23,10 @@ public static void main(String[] args) { state = args[i + 1]; i++; } else if ("-key".equals(args[i])) { - key = Integer.parseInt(args[i + 1]); + key = args[i + 1]; + i++; + }else if ("-alg".equals(args[i])) { + algName = args[i + 1]; i++; } else if ("-data".equals(args[i])) { message = args[i + 1]; @@ -36,9 +40,11 @@ public static void main(String[] args) { } } - if (key == 0) { + CryptoAlgorithm cryptoAlgorithm = CryptoAlgorithmFactory.getAlgorithm(algName); + + if (key.equals("")) { System.out.print("Enter key: "); - key = scanner.nextInt(); + key = scanner.nextLine(); } if (!in.equals("")) { @@ -58,11 +64,11 @@ public static void main(String[] args) { } if ("enc".equals(state)) { - result = encryption(message, key); + result = cryptoAlgorithm.encryption(message, key); } if ("dec".equals(state)) { - result = decryption(message, key); + result = cryptoAlgorithm.decryption(message, key); } if (out.equals("")) { @@ -88,40 +94,5 @@ public static void main(String[] args) { } } } - - } - - public static String encryption(String message, int key) { - String encrypt = ""; - for (int i = 0; i < message.length(); i++) { - int index = message.charAt(i); - - if (index != -1) { - int newIndex = index + key; - char c = (char) newIndex; - encrypt += c; - } else { - encrypt += String.valueOf(index);; - } - } - - return encrypt; - } - - public static String decryption(String message, int key) { - String encrypt = ""; - for (int i = 0; i < message.length(); i++) { - int index = message.charAt(i); - - if (index != -1) { - int newIndex = index - key; - char c = (char) newIndex; - encrypt += c; - } else { - encrypt += String.valueOf(index);; - } - } - - return encrypt; } } diff --git a/src/crypto/CryptoAlgorithm.java b/src/crypto/CryptoAlgorithm.java new file mode 100644 index 0000000..8937b4e --- /dev/null +++ b/src/crypto/CryptoAlgorithm.java @@ -0,0 +1,4 @@ +package crypto;public interface CryptoAlgorithm { + String encryption(String message, String key); + String decryption(String encryptedMessage, String key); +} diff --git a/src/crypto/CryptoAlgorithmFactory.java b/src/crypto/CryptoAlgorithmFactory.java new file mode 100644 index 0000000..a543449 --- /dev/null +++ b/src/crypto/CryptoAlgorithmFactory.java @@ -0,0 +1,9 @@ +package crypto;public class CryptoAlgorithmFactory { + public static CryptoAlgorithm getAlgorithm(String algName) { + if (algName.equals("aes")) { + return new AesCryptoAlgorithm(); + } else { + return new SimpleCryptoAlgorithm(); + } + } +} diff --git a/src/crypto/SimpleCryptoAlgorithm.java b/src/crypto/SimpleCryptoAlgorithm.java new file mode 100644 index 0000000..17cf48b --- /dev/null +++ b/src/crypto/SimpleCryptoAlgorithm.java @@ -0,0 +1,47 @@ +package crypto;public class SimpleCryptoAlgorithm implements CryptoAlgorithm { + + @Override + public String encryption(String message, String key) { + String encrypt = ""; + int secretKey = 0; + try { + secretKey = Integer.parseInt(key); + } catch (NumberFormatException ex) { + System.out.println("Error: For the simple algorithm, the key must be a number"); + } + for (int i = 0; i < message.length(); i++) { + int index = message.charAt(i); + + if (index != -1) { + int newIndex = index + secretKey; + char c = (char) newIndex; + encrypt += c; + } else { + encrypt += String.valueOf(index);; + } + } + return encrypt; } + + @Override + public String decryption(String encryptedMessage, String key) { + String encrypt = ""; + int secretKey = 0; + try { + secretKey = Integer.parseInt(key); + } catch (NumberFormatException ex) { + System.out.println("Error: For the simple algorithm, the key must be a number"); + } + for (int i = 0; i < encryptedMessage.length(); i++) { + int index = encryptedMessage.charAt(i); + + if (index != -1) { + int newIndex = index - secretKey; + char c = (char) newIndex; + encrypt += c; + } else { + encrypt += String.valueOf(index);; + } + } + return encrypt; + } +} From 10c65d572ac4edb5018e5a477cc91e1855ed8481 Mon Sep 17 00:00:00 2001 From: Sensdoo Date: Sun, 13 Jan 2019 13:38:43 +0300 Subject: [PATCH 9/9] change README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bc19ad0..9eff799 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,6 @@ Added two methods for encryption/description Command line support added -Work with files added \ No newline at end of file +Work with files added + +New aes algorithm added (13.01.19) \ No newline at end of file