-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnigma.cpp
185 lines (167 loc) · 4.15 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
#include <algorithm>
#include <array>
#include <iostream>
#include <fstream>
#include <string>
#include "Enigma.hpp"
#include "Plugboard.hpp"
#include "Reflector.hpp"
#include "Rotor.hpp"
bool Enigma::duplicateCheck(std::array<int, 3> rotorIndexes)
{
if (rotorIndexes[0] == rotorIndexes[1]
|| rotorIndexes[1] == rotorIndexes[2]
|| rotorIndexes[0] == rotorIndexes[2])
{
std::cout << "Error: duplicates found" << std::endl;
return true;
}
else
{
return false;
}
}
bool Enigma::correctInput(int choice)
{
if (choice > 0 && choice < 6)
{
return false;
}
else
{
std::cout << "Error: incorrect input. There're only rotors 1-5" << std::endl;
return true;
}
}
int Enigma::atoi(char charToInteger)
{
return static_cast<int>(charToInteger) - 48;
}
bool Enigma::setRingSettings(std::string &ringSettings)
{
std::cin >> ringSettings;
std::transform(ringSettings.begin(), ringSettings.end(), ringSettings.begin(), ::toupper);
if (ringSettings.length() != 3)
{
std::cout << "Error: too much or too few rings. There're should be exactly 3 rings" << std::endl;
return true;
}
else
{
for (auto &iterator : ringSettings)
{
if (!(iterator > 64 && iterator < 91)) //65 == 'A', 90 == 'Z'
{
std::cout << "Error: rings should be alphabetical characters (A-Z)" << std::endl;
return true;
}
}
return false;
}
}
void Enigma::encipher(std::array<Rotor, 3> &rotors, Reflector &reflector, Plugboard &plugboard, char &eachCharacter, int &offsetCounter)
{
plugboard.substitute(eachCharacter);
rotors[2].offset();
if (offsetCounter / 26 > 0)
{
for (int secondOffsetCounter = offsetCounter / 26; secondOffsetCounter > 0; --secondOffsetCounter)
{
rotors[1].offset();
}
if (offsetCounter / 676 > 0)
{
for (int thirdOffsetCounter = offsetCounter / 676; thirdOffsetCounter > 0; --thirdOffsetCounter)
{
rotors[0].offset();
}
}
}
++offsetCounter;
for (auto i = 0; i < 3; i++)
{
rotors[i].ringApply(rotors[i].ring);
}
for (auto i = 2; i > -1; i--)
{
rotors[i].substitute(eachCharacter, 's');
}
reflector.reflectorSubstitute(eachCharacter);
for (auto i = 0; i < 3; i++)
{
rotors[i].substitute(eachCharacter, 'r');
}
plugboard.substitute(eachCharacter);
}
int Enigma::start()
{
std::array<Rotor, 3> rotors;
std::array<int, 3> rotorIndexes = {0, 0, 0};
char buffer = '0';
std::cout << "Set up 3 rotors. Choose from 1 to 5. Repeats restricted" << std::endl;
std::cout << "First rotor: ";
std::cin >> buffer;
rotorIndexes[0] = atoi(buffer);
if (correctInput(rotorIndexes[0]))
{
return EXIT_FAILURE;
}
std::cout << "Second rotor: ";
std::cin >> buffer;
rotorIndexes[1] = atoi(buffer);
if (duplicateCheck(rotorIndexes) || correctInput(rotorIndexes[1]))
{
return EXIT_FAILURE;
}
std::cout << "Third rotor: ";
std::cin >> buffer;
rotorIndexes[2] = atoi(buffer);
if (duplicateCheck(rotorIndexes) || correctInput(rotorIndexes[2]))
{
return EXIT_FAILURE;
}
for (auto i = 0; i < 3; i++)
{
rotors[i].setRotor(rotorIndexes[i]);
}
std::cout << "Set up rings. Enter 3 characters from A to Z. Not case sensitive" << std::endl;
if (setRingSettings(*ringSettings))
{
return EXIT_FAILURE;
}
for (auto i = 0; i < 3; i++)
{
rotors[i].ring = ringSettings[0][i];
}
Plugboard plugboard;
char plugboardOption;
std::cout << "Would you like to set up plugboard? Y/y to set up or any other character to skip: ";
std::cin >> plugboardOption;
if (plugboardOption == 'y' || plugboardOption == 'Y')
{
if (plugboard.setPlugboard())
{
return EXIT_FAILURE;
}
}
Reflector reflector;
std::string message;
int offsetCounter = 0;
std::cout << "Type your message: ";
if (!(plugboardOption == 'y' || plugboardOption == 'Y')) std::cin.ignore();
std::getline(std::cin, message);
std::transform(message.begin(), message.end(), message.begin(), ::toupper);
for (auto &eachCharacter : message)
{
if (eachCharacter > 64 && eachCharacter < 91) // 65 == 'A', 90 == 'Z'
{
encipher(rotors, reflector, plugboard, eachCharacter, offsetCounter);
}
}
std::cout << "Ciphered message: " << message << std::endl;
std::ofstream result;
result.open("result.txt");
result << message;
result.close();
return EXIT_SUCCESS;
}