-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathBacon.java
97 lines (88 loc) · 2.49 KB
/
Bacon.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
package cryptography.ciphers.bacon;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import cryptography.Mode;
public class Bacon {
public static void main(String[] args) {
}
public static String bacon(String input, Mode mode) {
String output = "";
StringBuilder stringBuilder = new StringBuilder();
Map<String, String> table = new HashMap<>();
if (mode == Mode.ENCRYPT) {
table.put("A", "AAAAA");
table.put("B", "AAAAB");
table.put("C", "AAABA");
table.put("D", "AAABB");
table.put("E", "AABAA");
table.put("F", "AABAB");
table.put("G", "AABBA");
table.put("H", "AABBB");
table.put("I", "ABAAA");
table.put("J", "ABAAB");
table.put("K", "ABABA");
table.put("L", "ABABB");
table.put("M", "ABBAA");
table.put("N", "ABBAB");
table.put("O", "ABBBA");
table.put("P", "ABBBB");
table.put("Q", "BAAAA");
table.put("R", "BAAAB");
table.put("S", "BAABA");
table.put("T", "BAABB");
table.put("U", "BABAA");
table.put("V", "BABAB");
table.put("W", "BABBA");
table.put("X", "BABBB");
table.put("Y", "BBAAA");
table.put("Z", "BBAAB");
char[] chars = input.toUpperCase().replace(" ", "").toCharArray();
for (char aChar : chars) {
String ch = Character.toString(aChar);
stringBuilder.append(table.get(ch)).append(" ");
}
output = stringBuilder.toString();
}
if (mode == Mode.DECRYPT) {
table.put("AAAAA", "A");
table.put("AAAAB", "B");
table.put("AAABA", "C");
table.put("AAABB", "E");
table.put("AABAA", "E");
table.put("AABAB", "F");
table.put("AABBA", "G");
table.put("AABBB", "H");
table.put("ABAAA", "I");
table.put("ABAAB", "J");
table.put("ABABA", "K");
table.put("ABABB", "L");
table.put("ABBAA", "M");
table.put("ABBAB", "N");
table.put("ABBBA", "O");
table.put("ABBBB", "P");
table.put("BAAAA", "Q");
table.put("BAAAB", "R");
table.put("BAABA", "S");
table.put("BAABB", "T");
table.put("BABAA", "U");
table.put("BABAB", "V");
table.put("BABBA", "W");
table.put("BABBB", "X");
table.put("BBAAA", "Y");
table.put("BBAAB", "Z");
ArrayList<String> chars = new ArrayList<>();
@SuppressWarnings("resource")
Scanner scanner = new Scanner(input).useDelimiter(" ");
while (scanner.hasNext()) {
chars.add(scanner.next());
}
for (int i = 0; i < chars.size(); i++) {
stringBuilder.append(table.get(chars.get(i)));
}
output = stringBuilder.toString();
}
return output;
}
}