-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enigma.h
305 lines (255 loc) · 5.84 KB
/
Enigma.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#ifndef ENIGMA_H
#define ENIGMA_H
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int mod(int a)
{
return a>=0?a%26:a+26;
}
bool CompareVector(const vector<int>& v1,const vector<int>& v2)
{
if(v1.size()!=v2.size()) return false;
else
{
for(int i=0;i<v1.size();i++)
{
if(v1[i]!=v2[i]) return false;
}
return true;
}
}
class Rotor
{
int output[26];
int backoutput[26];
char pos;
int dev;
char notch;
public:
Rotor(){}
explicit Rotor(string charr,char _pos,char _dev,char _notch)
{
for(int i=0;i<26;i++)
{
output[i]=charr[i]-'A';
backoutput[charr[i]-'A']=i;
}
pos=_pos;
dev=_dev-'A';
notch=_notch;
}
explicit Rotor(string charr,char _notch)
{
for(int i=0;i<26;i++)
{
output[i]=charr[i]-'A';
backoutput[charr[i]-'A']=i;
}
pos='A';
dev=0;
notch=_notch;
}
void setState(char _pos)
{
pos=_pos;
}
bool reachNotch()
{
return pos==notch+1;
}
char getState()
{
return pos;
}
int GetOutput(int input)
{
int delta=pos-'A';
return mod(output[mod(input+delta-dev)]-delta+dev);
}
int GetBackOutput(int input)
{
int delta=pos-'A';
return mod(backoutput[mod(input+delta-dev)]-delta+dev);
}
char GetCharOutput(int input)
{
return (char)(GetOutput(input)+'A');
}
char GetCharBackOutput(int input)
{
return (char)(GetBackOutput(input)+'A');
}
void Rotate()
{
pos=='Z'?pos='A':pos++;
}
void setRingSetting(char ch)
{
dev=ch-'A';
}
};
class Reflexor
{
int output[26];
public:
Reflexor(){}
Reflexor(int* arr)
{
for(int i=0;i<26;i++) output[i]=arr[i];
}
Reflexor(string arr)
{
for(int i=0;i<26;i++) output[i]=arr[i]-'A';
}
int GetOutPut(int input)
{
return output[input];
}
};
class Enigma
{
Rotor rotor[3];
Reflexor reflexor;
char state[3];
public:
Enigma(){}
Enigma(Rotor rotor1,Rotor rotor2,Rotor rotor3,Reflexor ref,string state_initial,string ringsetting)
{
rotor[0]=rotor1;
rotor[1]=rotor2;
rotor[2]=rotor3;
reflexor=ref;
state[0]=state_initial[0];
state[1]=state_initial[1];
state[2]=state_initial[2];
rotor[0].setState(state[0]);rotor[0].setRingSetting(ringsetting[0]);
rotor[1].setState(state[1]);rotor[1].setRingSetting(ringsetting[1]);
rotor[2].setState(state[2]);rotor[2].setRingSetting(ringsetting[2]);
}
void NextState()
{
rotor[2].Rotate();
state[2]=rotor[2].getState();
if(rotor[2].reachNotch())
{
rotor[1].Rotate();
state[1]=rotor[1].getState();
if(rotor[1].reachNotch())
{
rotor[0].Rotate();
state[0]=rotor[0].getState();
}
}
}
void GotoNextNState(int n)
{
while(n--)
{
NextState();
}
}
int GetOutput(int input)
{
NextState();
int o2=rotor[2].GetOutput(input);
int o1=rotor[1].GetOutput(o2);
int o0=rotor[0].GetOutput(o1);
int ef=reflexor.GetOutPut(o0);
int e0=rotor[0].GetBackOutput(ef);
int e1=rotor[1].GetBackOutput(e0);
int e2=rotor[2].GetBackOutput(e1);
//cout<<input<<"->"<<o2<<"->"<<o1<<"->"<<o0<<"->(ref)"<<ef<<"->"<<e0<<"->"<<e1<<"->"<<e2<<endl;
return e2;
}
int GetOutputWithoutRotate(int input)
{
int o2=rotor[2].GetOutput(input);
int o1=rotor[1].GetOutput(o2);
int o0=rotor[0].GetOutput(o1);
int ef=reflexor.GetOutPut(o0);
int e0=rotor[0].GetBackOutput(ef);
int e1=rotor[1].GetBackOutput(e0);
int e2=rotor[2].GetBackOutput(e1);
return e2;
}
char GetCharOutput(char input)
{
return (char)(GetOutput(input-'A')+'A');
}
void setState(char ch1,char ch2,char ch3)
{
rotor[0].setState(ch1);
rotor[1].setState(ch2);
rotor[2].setState(ch3);
}
string getState()
{
string result;
result+=state[0];result+=state[1];result+=state[2];
return result;
}
string Encrypt(string cleartext)
{
string cipher_text;
for(int i=0;i<cleartext.length();i++)
{
cipher_text+=GetCharOutput(cleartext[i]);
}
return cipher_text;
}
};
vector<int> FindCircle(const char* chs)
{
bool visit[26];
for(int i=0;i<26;i++) visit[i]=false;
vector<int> loopsizes;
for(int i=0;i<26;i++)
{
if(visit[i]) continue;
visit[i]=true;
int loop=1;
int next=chs[i]-'A';
while(!visit[next])
{
loop++;
visit[next]=true;
next=chs[next]-'A';
}
loopsizes.push_back(loop);
}
sort(loopsizes.begin(),loopsizes.end(),greater<int>());
return loopsizes;
}
class CircleCondition
{
vector<int> circle1;
vector<int> circle2;
vector<int> circle3;
public:
CircleCondition(vector<int> str1,vector<int> str2,vector<int> str3)
{
circle1=str1;
circle2=str2;
circle3=str3;
}
bool CompareComponent(const vector<int>& other,int dim)
{
switch(dim)
{
case 1: return CompareVector(circle1,other);
case 2: return CompareVector(circle2,other);
case 3: return CompareVector(circle3,other);
default: return false;
}
}
};
void PrintVec(vector<int>& vec)
{
for(int i=0;i<vec.size();i++)
cout<<vec[i]<<" ";
cout<<endl;
}
#endif