-
Notifications
You must be signed in to change notification settings - Fork 0
/
enigma.cpp
188 lines (153 loc) · 4.04 KB
/
enigma.cpp
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
#include <iostream>
#include "enigma.h"
#include "plugboard.h"
#include "rotor.h"
#include "reflector.h"
#include "errors.h"
#include "helper.h"
using namespace std;
bool Enigma::set_enigma(int argc, char** argv, int& error)
{
if (argc < 4)//Check input
{
error = INSUFFICIENT_NUMBER_OF_PARAMETERS;
cerr << "usage: enigma plugboard-file reflector-file"
"(<rotor-file>)* rotor-positions";
return false;
}
if (!(plugboard.set_plugboard(argv[1], error)))//Load plugboard
{
return false;
}
if (!(reflector.set_reflector(argv[2], error)))//Load reflector
{
return false;
}
num_rotors = argc - 4;
if (num_rotors > 0)//Load rotors
{
rotors = new Rotor[num_rotors];
int j = 0;
for (int i = num_rotors - 1; i >= 0; i-- )
{
if (!(rotors[j].set_rotor(argv[i + 3], error)))
{
return false;
}
j++;
}
}
int positions[num_rotors];//Load rotor positions
if(!(fetch_rotor_pos(argv[argc - 1], num_rotors, positions, error)))
{
return false;
}
for (int i = 0 ; i < num_rotors ; i++)//Calibrate starting positions
{
rotors[i].calibrate_start_pos(positions, i, num_rotors);
}
return true;
}
bool Enigma::fetch_rotor_pos(char const filename[], int num_of_rotors,
int positions[], int& error)
{
ifstream rotor_pos_file;
rotor_pos_file.open(filename);//Open file
if(rotor_pos_file.fail())//If error opening file
{
cerr << "Rotor positions file " << filename << "." << endl;
error = ERROR_OPENING_CONFIGURATION_FILE;
return false;
}
if (eof_test(rotor_pos_file))//Check if file empty
{
cerr << "Insufficient number of parameters in rotor position "
"file " << filename << "." << endl << "No starting position for"
" rotor 0." << endl;
rotor_pos_file.close();
error = NO_ROTOR_STARTING_POSITION;
return false;
}
int index;
for (index = 0 ; index < num_of_rotors && !(eof_test(rotor_pos_file)) ; index++)
{
if(!(symbol_test(rotor_pos_file)))//Check for non-numeric symbols
{
cerr << "Non-numeric character in rotor positions file "
<< filename << "." << endl;
rotor_pos_file.close();
error = NON_NUMERIC_CHARACTER;
return false;
}
rotor_pos_file >> positions[index];//Read in
if (!(range_test(positions, index)))//Check if number within range
{
cerr << "Number out of range in rotor positions file "
<< filename << "." << endl;
rotor_pos_file.close();
error = INVALID_INDEX;
return false;
}
}
if (index < num_of_rotors)//If fewer values than number of rotors
{
cerr << "Insufficient number of parameters in rotor positions "
" file " << filename << "." << endl << "No starting position for "
" rotor " << index << "." << endl;
rotor_pos_file.close();
error = NO_ROTOR_STARTING_POSITION;
return false;
}
rotor_pos_file.close();
return true;
}
bool Enigma::encrypt(istream& input, ostream& output, int& error)
{
char letter;
int letter_num;
while (input >> ws >> letter && letter != '.')
{
if (letter < 64 || letter > 91)//Check if input valid
{
cerr << endl << letter << " is an invalid input "
"character (input characters must be upper case letters "
"(A to Z)!" << endl;
error = INVALID_INPUT_CHARACTER;
return false;
}
letter_num = static_cast<int>(letter - 65);
if(num_rotors > 0)
{
rotors[0].rotor_rotate();
for (int i = 0 ; i < num_rotors - 1 ; i++)//Check all rotors
{
if(rotors[i].notch)//If a notch is triggered
{
rotors[i + 1].rotor_rotate();//Rotate next
rotors[i].notch = false;
}
}
}
plugboard.operate_plugboard(letter_num);//Operate plugboard 1st time
for (int i = 0 ; i < num_rotors ; i++)//Operate rotors right-left
{
letter_num = rotors[i].rtol(letter_num);
}
reflector.operate_reflector(letter_num);
for (int i = num_rotors - 1 ; i >= 0 ; i--)//Operate rotors left-right
{
letter_num = rotors[i].ltor(letter_num);
}
plugboard.operate_plugboard(letter_num);//Operate plugboard 2nd time
letter = static_cast<char>(letter_num + 65);
output << letter;
}
return true;
}
Enigma::~Enigma()
{
if (num_rotors > 0)//Deallocate dynamic rotor array
{
delete [] rotors;
}
}