-
Notifications
You must be signed in to change notification settings - Fork 0
/
Machine.cpp
87 lines (67 loc) · 1.99 KB
/
Machine.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
#include <iostream>
#include <fstream>
#include <string>
#include "Machine.hpp"
#include "Plugboard.hpp"
#include "Rotor.hpp"
#include "Reflector.hpp"
using namespace std;
Machine::Machine(int argc, char **argv) {
this->numRotors = argc - 1;
rotors.resize(numRotors);
int indexRotor = 0;
while (argc > 0) {
if (argc == 1) {
plugboard.readfile(*argv);
} else {
Rotor rotor;
rotor.readfile(*argv);
rotors[indexRotor] = rotor;
indexRotor++;
}
argc--;
argv++;
}
}
string Machine::encrypt(string input) {
string output;
for (char c : input) {
//convert into number
int index = c - 'A';
//go through plugboard
index = plugboard.swap(index);
//rotors
for (int i = 0; i < numRotors; i++) {
index = rotors[i].rotate(index);
}
index = reflector.reflect(index);
//rotors backward
if (numRotors > 0) {
rotors[0].canRotate = true;
for (int j = numRotors - 1; j >= 0; j--) {
index = rotors[j].rotateBackwards(index);
if (rotors[j].canRotate) {
rotors[j].numRotation++;
rotors[j].offset++;
//rotate the next rotor
if (j + 1 < numRotors) {
if (rotors[j].numRotation % 26 == 25) {
rotors[j + 1].canRotate = true;
}
}
//only rotors[0] can rotate
if (j != 0) {
rotors[j].canRotate = false;
}
}
}
}
//go through plugboard again
index = plugboard.swap(index);
//convert into upper case character
char c_output = (char) (index + 'A');
//append to output string
output.push_back(c_output);
}
return output;
}