-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnigma.java
184 lines (164 loc) · 8.2 KB
/
Enigma.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*
* @author Logan
*/
public class Enigma {
private String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ#";
private String key_1 = "GNUAHOVBIPWCJQXDKRY#ELSZFMT";
private String key_2 = "EJ#OTYCHMRWAFKPUZDINSXBGLQV";
private String key_3 = "BDFHJLNPRTVXZACEGI#KMOQSUWY";
private String key_4 = "KPHDEAC#VTWQMYNLXSURZOJFBGI";
private String key_5 = "NDYGLQICVEZRPTAOXWBMJSUHK#F";
private String key_O;
private String key_M;
private String key_I;
public String assignKey(int keyNum) { //Sets selected key's string to the input key's position
String key = "";
if (keyNum == 1) { //If 1 is selected, set the key to key_1
key = key_1;
} else {
if (keyNum == 2) { //If 2 is selected, set the key to key_2
key = key_2;
} else {
if (keyNum == 3) { //If 3 is selected, set the key to key_3
key = key_3;
} else {
if (keyNum == 4) { //If 4 is selected, set the key to key_4
key = key_4;
} else {
if (keyNum == 5) { //If 5 is selected, set the key to key_5
key = key_5;
}
}
}
}
}
return key;
}
public String startPos(String key, int position) {
int end = key.length();
String part1 = key.substring(0, position); //Takes everything before the start position
String part2 = key.substring(position, end); //Takes everything after the start position
key = part2 + part1; //Puts the start position as the first character, and puts the original start position after the original last character
return key;
}
public String[] plugboardIn(String plugboard) {
plugboard = plugboard.toUpperCase();
String[] plug = plugboard.split(" "); //Splits the plugboard input by spaces and creates an array of two character long strings
return plug;
}
public String plugboard(String text, String[] plug) {
int length = text.length();
for (int i = 0; i < length; i++) {//For all characters
char ch = text.charAt(i);//Set ch to the character at the index
for (int j = 0; j < 10; j++) {//For all indexes in the plugboard array, check...
if (ch == plug[j].charAt(0)) {//...if ch is the character at index 0 in the string at index j.
text = text.substring(0, i) + plug[j].charAt(1) + text.substring(i + 1);//replace the character at index 0 with the character at index 1
}
if (ch == plug[j].toLowerCase().charAt(0)) {//...if ch is the lowercase character at index 0 in the string at index j.
text = text.substring(0, i) + plug[j].toLowerCase().charAt(1) + text.substring(i + 1);//replace the character at index 0 with the character at index 1
} else {// And if not, ...
if (ch == plug[j].charAt(1)) {//...if ch is the character at index 1 in the string at index j
text = text.substring(0, i) + plug[j].charAt(0) + text.substring(i + 1);//replace the character at index 1 with the character at index 0
}
if (ch == plug[j].toLowerCase().charAt(1)) {//...if ch is the lowercase character at index 1 in the string at index j
text = text.substring(0, i) + plug[j].toLowerCase().charAt(0) + text.substring(i + 1);//replace the character at index 1 with the character at index 0
}
}
}
}
return text;
}
public String rotationForward(String key) {
String part1 = key.substring(25);
String part2 = key.substring(0, 25);
key = part1 + part2;
return key;
}
public String rotationBackward(String key) {
String part1 = key.substring(1);
String part2 = key.substring(0, 1);
key = part1 + part2;
return key;
}
public String encode(String text, String plugboard, int keyNum_O, int keyNum_M, int keyNum_I, int keyPos_O, int keyPos_M, int keyPos_I) {
text = text.replaceAll(" ", "#");
key_O = assignKey(keyNum_O);
key_M = assignKey(keyNum_M);
key_I = assignKey(keyNum_I);
key_O = startPos(key_O, keyPos_O);
key_M = startPos(key_M, keyPos_M);
key_I = startPos(key_I, keyPos_I);
int length = text.length();
text = plugboard(text, plugboardIn(plugboard));
for (int i = 0; i < length; i++) {
char ch = text.charAt(i);//Set ch to the character at the index
if (alpha.contains(text.substring(i, i + 1)) || alpha.toLowerCase().contains(text.substring(i, i + 1))) {
for (int j = 0; j < 27; j++) {
if (ch == key_I.charAt(j)) {
text = text.substring(0, i) + key_O.charAt(key_M.indexOf(key_O.charAt(j))) + text.substring(i + 1);
}
if (ch == key_I.toLowerCase().charAt(j)) {
text = text.substring(0, i) + key_O.toLowerCase().charAt(key_M.toLowerCase().indexOf(key_O.toLowerCase().charAt(j))) + text.substring(i + 1);
}
}
key_I = rotationForward(key_I);
if (i % 27 == 0 && i != 0) {
key_M = rotationForward(key_M);
if (i % 729 == 0) {
key_O = rotationForward(key_O);
}
}
}
}
text = plugboard(text, plugboardIn(plugboard));
text = text.replaceAll("#", " ");
return text;
}
public String decode(String text, String plugboard, int keyNum_O, int keyNum_M, int keyNum_I, int keyPos_O, int keyPos_M, int keyPos_I) {
text = text.replaceAll(" ", "#");
key_O = assignKey(keyNum_O);
key_M = assignKey(keyNum_M);
key_I = assignKey(keyNum_I);
key_O = startPos(key_O, keyPos_O);
key_M = startPos(key_M, keyPos_M);
key_I = startPos(key_I, keyPos_I);
int length = text.length();
for (int i = 0; i < length; i++) {
key_I = rotationForward(key_I);
if (i % 27 == 0 && i != 0) {
key_M = rotationForward(key_M);
if (i % 729 == 0) {
key_O = rotationForward(key_O);
}
}
}
text = plugboard(text, plugboardIn(plugboard));
for (int i = length - 1; i >= 0; i--) {
char ch = text.charAt(i);//Set ch to the character at the index
if (alpha.contains(text.substring(i, i + 1)) || alpha.toLowerCase().contains(text.substring(i, i + 1))) {
for (int j = 26; j >= 0; j--) {
if (ch == key_O.charAt(j)) {
text = text.substring(0, i) + key_I.charAt(key_O.indexOf(key_M.charAt(j))) + text.substring(i + 1);
}
if (ch == key_O.toLowerCase().charAt(j)) {
text = text.substring(0, i) + key_I.toLowerCase().charAt(key_O.toLowerCase().indexOf(key_M.toLowerCase().charAt(j))) + text.substring(i + 1);
}
}
key_I = rotationBackward(key_I);
if (i % 27 == 0 && i != 0) {
key_M = rotationBackward(key_M);
if (i % 729 == 0) {
key_O = rotationBackward(key_O);
}
}
}
}
text = plugboard(text, plugboardIn(plugboard));
text = text.replaceAll("#", " ");
return text;
}
}