-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
203 lines (140 loc) · 5.34 KB
/
Main.java
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import java.io.*;
import java.text.Normalizer;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 100.txt --> P_100.txt & T_100.txt
// 500.txt
// 1000.txt
// prve spustenie = vytvori subory P_XXX.txt, T_XXX.txt = ZT bez klamacov
// druhe spustenie vytvori subory s klamacmi
// tretie spustenie by znova aplikovalo klamace na povodne ZT bez klamacov
// t.j. pre nove vytvorenie klucov treba vymazat vsetky subory okrem 100.txt, 500.txt, 1000.txt.
// novy kluc sa generuje pri spusteni pokial neexistuju subor P/T_XYZ.txt
File file = new File("P_100.txt");
if (file.exists()) {
doTrickerEnc();
System.exit(0);
} else {
doNoTrickerEnc();
System.exit(0);
}
}
public static String textToTSAandRemoveJ(String text) {
Normalizer.Form NFD = Normalizer.Form.NFD;
text = Normalizer.normalize(text.toLowerCase(), NFD);
text = text.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
String regex = "[^a-z]";
text = text.replaceAll(regex, "");
text = text.replaceAll("j", "i");
return text;
}
public static String readFileToString(File file) {
StringBuilder sb = new StringBuilder();
if (file.exists()) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
sb = null;
} finally {
try {
br.close();
} catch (IOException ex) {
}
}
}
return sb.toString();
}
public static boolean writeToFile(File file, String content) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
bw.write(content);
bw.flush();
return true;
} catch (IOException ex) {
} finally {
try {
bw.close();
} catch (IOException ex) {
}
}
return false;
}
public static String intArrayToString(int[] arr) {
StringBuilder sb = new StringBuilder();
for (int element : arr) {
sb.append(String.valueOf(element));
}
return new String(sb);
}
public static String intListtoString(List<Integer> list) {
StringBuilder sb = new StringBuilder();
for (int item : list) {
sb.append(item);
}
return new String(sb);
}
public static String charListtoString(List<Character> list) {
StringBuilder sb = new StringBuilder();
for (char item : list) {
sb.append(item);
}
return new String(sb);
}
public static void doNoTrickerEnc() {
Polybius p = new Polybius();
Trifid t = new Trifid();
File inFile1 = new File("100.txt");
File inFile2 = new File("500.txt");
File inFile3 = new File("1000.txt");
File P_100 = new File("P_100.txt");
File P_500 = new File("P_500.txt");
File P_1000 = new File("P_1000.txt");
File T_100 = new File("T_100.txt");
File T_500 = new File("T_500.txt");
File T_1000 = new File("T_1000.txt");
String s1 = readFileToString(inFile1);
String s2 = readFileToString(inFile2);
String s3 = readFileToString(inFile3);
writeToFile(P_100, p.doAndReturnEncryption(s1));
writeToFile(P_500, p.doAndReturnEncryption(s2));
writeToFile(P_1000, p.doAndReturnEncryption(s3));
writeToFile(T_100, t.doAndReturnEncryption(s1));
writeToFile(T_500, t.doAndReturnEncryption(s2));
writeToFile(T_1000, t.doAndReturnEncryption(s3));
}
public static void doTrickerEnc() {
File P_100 = new File("P_100.txt");
File P_500 = new File("P_500.txt");
File P_1000 = new File("P_1000.txt");
File T_100 = new File("T_100.txt");
File T_500 = new File("T_500.txt");
File T_1000 = new File("T_1000.txt");
String Ps1 = readFileToString(P_100);
String Ps2 = readFileToString(P_500);
String Ps3 = readFileToString(P_1000);
String Ts1 = readFileToString(T_100);
String Ts2 = readFileToString(T_500);
String Ts3 = readFileToString(T_1000);
// na kazdy ZT s prveho spustenia aplikujeme jeden klamac, keby chceme mozme aj viac
File A_P_100 = new File("A_P_100.txt"); // ps1
File B2b_P_500 = new File("B2b_P_500.txt"); // ps2
File C_P_1000 = new File("C_P_1000.txt"); // ps3
File A_T_100 = new File("A_T_100.txt"); // ts1
File B3c_T_500 = new File("B3c_T_500.txt"); // ts2
File C_T_1000 = new File("C_T_1000.txt"); // ts3
writeToFile(A_P_100, Tricker.A(Ps1));
writeToFile(B2b_P_500, Tricker.B(Ps2, 2, 'b'));
writeToFile(C_P_1000, Tricker.C(Ps3));
writeToFile(A_T_100, Tricker.A(Ts1));
writeToFile(B3c_T_500, Tricker.B(Ts2, 3, 'c'));
writeToFile(C_T_1000, Tricker.C(Ts3));
System.exit(0);
}
}