From c9f56b2b5feaaa64f3f41b496959c3b442d606df Mon Sep 17 00:00:00 2001 From: ShilovSetCorp Date: Mon, 4 Feb 2019 16:09:26 +0300 Subject: [PATCH 1/5] Encryption-Decryption --- .idea/misc.xml | 2 +- src/crypto/Crypto.java | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index a165cb3..b5b1202 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..4e92d91 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -5,6 +5,17 @@ public class Crypto { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - // write your code here + String operation = scanner.nextLine(); + String mess = scanner.nextLine(); + int key = scanner.nextInt(); + StringBuilder sb = new StringBuilder(mess); + for (int i = 0; i < sb.length(); i++) { + if(operation.equals("enc")) { + sb.setCharAt(i, (char) (sb.charAt(i) + key)); + }else{ + sb.setCharAt(i, (char) (sb.charAt(i) - key)); + } + } + System.out.println(sb); } } From 8995c042a11c27e744d210eba9848f99a624fa88 Mon Sep 17 00:00:00 2001 From: ShilovSetCorp Date: Mon, 4 Feb 2019 16:34:08 +0300 Subject: [PATCH 2/5] Encryption-Decryption with command line args --- src/crypto/Crypto.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 4e92d91..349b25a 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -4,10 +4,16 @@ public class Crypto { public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - String operation = scanner.nextLine(); - String mess = scanner.nextLine(); - int key = scanner.nextInt(); + int key = 0; + String operation = "enc"; + if(args[0].equals("-mode")){ + operation = args[1]; + } + if (args[2].equals("-key")){ + key = Integer.parseInt(args[3]); + } + String mess = args[args.length - 1]; + StringBuilder sb = new StringBuilder(mess); for (int i = 0; i < sb.length(); i++) { if(operation.equals("enc")) { From d3649615c6858752aac437e4a15c3b3d5200a32b Mon Sep 17 00:00:00 2001 From: ShilovSetCorp Date: Tue, 5 Feb 2019 09:59:57 +0300 Subject: [PATCH 3/5] file read and write added --- src/crypto/Crypto.java | 59 ++++++++++++++++++++++++++++++++++------ src/road_to_treasure.txt | 1 + 2 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 src/road_to_treasure.txt diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 349b25a..66054dc 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -1,20 +1,56 @@ package crypto; + +import java.io.*; import java.util.Scanner; public class Crypto { - public static void main(String[] args) { + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); int key = 0; + String mess = ""; String operation = "enc"; - if(args[0].equals("-mode")){ - operation = args[1]; - } - if (args[2].equals("-key")){ - key = Integer.parseInt(args[3]); - } - String mess = args[args.length - 1]; + String fileIn = ""; + String fileOut = ""; + if(args.length == 6) { + if (args[0].equals("-mode")) { + operation = args[1]; + } + if (args[2].equals("-key")) { + key = Integer.parseInt(args[3]); + } + mess = args[args.length - 1]; + }else if(args.length == 8) { + if (args[0].equals("-mode")) { + operation = args[1]; + } + if (args[2].equals("-key")) { + key = Integer.parseInt(args[3]); + } + mess = args[args.length - 1]; + if (args[4].equals("-in")) { + fileIn = args[5]; + } + if (args[6].equals("-out")) { + fileOut = args[7]; + } + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(new File(fileIn))); + mess = br.readLine(); + br.close(); + } catch (IOException e) { + System.out.println("Input file doesn't exist"); + e.printStackTrace(); + + } + }else { + mess = sc.nextLine(); + key = sc.nextInt(); + } StringBuilder sb = new StringBuilder(mess); + for (int i = 0; i < sb.length(); i++) { if(operation.equals("enc")) { sb.setCharAt(i, (char) (sb.charAt(i) + key)); @@ -22,6 +58,13 @@ public static void main(String[] args) { sb.setCharAt(i, (char) (sb.charAt(i) - key)); } } + try(FileWriter fw = new FileWriter(new File(fileOut))) { + fw.write(sb.toString()); + fw.flush(); + } catch (IOException e) { + System.out.println("Output file name was not found"); + e.printStackTrace(); + } System.out.println(sb); } } diff --git a/src/road_to_treasure.txt b/src/road_to_treasure.txt new file mode 100644 index 0000000..3afbdf2 --- /dev/null +++ b/src/road_to_treasure.txt @@ -0,0 +1 @@ +hyperskill \ No newline at end of file From 0c19eb1cb04fc074d9379826c071cea6e26dad64 Mon Sep 17 00:00:00 2001 From: ShilovSetCorp Date: Tue, 5 Feb 2019 10:08:34 +0300 Subject: [PATCH 4/5] file read and write added --- src/crypto/Crypto.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 66054dc..6a0b04b 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -16,6 +16,7 @@ public static void main(String[] args){ if (args[0].equals("-mode")) { operation = args[1]; } + if (args[2].equals("-key")) { key = Integer.parseInt(args[3]); } From 7022bf3d6c5251110197239f4a0974779d64dc9a Mon Sep 17 00:00:00 2001 From: ShilovSetCorp Date: Tue, 5 Feb 2019 11:55:39 +0300 Subject: [PATCH 5/5] different algorithms added with interfaces --- src/crypto/Crypto.java | 141 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 125 insertions(+), 16 deletions(-) diff --git a/src/crypto/Crypto.java b/src/crypto/Crypto.java index 6a0b04b..2584931 100644 --- a/src/crypto/Crypto.java +++ b/src/crypto/Crypto.java @@ -12,6 +12,7 @@ public static void main(String[] args){ String operation = "enc"; String fileIn = ""; String fileOut = ""; + Encoderable encoder = null; if(args.length == 6) { if (args[0].equals("-mode")) { operation = args[1]; @@ -21,19 +22,26 @@ public static void main(String[] args){ key = Integer.parseInt(args[3]); } mess = args[args.length - 1]; - }else if(args.length == 8) { + }else if(args.length == 10) { if (args[0].equals("-mode")) { operation = args[1]; } - if (args[2].equals("-key")) { - key = Integer.parseInt(args[3]); + if(args[2].equals("-alg")){ + if(args[3].equals("morse")){ + encoder = new Morse(); + }else if(args[3].equals("std")){ + encoder = new Stand(); + } + } + if (args[4].equals("-key")) { + key = Integer.parseInt(args[5]); } mess = args[args.length - 1]; - if (args[4].equals("-in")) { - fileIn = args[5]; + if (args[6].equals("-in")) { + fileIn = args[7]; } - if (args[6].equals("-out")) { - fileOut = args[7]; + if (args[8].equals("-out")) { + fileOut = args[9]; } BufferedReader br = null; try { @@ -50,7 +58,105 @@ public static void main(String[] args){ mess = sc.nextLine(); key = sc.nextInt(); } - StringBuilder sb = new StringBuilder(mess); + String res = encoder.encode(mess); + try(FileWriter fw = new FileWriter(new File(fileOut))) { + fw.write(res); + fw.flush(); + } catch (IOException e) { + System.out.println("Output file name was not found"); + e.printStackTrace(); + } + System.out.println(res); + } +} + +interface Encoderable{ + String encode(String s); + } + +class Morse implements Encoderable{ + + static String morseEncode(char x){ + switch (x) + { + case 'a': + return ".-"; + case 'b': + return "-..."; + case 'c': + return "-.-."; + case 'd': + return "-.."; + case 'e': + return "."; + case 'f': + return "..-."; + case 'g': + return "--."; + case 'h': + return "...."; + case 'i': + return ".."; + case 'j': + return ".---"; + case 'k': + return "-.-"; + case 'l': + return ".-.."; + case 'm': + return "--"; + case 'n': + return "-."; + case 'o': + return "---"; + case 'p': + return ".--."; + case 'q': + return "--.-"; + case 'r': + return ".-."; + case 's': + return "..."; + case 't': + return "-"; + case 'u': + return "..-"; + case 'v': + return "...-"; + case 'w': + return ".--"; + case 'x': + return "-..-"; + case 'y': + return "-.--"; + // for space + case 'z': + return "--.."; + } + return ""; + } + + static String morseCode(String s) + { + // character by character print + // Morse code + StringBuilder enc = new StringBuilder(); + for (int i = 0;i