forked from cliche-niche/CS641
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
153 lines (127 loc) · 4.82 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
#include "utils.hpp"
vector < vector<int> > a(8, vector<int> (8, 0));
vector <int> e(8, 0);
int main(){
vector< set< vector<int> > > candidates(8); // Candidates for (a_ii, e_i) pairs
vector< vector <vector <int> > > outputs_i(8);
for(int i = 0; i < 8; i++){
outputs_i[i] = get_vectors_i(i + 1, "fout.txt");
{
vector<int> temp;
for(auto v : outputs_i[i]){
temp.push_back(v[i]);
}
candidates[i] = solve_diag(temp);
}
}
// * At this point we have all the candidate pairs (a_ii, e_i), but no exact solutions
// * And the outputs corresponding to each 1-hot vector
// * We now solve for a_{i, i+1}
for(int i = 6; i >= 0; i--){
int cands = 0;
for(auto c_i : candidates[i]){
a[i][i] = c_i[0];
e[i] = c_i[1];
for(auto c_i1 : candidates[i + 1]){
a[i + 1][i + 1] = c_i1[0];
e[i + 1] = c_i1[1];
for(int c = 0; c < 128; c++){
a[i][i + 1] = c;
cands += check_sol(i, 1, outputs_i[i], a, e);
}
}
}
if(cands == 1){
cands = 0;
for(auto c_i : candidates[i]){
a[i][i] = c_i[0];
e[i] = c_i[1];
for(auto c_i1 : candidates[i + 1]){
a[i + 1][i + 1] = c_i1[0];
e[i + 1] = c_i1[1];
for(int c = 0; c < 128; c++){
a[i][i + 1] = c;
if(check_sol(i, 1, outputs_i[i], a, e)){
cands = 1;
vector<int> v;
candidates[i].clear();
v = {c_i[0], c_i[1]};
candidates[i].insert(v);
candidates[i + 1].clear();
v = {c_i1[0], c_i1[1]};
candidates[i + 1].insert(v);
break;
}
}
if(cands){
break;
}
}
if(cands){
break;
}
}
}else{
cout << "Multiple solutions found for a[" << i << "][" << i + 1 << "]" << endl;
return 0;
}
}
// * At this point, we know a_{ii}, e_i, a_{i, i+1} of the matrix exactly
// * We now just solve for a_{ij} one-by-one from lower-right to upper-left
for(int l = 2; l <= 7; l++){
for(int i = 7-l; i >= 0; i--){
int cands = 0, candidate;
for(int c = 0; c < 128; c++){
a[i][i + l] = c;
if(check_sol(i, l, outputs_i[i], a, e)){
cands++;
candidate = c;
}
}
if(cands == 1){
a[i][i + l] = candidate;
}else{
cout << "Multiple solutions found for a[" << i << "][" << i + l << "]" << endl;
return 0;
}
}
}
// * At this point, we have the complete matrix A, and the exponent vector E
cout << "The solution till now has been found to be:" << endl;
cout << "(Note: 0's might represent garbage value)\n" << endl;
cout << "E = [";
for(auto ei : e){
cout << ei << '\t';
}
cout << "]\n" << endl;
cout << "A = [\n";
for(auto i : a){
cout << "[";
for(auto j : i){
cout << j << '\t';
}
cout << "]" << endl;
}
cout << "]\n" << endl;
// Figuring out the required password
string encrypted_password = "gsgogpgflqlffhgrikljjmijfgjqjpio";
cout << "The given encrypted password is:\t\t\t\t" << encrypted_password << endl;
// Block size = 16. Hence we divide 32 length password into two substrings.
string left_encrypted_password = encrypted_password.substr(0,16);
string right_encrypted_password = encrypted_password.substr(16,16);
string decrypted_password = decrypt_password(left_encrypted_password, e, a) + decrypt_password(right_encrypted_password, e, a);
cout << "The decrypted password is:\t\t\t\t\t" << decrypted_password << endl;
string required_password = to_ascii_string(decrypted_password);
cout << "The ASCII string corresponding to the decrypted password is:\t" << required_password << endl;
cout << "The required password after removing trailing 0s is:\t\t";
// Removing trailing 0s
for(int i = required_password.size()-1; i >= 0; i--){
if(required_password[i]!='0'){
for(int j = 0; j <= i; j++){
cout << required_password[j];
}
break;
}
}
return 0;
}