- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
116 lines (107 loc) · 3.95 KB
/
Copy pathMain.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
packageencryptdecrypt;
importencryptdecrypt.algorithm.Algorithm;
importencryptdecrypt.algorithm.Shift;
importencryptdecrypt.algorithm.Unicode;
importjava.io.*;
importjava.util.HashMap;
publicclassMain {
privatestaticHashMap<String, String> Params() {
HashMap<String, String> params = newHashMap<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");
returnparams;
}
privatestaticvoidcheckParam(Stringparam, Stringvalue) throwsException {
char[] arr = value.toCharArray();
if (arr[0] == '-')
thrownewException("forbidden symbol for value");
if (param.equals("mode")) {
if (!value.equals("enc") && !value.equals("dec"))
thrownewException("mode should be enc|dec");
} elseif (param.equals("key")) {
try {
doubled = Double.parseDouble(value);
} catch (NumberFormatException | NullPointerExceptionnfe) {
thrownewException("key should to be number");
}
}
}
privatestaticStringreadFile(Stringpath) throwsException {
Filefile = newFile(path);
BufferedReaderreader = newBufferedReader(newFileReader(file));
Stringstr = null, line;
while ((line = reader.readLine()) != null) {
if (str == null)
str = "";
else
str += "\n";
str += line;
}
reader.close();
returnstr;
}
privatestaticvoidwriteToFile(Stringstr, Stringpath) throwsException {
BufferedWriterwriter = newBufferedWriter(newFileWriter(path));
writer.write(str);
writer.close();
}
publicstaticvoidmain(String[] args) {
HashMap<String, String> params = Params();
StringcurrentKey = null;
Stringstr;
try {
for (Stringarg : args) {
if (currentKey == null) {
currentKey = "";
char[] arr = arg.toCharArray();
booleantype = arr[0] == '-';
if (type) {
for (inti = 0; i < arr.length - 1; i++) {
currentKey += Character.toString(arr[i + 1]);
}
}
} else {
if (!params.containsKey(currentKey))
thrownewException("Wrong param " + currentKey);
checkParam(currentKey, arg);
params.replace(currentKey, arg);
currentKey = null;
}
}
booleanreverse = params.get("mode").equals("dec");
str = params.get("data");
if (str.length() == 0) {
Stringpath = params.get("in");
if (path == null)
thrownewException("there is no data or file path");
else
str = readFile(path);
}
intshift = Integer.parseInt(params.get("key"));
Stringalgorithm = params.get("alg");
Algorithmmethod;
switch (algorithm) {
case"unicode":
method = Crypto.getUnicode();
break;
case"shift":
method = Crypto.getShift();
break;
default:
thrownewException("there is no method");
}
str = (reverse) ? method.decrypt(str, shift) : method.encrypt(str, shift);
Stringoutput = params.get("out");
if (output == null)
System.out.println(str);
else
writeToFile(str, output);
} catch (Exceptione) {
thrownewError(e.getMessage());
}
}
}