This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enigma.cc
321 lines (291 loc) · 11.2 KB
/
Enigma.cc
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "Enigma.h"
#include <cstdlib>
#include<fstream>
#include<cstring>
#include "Rottor.h"
#include "Rottor.cc"
#include "Plugboard.h"
#include "Plugboard.cc"
#include "Reflector.h"
#include "Reflector.cc"
using namespace std;
Enigma::Enigma()
{
// constructor
}
void Enigma::FileHandling(char * arrq, char * arrp, int p)
{
plug = 0; //variable for checking which setting is used in Encryption
Ref = 0; // 0 for default
Rot1 = 0; // 1 for setting read from file
Rot2 = 0;
Rot3 = 0;
if (p == 3) // if third argument is present
{
ifstream finp;
finp.open(arrp); // opening that file
if (finp.fail()) // if opening fails
{
cout << "Couldn't find the argument for Rottor setting \n Running Enigma with default Rotor Setting ...\n" << endl;
plug = 0;
Ref = 0;
Rot1 = 0;
Rot2 = 0;
Rot3 = 0;
}
else
{
cout<<"Enigma setting invoked"<<endl;
string word;
finp >> word;
//###################################Reading Plugboard setting#######################################
if(word == "Plugboard:")
{
plug = 1;
q = new char [26];
finp.get(ch);
finp.get(ch);
g=0;
t=25;
for(int g=0; g<=13; g++)
{
finp.get(ch);
q[g] =ch;
finp.get(ch);
q[t] = ch;
finp.get(ch);
finp.get(ch);
if(int(ch) != 10)
{
cout<<"Enter in correct pair \n Remember no pair has more two elements\n";
plug = 0;
break;
exit(0);
}
if(g == 13 || t == 13)
break;
t--;
}
}
cout<<"Plugboard Array : "<< q <<endl;
//###################################Reading Rotor 1 setting#######################################
finp >> word;
if(word == "Rotor1:")
{
Rot1 = 1;
RR1 = new char [26];
finp.get(ch);
for (int i=0; i<26; i++)
{
finp.get(ch);
RR1[i] = ch;
}
cout<<"Rotor1 Array : "<<RR1<<endl;
}
//###################################Reading Rotor 2 setting#######################################
finp >> word;
if(word == "Rotor2:")
{
Rot2=1;
RR2 = new char [26];
finp.get(ch);
for (int i=0; i<26; i++)
{
finp.get(ch);
RR2[i] = ch;
}
cout<<"Rotor2 Array : "<<RR2<<endl;
}
//###################################Reading Rotor 3 setting#######################################
finp >> word;
if(word == "Rotor3:")
{
Rot3 = 1;
RR3 = new char [26];
finp.get(ch);
for (int i=0; i<27; i++)
{
finp.get(ch);
RR3[i] = ch;
}
cout<<"Rotor3 Array : "<<RR3<<endl;
}
//###################################Reading Reflector setting#######################################
finp >> word;
if(word == "Reflector:")
{
RF = new char [26];
finp.get(ch);
finp.get(ch);
g=0;
t=25;
Ref= 1;
for(int g=0; g<=13; g++)
{
finp.get(ch);
RF[g] =ch;
finp.get(ch);
RF[t] = ch;
finp.get(ch);
finp.get(ch);
if(int(ch) != 10)
{
cout<<"Enter in correct pair \n Remember no pair has more two elements\n";
Ref = 0;
break;
exit(0);
}
if(g == 13 || t == 13)
break;
t--;
}
cout<<"Reflector Array : "<<RF<<endl<<endl;
}
}
finp.close(); // closing file
}
ifstream fin; // opening Enigma.ini file
fin.open(arrq);
if (fin.fail())
{
cout << "Couldn't find the argument \n opening default ini file ...\n" << endl;
fin.open("enigma.ini");
}
while (!fin.eof())
{
fin.get(ch);
if (ch != 13 || ch != 10 )
{
if(int(ch) == 10)
{
break;
}
else
i++; // counting string length of message
}
}
i--;
ptr = new char [i]; // creating array memory on heap
fin.seekg(0); // pointing cusor back to start in text file
int b= 0;
while (!fin.eof())
{
fin.get(ch);
if (ch != 13 || ch != 10 )
{
if(int(ch) == 10)
{
break;
}
else
{
ptr[b] = ch;
if (int(ch) == 48 || int(ch) == 49 || int(ch) == 50|| int(ch) == 51|| int(ch) == 52|| int(ch) == 53|| int(ch) == 54|| int(ch) == 55|| int(ch) == 56|| int(ch) == 57)
{
cout<<"Number in Message,... but program will handle it."<<endl;
}
b++;
}
}
}
//################################Converting Upper case to Lower case ################################################################//
for(int y=0; y<i; y++)
{
if(ptr[y]>=65 && ptr[y]<=92)
{
ptr[y]=ptr[y]+32;
}
}
//##############################Exception Handling in Key##################################################################//
fin >> R1;
try{ if (R1 == 0)
{
throw 1;
}
}
catch(int a)
{
cout<<"Enter correct value of Rotor 1"<<endl;
cout<<"good bye....";
delete ptr;
exit(0);
}
fin.get(ch);
fin >> R2;
try{ if (R2 == 0)
{
throw 1;
}
}
catch(int a)
{
cout<<"Enter correct value of Rotor 2"<<endl;
cout<<"good bye....";
delete ptr;
exit(0);
}
fin.get(ch);
fin >> R3;
try{ if (R1 == 0)
{
throw 1;
}
}
catch(int a)
{
cout<<"Enter correct value of Rotor 3"<<endl;
cout<<"good bye....";
delete ptr;
exit(0);
}
cout<<"Rotor 1 key : "<<R1<<endl<<"Rotor 2 key : "<<R2<<endl<<"Rotor 3 key : "<<R3<<endl<<endl;
}
void Enigma::Cipher()
{
Plugboard A(q, plug); // Making Objects of Classes
Reflector B(RF, Ref);
Rottor C(R1,RR1, 1, Rot1);
Rottor D(R2,RR2, 2, Rot2);
Rottor E(R3,RR3, 3, Rot3);
char a;
cout << "String is "<<ptr<<endl<<"Message length is "<<i<<endl<<endl;
cout<< "Encrypted message is \" ";
ofstream fout;
fout.open("Output.txt");
for(int y=0; y<i; y++)
{
if(ptr[y]>= 'a' && ptr[y] <= 'z' )
{
a = A.swap(ptr[y]);
a = C.Encrypt(a);
a = D.Encrypt(a);
a = E.Encrypt(a);
a = B.swap(a);
a = E.Encrypt(a);
a = D.Encrypt(a);
a = C.Encrypt(a);
a = A.swap(a);
cout<< a;
fout<<a;
}
else
{
cout<<ptr[y];
fout<<ptr[y];
}
}
cout<<"\""<<endl;
fout<<" "<<endl;
fout<<R1<<endl<<R2<<endl<<R3<<endl;
fout.close();
}
Enigma::~Enigma()
{
delete[] Instance;
delete[] ptr;
delete[] q;
delete[] RR1;
delete[] RR2;
delete[] RR3;
delete[] RF; // Destructor
}