-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnigma.cpp
More file actions
51 lines (47 loc) · 1022 Bytes
/
Copy pathEnigma.cpp
File metadata and controls
51 lines (47 loc) · 1022 Bytes
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
#include "Enigma.hpp"
#include <iostream>
#include <cmath>
using namespace std;
Enigma::Enigma(const vector<Rotor> &rotors, const Plugboard &plugboard)
: plugboard(plugboard)
, reflector(Reflector())
, rotors(rotors)
, count(0)
{
}
int Enigma::encode(int n)
{
n = plugboard.get(n);
if(rotors.size() > 0)
{
for(vector<Rotor>::iterator it = rotors.begin(); it != rotors.end(); it++) {
n = it -> get(n);
}
}
n = reflector.get(n);
if(rotors.size() > 0)
{
for (vector<Rotor>::reverse_iterator it = rotors.rbegin();
it != rotors.rend(); it++ ) {
n = it -> getInverse(n);
}
}
n = plugboard.get(n);
if(rotors.size() > 0)
{
count++;
rotate();
}
return n;
}
void Enigma::rotate()
{
int rotor = 0;
for(double i = rotors.size() - 1; i >= 0; i--)
{
if(count % (int) (pow(26.0, i) + 0.5) == 0)
{
rotors.at(i).rotate();
}
}
}