|
| 1 | +package encryptdecrypt; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + public static void main(String[] args) { |
| 8 | + for (int i = 0; i < args.length; i += 2) { |
| 9 | + switch (args[i]) { |
| 10 | + case "-mode" -> mode = args[i + 1]; |
| 11 | + case "-data" -> data = args[i + 1]; |
| 12 | + case "-key" -> key = Integer.parseInt(args[i + 1]); |
| 13 | + case "-in" -> inputFileName = args[i + 1]; |
| 14 | + case "-out" -> outputFileName = args[i + 1]; |
| 15 | + case "-alg" -> alg = args[i + 1]; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + boolean readFromFile = !inputFileName.isEmpty(); |
| 20 | + boolean writeToFile = !outputFileName.isEmpty(); |
| 21 | + |
| 22 | + if (!data.isEmpty() && !inputFileName.isEmpty()) { |
| 23 | + readFromFile = false; |
| 24 | + } |
| 25 | + |
| 26 | + if (readFromFile){ |
| 27 | + File file = new File(inputFileName); |
| 28 | + try (Scanner fileScanner = new Scanner(file)) { |
| 29 | + data = fileScanner.nextLine(); |
| 30 | + } catch (FileNotFoundException e) { |
| 31 | + System.out.println("Error " + e.getMessage()); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + switch (mode) { |
| 36 | + case "enc" -> { |
| 37 | + if ("unicode".equals(alg)) { |
| 38 | + unicode(data, key, writeToFile); |
| 39 | + } else { |
| 40 | + shift(data, key, writeToFile); |
| 41 | + } |
| 42 | + } |
| 43 | + case "dec" -> { |
| 44 | + if ("unicode".equals(alg)) { |
| 45 | + unicodeDecrypt(data, key, writeToFile); |
| 46 | + } else { |
| 47 | + shiftDecrypt(data, key, writeToFile); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static void unicode(String message, int key, boolean writeToFile) { |
| 54 | + StringBuilder sb = new StringBuilder(); |
| 55 | + for (int i = 0; i < message.length(); i++) { |
| 56 | + char ch = message.charAt(i); |
| 57 | + int ascii = (int) ch + key; |
| 58 | + sb.append((char) ascii); |
| 59 | + } |
| 60 | + showResults(writeToFile, sb); |
| 61 | + } |
| 62 | + |
| 63 | + public static void shift(String message, int key, boolean writeToFile) { |
| 64 | + StringBuilder sb = new StringBuilder(); |
| 65 | + for (int i = 0; i < message.length(); i++) { |
| 66 | + char ch = message.charAt(i); |
| 67 | + if (ch >= 'A' && ch <= 'Z') { |
| 68 | + sb.append((char) ((ch - 'A' + key) % 26 + 'A')); |
| 69 | + } else if (ch >= 'a' && ch <= 'z') { |
| 70 | + sb.append((char) ((ch - 'a' + key) % 26 + 'a')); |
| 71 | + } else sb.append(ch); |
| 72 | + } |
| 73 | + showResults(writeToFile, sb); |
| 74 | + } |
| 75 | + |
| 76 | + public static void unicodeDecrypt(String message, int key, boolean writeToFile) { |
| 77 | + StringBuilder sb = new StringBuilder(); |
| 78 | + for (int i = 0; i < message.length(); i++) { |
| 79 | + char ch = message.charAt(i); |
| 80 | + int ascii = (int) ch - key; |
| 81 | + sb.append((char) ascii); |
| 82 | + } |
| 83 | + showResults(writeToFile, sb); |
| 84 | + } |
| 85 | + |
| 86 | + public static void shiftDecrypt(String message, int key, boolean writeToFile) { |
| 87 | + StringBuilder sb = new StringBuilder(); |
| 88 | + for (int i = 0; i < message.length(); i++) { |
| 89 | + char ch = message.charAt(i); |
| 90 | + if (ch >= 'A' && ch <= 'Z') { |
| 91 | + sb.append((char) (((ch - 'A' - key + 26) % 26) + 'A')); |
| 92 | + } else if (ch >= 'a' && ch <= 'z') { |
| 93 | + sb.append((char) (((ch - 'a' - key + 26) % 26) + 'a')); |
| 94 | + } else sb.append(ch); |
| 95 | + } |
| 96 | + showResults(writeToFile, sb); |
| 97 | + } |
| 98 | + |
| 99 | + private static void showResults(boolean writeToFile, StringBuilder sb) { |
| 100 | + if (writeToFile) { |
| 101 | + try { |
| 102 | + fw = new FileWriter(outputFileName); |
| 103 | + fw.write(String.valueOf(sb)); |
| 104 | + } catch (IOException e) { |
| 105 | + System.out.println("Error " + e.getMessage()); |
| 106 | + } finally { |
| 107 | + try { |
| 108 | + if (fw != null) { |
| 109 | + fw.close(); |
| 110 | + } |
| 111 | + } catch (IOException e) { |
| 112 | + System.out.println("Error " + e.getMessage()); |
| 113 | + } |
| 114 | + } |
| 115 | + } else { |
| 116 | + System.out.print(sb); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + static FileWriter fw = null; |
| 121 | + static String outputFileName = ""; |
| 122 | + static String mode = "enc", data = "", inputFileName = "", alg = "shift"; |
| 123 | + static int key = 0; |
| 124 | +} |
0 commit comments