-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
116 lines (107 loc) · 3.95 KB
/
Main.java
File metadata and controls
116 lines (107 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package encryptdecrypt;
import encryptdecrypt.algorithm.Algorithm;
import encryptdecrypt.algorithm.Shift;
import encryptdecrypt.algorithm.Unicode;
import java.io.*;
import java.util.HashMap;
public class Main {
private static HashMap<String, String> Params() {
HashMap<String, String> params = new HashMap<String, String>();
params.put("mode", "enc");
params.put("data", "");
params.put("key", "0");
params.put("in", null);
params.put("out", null);
params.put("alg", "shift");
return params;
}
private static void checkParam(String param, String value) throws Exception {
char[] arr = value.toCharArray();
if (arr[0] == '-')
throw new Exception("forbidden symbol for value");
if (param.equals("mode")) {
if (!value.equals("enc") && !value.equals("dec"))
throw new Exception("mode should be enc|dec");
} else if (param.equals("key")) {
try {
double d = Double.parseDouble(value);
} catch (NumberFormatException | NullPointerException nfe) {
throw new Exception("key should to be number");
}
}
}
private static String readFile(String path) throws Exception {
File file = new File(path);
BufferedReader reader = new BufferedReader(new FileReader(file));
String str = null, line;
while ((line = reader.readLine()) != null) {
if (str == null)
str = "";
else
str += "\n";
str += line;
}
reader.close();
return str;
}
private static void writeToFile(String str, String path) throws Exception {
BufferedWriter writer = new BufferedWriter(new FileWriter(path));
writer.write(str);
writer.close();
}
public static void main(String[] args) {
HashMap<String, String> params = Params();
String currentKey = null;
String str;
try {
for (String arg : args) {
if (currentKey == null) {
currentKey = "";
char[] arr = arg.toCharArray();
boolean type = arr[0] == '-';
if (type) {
for (int i = 0; i < arr.length - 1; i++) {
currentKey += Character.toString(arr[i + 1]);
}
}
} else {
if (!params.containsKey(currentKey))
throw new Exception("Wrong param " + currentKey);
checkParam(currentKey, arg);
params.replace(currentKey, arg);
currentKey = null;
}
}
boolean reverse = params.get("mode").equals("dec");
str = params.get("data");
if (str.length() == 0) {
String path = params.get("in");
if (path == null)
throw new Exception("there is no data or file path");
else
str = readFile(path);
}
int shift = Integer.parseInt(params.get("key"));
String algorithm = params.get("alg");
Algorithm method;
switch (algorithm) {
case "unicode":
method = Crypto.getUnicode();
break;
case "shift":
method = Crypto.getShift();
break;
default:
throw new Exception("there is no method");
}
str = (reverse) ? method.decrypt(str, shift) : method.encrypt(str, shift);
String output = params.get("out");
if (output == null)
System.out.println(str);
else
writeToFile(str, output);
} catch (Exception e) {
throw new Error(e.getMessage());
}
}
}