-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
119 lines (102 loc) · 2.87 KB
/
Copy pathmain.cpp
File metadata and controls
119 lines (102 loc) · 2.87 KB
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
#include <iostream>
#include <cstring>
#include <cstdint>
#include <array>
#include <limits>
#include "Menu.h"
#include "Global.h"
#include "Rotor.h"
namespace Config
{
std::array<int, 3> rotors{0, 1, 2};
std::array<int, 3> rotations{0, 0, 0};
}
class Enigma
{
std::array<Rotor, 3> m_rotors{Global::DefRotors[0], Global::DefRotors[1], Global::DefRotors[2]};
void rotateRotors()
{
m_rotors[0].rotate();
if(m_rotors[0].getRotations() % 5 == 0)
m_rotors[1].rotate();
if(m_rotors[1].getRotations() % 5 == 0)
m_rotors[2].rotate();
}
char reflect(char c)
{
return Global::g_stdalph[(94 / 2 + (94 / 2 - Global::g_stdMap[c])) - 1];
}
public:
Enigma() = default;
Enigma(const std::array<int, 3>& rotors, const std::array<int, 3>& rotations)
:m_rotors{Global::DefRotors[rotors[0]], Global::DefRotors[rotors[1]], Global::DefRotors[rotors[2]]}
{
m_rotors[0].rotate(rotations[0]);
m_rotors[1].rotate(rotations[1]);
m_rotors[2].rotate(rotations[2]);
}
char pass(char c)
{
c = m_rotors[0].passForward(c);
c = m_rotors[1].passForward(c);
c = m_rotors[2].passForward(c);
c = reflect(c);
c = m_rotors[2].passBackward(c);
c = m_rotors[1].passBackward(c);
c = m_rotors[0].passBackward(c);
rotateRotors();
return c;
}
};
namespace Global
{
Enigma enigma{Config::rotors, Config::rotations};
}
void reSeed()
{
Global::enigma = Enigma(Config::rotors, Config::rotations);
}
void setting()
{
std::cout << "Current Config :\nRotors : {" << Config::rotors[0] << ", " << Config::rotors[1] << ", " << Config::rotors[2] << "}"
<< "\nRotations : {" << Config::rotations[0] << ", " << Config::rotations[1] << ", " << Config::rotations[2] << "}\n";
std::cout << "Enter new values : \n" << "Rotors (enter 3 values seperated with space (' ') 0 to 4 e.g: 0 4 3 ): \n";
std::cin >> Config::rotors[0];
std::cin >> Config::rotors[1];
std::cin >> Config::rotors[2];
std::cout << "Rotations (enter 3 values seperated with space (' ') e.g: 10 45 61 ): \n";
std::cin >> Config::rotations[0];
std::cin >> Config::rotations[1];
std::cin >> Config::rotations[2];
reSeed();
}
void encrypt()
{
std::cout << "Enter Your Text : \n";
std::string input;
std::getline(std::cin, input);
std::cout << "-----------\n";
for(const auto& c : input)
{
std::cout << Global::enigma.pass(c);
}
std::cout << "\n\n";
reSeed();
}
int main()
{
while(true)
{
std::cout << Menu::home;
int com{};
std::cin >> com;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (com == 1)
setting();
if(com == 2)
break;
if(com == 0)
encrypt();
}
return 0;
}