-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.cpp
More file actions
182 lines (143 loc) · 4.72 KB
/
Copy pathindex.cpp
File metadata and controls
182 lines (143 loc) · 4.72 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
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
173
174
175
176
177
178
179
180
181
182
#include <bits/stdc++.h>
#include "functions.h"
using namespace std;
void generatePublicKey(mpz_class e_mpz, mpz_class n_mpz){
string e = e_mpz.get_str();
string n = n_mpz.get_str();
write_file("publicKey.txt", n, e);
}
void encrypt(string msg, string n_str, string e_str){
char alphabet[27] = {'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' '};
transform(msg.begin(), msg.end(), msg.begin(), ::toupper);
int size = msg.size();
vector <mpz_class> encryptMsg;
mpz_class e, n;
e = e_str;
n = n_str;
for(int i = 0; i < size; i++){
mpz_class m;
m = find(msg[i], alphabet);
if(m == -1) continue;
mpz_class c;
mpz_powm(c.get_mpz_t(), m.get_mpz_t(), e.get_mpz_t(), n.get_mpz_t());
encryptMsg.push_back(c);
}
write_msg("encryptedMsg.txt", encryptMsg);
}
void decrypt(string p_str, string q_str, string e_str){
mpz_class p, q, e;
p = p_str;
q = q_str;
e = e_str;
char alphabet[27] = {'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' '};
mpz_class totiente, d;
totiente = (p - 1) * (q - 1);
d = getReverse(e, totiente);
vector <mpz_class> numbers = read_file("encryptedMsg.txt");
vector <mpz_class> m;
for(int i = 0; i < numbers.size(); i++){
mpz_class ch, base, n;
n = p * q;
mpz_powm(ch.get_mpz_t(), numbers[i].get_mpz_t(), d.get_mpz_t(), n.get_mpz_t() );
m.push_back(ch);
}
string message;
for(int i = 0; i < m.size(); i++){
int index = mpz_to_int(m[i]);
message.push_back(alphabet[index - 2]);
}
write_file("decryptedMsg.txt", message, "");
}
int main(){
printf("------------------------\n");
printf("-------ℝ𝕊𝔸 ℂℝ𝕐ℙ𝕋𝕆-------\n");
printf("------------------------\n\n");
int option, p, q, e;
printf("Escolha uma das opções:\n\n");
printf("Gerar chave pública [1]\n");
printf("Encriptar [2]\n");
printf("Desencriptar [3]\n");
cin >> option;
if(option == 1){
string e, p, q;
printf("Digite um número primo p: \n");
cin >> p;
mpz_class p_mpz;
p_mpz = p;
while (!primo(p_mpz))
{
printf("Valor digitado é inválido! Digite um valor válido\n");
cin >> p;
p_mpz = p;
}
printf("Digite um número primo q:\n");
cin >> q;
mpz_class q_mpz;
q_mpz = q;
while (!primo(q_mpz))
{
printf("Valor digitado é inválido! Digite um valor válido\n");
cin >> q;
q_mpz = q;
}
printf("Digite o expoente e: \n");
cin >> e;
mpz_class totiente_mpz, e_mpz ;
e_mpz = e;
totiente_mpz = (p_mpz - 1) * (q_mpz - 1);
while(coprimos(e_mpz, totiente_mpz) != 1 || e_mpz < 1 || e_mpz > totiente_mpz){
printf("Valor digitado é inválido! Digite um valor válido\n");
cin >> e;
e_mpz = e;
}
generatePublicKey(e_mpz, p_mpz * q_mpz);
}
else if(option == 2){
string msg, n, e;
printf("Digite a mensagem a ser criptografada:\n");
scanf("\n");
getline(cin, msg);
printf("Insira a chave pública(n, e)\n");
cin >> n >> e;
encrypt(msg, n, e);
}
else if( option == 3){
string e, p, q;
printf("Digite um número primo p:\n");
cin >> p;
mpz_class p_mpz;
p_mpz = p;
while (!primo(p_mpz))
{
printf("Valor digitado é inválido! Digite um valor válido\n");
cin >> p;
p_mpz = p;
}
printf("Digite um número primo q:\n");
cin >> q;
mpz_class q_mpz;
q_mpz = q;
while (!primo(q_mpz))
{
printf("Valor digitado é inválido! Digite um valor válido\n");
cin >> q;
q_mpz = q;
}
printf("Digite um expoente e:\n");
cin >> e;
mpz_class totiente_mpz, e_mpz ;
e_mpz = e;
totiente_mpz = (p_mpz - 1) * (q_mpz - 1);
while(coprimos(e_mpz, totiente_mpz) != 1 || e_mpz < 1 || e_mpz > totiente_mpz){
printf("Valor digitado é inválido! Digite um valor válido\n");
cin >> e;
e_mpz = e;
}
decrypt(p, q, e);
}
return 0;
}