-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
311 lines (288 loc) · 8.69 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <string.h>
#include <fstream>
using namespace std;
string folder = "criptografia/", tp_file = ".txt"; // Folder with files
/********************************************//**
* \brief Convert Numbers to String
*
* \param Get int (T)
* \return string
*
* [Converting numbers to strings and strings to numbers] [http://www.cplusplus.com/forum/articles/9645/])
***********************************************/
template <typename T>
string NumberToString(T pNumber)
{
ostringstream oOStrStream;
oOStrStream << pNumber;
return oOStrStream.str();
}
/********************************************//**
* \brief For create txt with messages
*
* \param message (string) with the message
* \param filename (string) with filename
* \return true or false (if sucess return true)
*
***********************************************/
bool create_message(string message, string filename)
{
ofstream arq;
filename = folder+filename+tp_file;
arq.open(filename.c_str()); // fix: Colocar uma variavel para criação de várias mensagens
if (arq.is_open()) // Se houve erro na abertura
{
arq << message; /* Grava a string, caractere a caractere */
arq.close();
return true;
}
else
return false;
}
/********************************************//**
* \brief For read txt
*
* \param filename (string) with filename for read
* \return string with the message
*
***********************************************/
string read_message(string filename)
{
filename = folder+filename+tp_file;
ifstream arq_read(filename.c_str());
string letra="";
if (arq_read.is_open()) // Se houve erro na abertura
{
while(!arq_read.eof())
getline(arq_read, letra); // Saves the line in letra.
arq_read.close();
return letra;
}
else
return "false";
}
/********************************************//**
* \brief Concatenate string with Integer
*
* \param name (string)
* \param i (integer)
* \return string with the result concatenation
*#include <string.h>
***********************************************/
string concatenate(std::string const& name, int i)
{
stringstream s;
s << name << i;
return s.str();
}
/********************************************//**
* \brief Convert text for ASCII
*
* \param letter (string)
* \return string with the ASCII Code
*
* [Converting string to ASCII] [http://stackoverflow.com/questions/6709795/converting-string-to-ascii]
***********************************************/
string convertToASCII(string letter)
{
string result = "";
unsigned int i;
for (i = 0; i < letter.length(); i++)
{
char x = letter.at(i);
/**< fix ASCII code :) */
if (int(x)<10)
result = result + "00";
if (int(x)<100 && int(x)>9)
result = result + "0";
result = concatenate(result, int(x));
}
return result;
}
/********************************************//**
* \brief Function for Encrypt message
* \todo Correct bug
*
* \param letter (string)
* \return string with the ASCII Code
*
***********************************************/
string Encrypt(string message, int e, int n)
{
string Result, temp;
long long m_c;
unsigned int i;
message = convertToASCII(read_message(message)); // read txt and convert to ASCII
long long m;
for (i = 0; i < message.length(); i +=3 )
{
temp = NumberToString(message[i]) + NumberToString(message[i+1]) + NumberToString(message[i+2]);
m = 1;
std::stringstream buffer(temp);
buffer >> m_c;
for (int j = 0; j < e; j++)
m = (m*m_c) % n;
m = m % n;
if (m<10000000)
Result = Result + "0"; // fix break characters
Result = Result + NumberToString(m);
}
Result = Result;
return Result;
}
/********************************************//**
* \brief Function for Decrypt message
* \todo Correct bug
*
* \param message (string)
* \param d (int)
* param n (int)
* \return string with the ASCII Code
*
***********************************************/
string Decrypt(string message, int d, int n)
{
string Result, temp;
long long m_c = 0;
unsigned int i;
message = read_message(message);
long long m;
for (i = 0; i < message.length(); i+=8 )
{
temp = NumberToString(message[i]) + NumberToString(message[i+1]) + NumberToString(message[i+2]) + NumberToString(message[i+3]) + NumberToString(message[i+4]) + NumberToString(message[i+5]) + NumberToString(message[i+6]) + NumberToString(message[i+7]);
m_c = 0;
m = 1;
stringstream buffer(temp);
buffer >> m_c;
for (int j = 0; j < d; j++)
m = (m*m_c) % n;
m = m % n;
Result = Result + NumberToString(m);
}
return Result;
}
/********************************************//**
* \brief Function for check public key
*
* \param int phi
* \return e - public key
*
***********************************************/
long long check(int phi)
{
int i=0, FLAG = 0, e=0;
do
{
cout << "\n Insire a chave publica (4253):\n";
cin >> e; // chave pública
for(i=3;e%i==0 && phi%i==0;i+=2)
FLAG = 1;
FLAG = 0;
}while(FLAG==1);
return e;
}
/********************************************//**
* \brief Function for Euler teorem
*
* \param e - public key
* \return Int, if validate key return FLAG 0
* [Teorema de Euler] [http://pt.wikipedia.org/wiki/Fun%C3%A7%C3%A3o_totiente_de_Euler]
***********************************************/
long long tot_euler(long long p, long long q)
{
long long phi=0;
phi=(p-1)*(q-1);
return phi;
}
/********************************************//**
* \brief Function to calculate the private key
*
* \param e - public key
* \return Int, if validate key return FLAG 0
* [Teorema de Euler] [http://pt.wikipedia.org/wiki/Fun%C3%A7%C3%A3o_totiente_de_Euler]
***********************************************/
long long calc_private(long long e, long long phi)
{
long long d = 1, s = 0;
do
{
s = (d*e)%phi;
d++;
}while(s!=1);
d = d-1;
return d;
}
/********************************************//**
* \brief Main Function
*
* \return int 0 for end
*
***********************************************/
int main()
{
string message = "", filename = "";
int menu = 0;
long long p = 4229, q = 4283; // Números primos
long long n = 0, phi = 0, e = 0;
n = p * q;
phi=tot_euler(p, q);
e = check(phi);
cout << "\n\Chave Publica: {" << e << ", " << n << "}";
do
{
system("cls");
cout << "Menu: \n1 - Criar texto\n2 - Criptografar\n3 - Descriptografar\n4 - Sair\n";
cin >> menu;
fflush(stdin);
switch (menu)
{
case 1:
system("cls");
cout << "Digite o nome do arquivo:\n";
getline(cin, filename);
fflush(stdin);
cout << "Digite a sua mensagem:\n";
getline(cin, message);
fflush(stdin);
if (create_message(message, filename)) // Se houve erro na abertura
cout << "Arquivo criado com exito\n";
else
cout << "Problemas na abertura do arquivo\n";
system("pause");
break;
case 2:
system("cls");
cout << "\n Digite o nome do arquivo:";
cin >> filename;
if (create_message(Encrypt(filename, e, n), filename)) // Se houve erro na abertura
cout << "Arquivo criptogrado com sucesso\n";
else
cout << "Problemas ao criptografar o arquivo, verifique se o arquivo existe\n";
system("pause");
break;
case 3:
system("cls");
cout << "\n Digite o nome do arquivo:";
cin >> filename;
cout << "\Chave Privada: {" << calc_private(e, phi) << ", " << n << "}";
if (create_message(Decrypt(filename, calc_private(e, phi), n), filename)) // Se houve erro na abertura
cout << "\nArquivo criptogrado com sucesso\n";
else
cout << "\nProblemas ao criptografar o arquivo, verifique se o arquivo existe\n";
system("pause");
break;
case 4:
menu = 4;
break;
default:
cout << "\nOpcao invalida\n";
system("pause");
menu = 5;
break;
}
} while (menu!=4);
return 0;
}