-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
217 lines (175 loc) · 7.54 KB
/
Main.java
File metadata and controls
217 lines (175 loc) · 7.54 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import models.Pair;
import models.Plugboard;
import models.Rotor;
import static consts.Constants.ALPHABET;
public class Main {
public static List<Pair> fillPlugBoard(List<Pair> plugBoard, String line) {
String[] swaps = line.split(" ");
for (String swap : swaps) {
String[] splitSwap = swap.split(",");
String x = splitSwap[0].substring(1);
String y = splitSwap[1].substring(0, 1);
int xInt = ALPHABET.indexOf(x.charAt(0));
int yInt = ALPHABET.indexOf(y.charAt(0));
Pair toAdd = new Pair(xInt, yInt);
plugBoard.add(toAdd);
}
return plugBoard;
}
public static List<Integer> fillRotors(List<Integer> rotors, String line) {
String[] rotorArr = line.split(" ");
for (String rotor : rotorArr) {
rotors.add(Integer.parseInt(rotor));
}
return rotors;
}
public static List<String> fillKeySettings(List<String> keySettings, String line) {
String[] rotorKey = line.split(" ");
return Arrays.asList(rotorKey);
}
public static List<Character> parseInput(String fileName) throws IOException {
File input = new File(fileName);
StringBuilder sb = new StringBuilder();
String line;
try (BufferedReader br = new BufferedReader(new FileReader(input))) {
while((line = br.readLine()) != null ) {
sb.append(line);
}
} catch (FileNotFoundException e) {
System.out.println("Your input file could not be found. Check the readme and try again.");
e.printStackTrace();
}
String contents = sb.toString().toUpperCase();
System.out.println("Enigma I encrypting: " + contents);
// This will allow us to replace our spaces at the end of encryption.
contents = contents.replace(" ", "+");
List<String> contentsList = Arrays.asList(contents.split("(?!^)"));
List<Character> output = new ArrayList<Character>();
for(String s : contentsList) {
char c = s.charAt(0);
if(Character.isAlphabetic(c) || c == '+') {
output.add(s.charAt(0));
}
}
return output;
}
public static List<Rotor> orderRotors(Rotor one, Rotor two, Rotor three, List<Integer> rotors) {
List<Rotor> oRotors = new ArrayList<Rotor>();
for(int i : rotors) {
if(i == 1) {
oRotors.add(one);
}
else if(i == 2) {
oRotors.add(two);
}
else if(i == 3) {
oRotors.add(three);
}
else {
System.out.println("Your day key contains an invalid rotor number. Check the readme and try again.");
System.exit(1);
}
}
return oRotors;
}
public static List<Character> encrypt(List<Character> toEncrypt, Plugboard pb, List<Rotor> rotors, Rotor reflector) {
List<Character> encrypted = new ArrayList<Character>();
for(Character c : toEncrypt) {
if(c == '+') {
encrypted.add(' ');
}
else {
int alphaIndex = ALPHABET.indexOf(c);
int afterPb = pb.translate(alphaIndex);
int afterRotors = rotorEncrypt(afterPb, rotors);
int afterReflector = reflector.translate(afterRotors);
int afterReverseRotors = reverseRotorEncrypt(afterReflector, rotors);
int afterReversePb = pb.reverseTranslate(afterReverseRotors);
System.out.println("The lamplight is illuminated. Encrypted output: " + ALPHABET.get(afterReversePb));
encrypted.add(ALPHABET.get(afterReversePb));
}
}
return encrypted;
}
public static int reverseRotorEncrypt(int current, List<Rotor> rotors) {
// There is no rotation in left-to-right rotor encryption.
for(int i = rotors.size() - 1; i >= 0; i--) {
current = rotors.get(i).translate(current);
}
return current;
}
public static int rotorEncrypt(int current, List<Rotor> rotors) {
// Rotate the outermost rotor.
rotors.get(0).rotate();
// Check if any of the other rotors are also rotated.
if(rotors.get(0).getRotor() == 1 && rotors.get(0).getTop() == 'R'
|| rotors.get(0).getRotor() == 2 && rotors.get(0).getTop() == 'F'
|| rotors.get(0).getRotor() == 3 && rotors.get(0).getTop() == 'W') {
rotors.get(1).rotate();
}
if(rotors.get(1).getRotor() == 1 && rotors.get(1).getTop() == 'R'
|| rotors.get(1).getRotor() == 2 && rotors.get(1).getTop() == 'F'
|| rotors.get(1).getRotor() == 3 && rotors.get(1).getTop() == 'W') {
rotors.get(2).rotate();
}
// Current is still just an alpha index.
for(int i = 0; i < rotors.size(); i++) {
current = rotors.get(i).translate(current);
}
return current;
}
public static void main(String[] args) throws IOException {
// Read the file and fill variables.
String dayKeyFileName = args[0];
File dayKey = new File(dayKeyFileName);
List<Pair> plugBoard = new ArrayList<Pair>();
List<Integer> rotors = new ArrayList<Integer>();
List<String> keySettings = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(dayKey))) {
for (int linesRead = 0; linesRead < 3; linesRead++) {
String line = br.readLine();
if (line != null && linesRead == 0) {
plugBoard = fillPlugBoard(plugBoard, line);
} else if (line != null && linesRead == 1) {
rotors = fillRotors(rotors, line);
} else if (line != null && linesRead == 2) {
keySettings = fillKeySettings(keySettings, line);
} else {
System.out.println("Your day key was not formatted properly. Check the readme and try again.");
System.exit(0);
}
}
} catch (FileNotFoundException e) {
System.out.println("The input file could not be found at your given location.");
e.printStackTrace();
}
String inputFileName = args[1];
List<Character> toEncrypt = parseInput(inputFileName);
// Set-up the plugboard.
Plugboard pb = new Plugboard(plugBoard);
// Set-up the rotors.
int oneOffset = ALPHABET.indexOf(keySettings.get(0).charAt(0));
int twoOffset = ALPHABET.indexOf(keySettings.get(1).charAt(0));
int threeOffset = ALPHABET.indexOf(keySettings.get(2).charAt(0));
Rotor one = new Rotor(1, oneOffset);
Rotor two = new Rotor(2, twoOffset);
Rotor three = new Rotor(3, threeOffset);
// The reflector is basically just another rotor with no offset or rotating.
Rotor reflector = new Rotor(0, 0);
// Place the rotors in their given order.
List<Rotor> oRotors = orderRotors(one, two, three, rotors);
List<Character> encrypted = encrypt(toEncrypt, pb, oRotors, reflector);
for(Character c : encrypted) {
System.out.print(c);
}
System.out.print("\n");
}
}