-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptographic.cpp
More file actions
74 lines (58 loc) · 1.88 KB
/
cryptographic.cpp
File metadata and controls
74 lines (58 loc) · 1.88 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <map>
#include <random>
#include <numeric>
std::map<char, char> createMap();
void showMap(std::map<char,char> & m);
void encrypt(std::string & s, std::map<char,char> & m);
void decrypt(std::string & s, std::map<char,char> & m);
int main ()
{
std::string s = "Ala ma kota";
std::map<char, char> mc = createMap();
encrypt(s,mc);
std::cout << std::endl;
decrypt(s,mc);
std::cout << std::endl;
return 0;
}
std::map<char, char> createMap() {
std::vector<int> v(95);
std::iota(v.begin(), v.end(), 32);
std::vector<char> word(v.size());
std::vector<char> cypher(v.size());
std::transform(v.begin(), v.end(), word.begin(), [](int i) {return static_cast<char>(i);});
std::transform(v.begin(), v.end(), cypher.begin(), [](int i) {return static_cast<char>(i);});
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(cypher.begin(), cypher.end(), g);
std::map<char, char> m;
for(auto i = 0; i <= word.size(); i++ )
m.emplace(std::make_pair(word[i], cypher[i]));
return m;
}
void showMap(std::map<char,char> & m) {
for (const auto & a : m) {
std::cout << a.first << " " << a.second << std::endl;
}
}
void encrypt(std::string & s, std::map<char,char> & m) {
std::vector<char> cs(s.c_str(),s.c_str() + s.size() + 1);
for (const auto & i : cs) {
auto search = m.find(i);
if(search != m.end())
std::cout << search->second;
}
}
void decrypt(std::string & s, std::map<char,char> & m) {
std::vector<char> cs(s.c_str(),s.c_str() + s.size() + 1);
for (const auto & i : cs) {
auto search = std::find_if(m.begin(), m.end(),[=](const auto & m) {return m.second == i;});
if (search != m.end())
std::cout << search->first;
}
}