-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
173 lines (139 loc) · 4.21 KB
/
main.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
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <map>
#include "crypt.h"
//#include "map.h"
using namespace std;
#define POWER_OF_TABLE 456976
int main(int argc, char * argv[]) {
int key_size = 32 / 8;
//struct map_t *first_table = map_create();
map<string, string> first_table;
Crypt crypt;
unsigned char *randKey = crypt.generateRandomKey(key_size);
crypt.setKey(randKey, key_size);
unsigned char text[] = "Secret Text";
//Crypt crypt(key, sizeof(key) - 1);
cout << "Source text: " << text << endl;
cout << "First key: " << randKey << endl;
std::string _encrypted = crypt.encrypt(text, sizeof(text) - 1);
printf("Encrypted text with first key: ");
for (int i = 0; i < _encrypted.size(); i++) {
printf("0x%hhx ", _encrypted.at(i));
}
printf("\n");
free(randKey);
randKey = crypt.generateRandomKey(key_size);
cout << "Second key: " << randKey << endl;
crypt.setKey(randKey, key_size);
std::string _reencrypted = crypt.encrypt(_encrypted);
printf("Encrypted text by two keys: ");
for (int i = 0; i < _reencrypted.size(); i++) {
printf("0x%hhx ", _reencrypted.at(i));
}
printf("\n");
unsigned char *seqKey = NULL;
cout.precision(3);
cout << "Generating first table..." << endl;
time_t _time = time(NULL);
for (int i = 0; i < POWER_OF_TABLE; i++) {
seqKey = crypt.generateSequentKey(key_size, i);
crypt.setKey(seqKey, key_size);
string encrypted = crypt.encrypt(text, sizeof(text) - 1);
first_table[encrypted] = string((char *)seqKey, key_size);
free(seqKey);
int map_size = first_table.size();
if (map_size % 1000 == 0) {
cout << "[";
for (int i = 0; i < 10; i++) {
if (map_size / (POWER_OF_TABLE * 1.) * 10 < i) {
cout << " ";
}
else if (map_size / (POWER_OF_TABLE * 1.) * 10 == i) {
cout << ">";
}
else
{
cout << "=";
}
}
cout << "] " << map_size / (POWER_OF_TABLE * 1.) * 100 << "%" << "\n\033[1A\033[2K";
}
}
cout << "First table were successfully generated!\n";
cout << "Generating second table and comparing it to the first one...\n";
for (int i = 0; i < POWER_OF_TABLE; i++) {
seqKey = crypt.generateSequentKey(key_size, i);
crypt.setKey(seqKey, key_size);
string decrypted = crypt.decrypt(_reencrypted);
if (first_table.find(decrypted) == first_table.end()) {
//first_table[decrypted] = string((char *)seqKey, key_size);
}
else {
cout << "I've found that pair of key in " << time(NULL) - _time << " seconds!\n";
cout << "First key: " << first_table[decrypted] << endl;
cout << "Second key: " << seqKey << endl;
free(seqKey);
break;
}
free(seqKey);
int map_size = i;
if (map_size % 1000 == 0) {
cout << "[";
for (int k = 0; k < 10; k++) {
if (map_size / (POWER_OF_TABLE * 1.) * 10 < k) {
cout << " ";
}
else if (map_size / (POWER_OF_TABLE * 1.) * 10 == k) {
cout << ">";
}
else
{
cout << "=";
}
}
cout << "] " << map_size / (POWER_OF_TABLE * 1.) * 100 << "%" << "\n\033[1A\033[2K";
}
}
_time = time(NULL);
cout << "Let us found key with brute-force search...\n";
double f = POWER_OF_TABLE;
double powered_table = f * f;
for (int i = 0; i < POWER_OF_TABLE; i++) {
unsigned char *firstKey = crypt.generateSequentKey(key_size, i);
for (int j = 0; j < POWER_OF_TABLE; j++) {
unsigned char *secondKey = crypt.generateSequentKey(key_size, j);
crypt.setKey(firstKey, key_size);
string firstEnc = crypt.encrypt(text, sizeof(text) - 1);
crypt.setKey(secondKey, key_size);
string secondEnc = crypt.encrypt(firstEnc);
if (secondEnc == _reencrypted) {
cout << "Found pair of keys in " << time(NULL) - _time << " seconds!\n";
cout << "First key: " << firstKey << endl;
cout << "Second key: " << secondKey << endl;
break;
}
int map_size = i * POWER_OF_TABLE + j;
if (map_size % 1000 == 0) {
cout << "[";
for (double k = 0; k < 10; k++) {
if (map_size / (powered_table) * 10 < k) {
cout << " ";
}
else if (map_size / (powered_table * 1.) * 10 == k) {
cout << ">";
}
else
{
cout << "=";
}
}
cout << "] " << map_size / (powered_table * 1.) * 100 << "%" << "\n\033[1A\033[2K";
}
}
}
free(randKey);
return 0;
}