1 | #include "dialog.h" |
---|
2 | #include "ui_dialog.h" |
---|
3 | #include <QMessageBox> |
---|
4 | #include<QProgressDialog> |
---|
5 | |
---|
6 | #include <QTime> |
---|
7 | #include <QHash> |
---|
8 | #include <QCryptographicHash> |
---|
9 | #include <QFile> |
---|
10 | #include <QTextStream> |
---|
11 | #include <QDateTime> |
---|
12 | #include <QDebug> |
---|
13 | #include <iostream> |
---|
14 | #include <ctime> |
---|
15 | |
---|
16 | #include "polynomial.h" |
---|
17 | #include "executionresult.h" |
---|
18 | |
---|
19 | using namespace std; |
---|
20 | using namespace pol; |
---|
21 | |
---|
22 | const int iterations = 20; |
---|
23 | |
---|
24 | Dialog::Dialog(QWidget *parent) : |
---|
25 | QDialog(parent), |
---|
26 | ui(new Ui::Dialog) |
---|
27 | { |
---|
28 | ui->setupUi(this); |
---|
29 | setWindowTitle("*** Criptosistema ***"); |
---|
30 | ui->pushButton_2->setEnabled(false); |
---|
31 | ui->pushButton_3->setEnabled(false); |
---|
32 | connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(GenerarClaves())); |
---|
33 | |
---|
34 | } |
---|
35 | |
---|
36 | Dialog::~Dialog() |
---|
37 | { |
---|
38 | delete ui; |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | bool Dialog::identicalFiles(QString f1, QString f2) |
---|
43 | { |
---|
44 | |
---|
45 | QCryptographicHash hash1( QCryptographicHash::Sha1 ); |
---|
46 | QCryptographicHash hash2( QCryptographicHash::Sha1 ); |
---|
47 | |
---|
48 | |
---|
49 | QFile file1( f1 ); |
---|
50 | |
---|
51 | if ( file1.open( QIODevice::ReadOnly ) ) { |
---|
52 | hash1.addData( file1.readAll() ); |
---|
53 | } else { |
---|
54 | // Handle "cannot open file" error |
---|
55 | } |
---|
56 | |
---|
57 | QFile file2( f2 ); |
---|
58 | |
---|
59 | if ( file2.open( QIODevice::ReadOnly ) ) { |
---|
60 | hash2.addData( file2.readAll() ); |
---|
61 | } else { |
---|
62 | // Handle "cannot open file" error |
---|
63 | } |
---|
64 | |
---|
65 | if (hash1.result() == hash2.result()){ |
---|
66 | qDebug(" hash1.result() == hash2.result()"); |
---|
67 | return true; |
---|
68 | } |
---|
69 | else{ |
---|
70 | qDebug(" diferentes"); |
---|
71 | return false; |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | void Dialog::GenerarClaves() |
---|
77 | { |
---|
78 | |
---|
79 | QMessageBox::information(this,"Alarta","Generando la clave......!!!"); |
---|
80 | |
---|
81 | /////////////// |
---|
82 | |
---|
83 | int numTasks = 100000; |
---|
84 | QProgressDialog progress("Task in progress...", "Cancel", 0, numTasks, this); |
---|
85 | progress.setWindowModality(Qt::WindowModal); |
---|
86 | |
---|
87 | for (int i = 0; i < numTasks; i++) { |
---|
88 | progress.setValue(i); |
---|
89 | |
---|
90 | if (progress.wasCanceled()) |
---|
91 | break; |
---|
92 | } |
---|
93 | progress.setValue(numTasks); |
---|
94 | |
---|
95 | |
---|
96 | // |
---|
97 | int invalidCount = 0; |
---|
98 | hash_nm::precalc_mul_table(); |
---|
99 | |
---|
100 | QTime timer; |
---|
101 | QHash<int, ExecutionResult*> results; |
---|
102 | |
---|
103 | |
---|
104 | int keyTime = 0; |
---|
105 | int encryptionTime = 0; |
---|
106 | int decryptionTime = 0; |
---|
107 | |
---|
108 | // repeticiones |
---|
109 | //for (int i = 0; i < 50; i++){ |
---|
110 | |
---|
111 | for(int i=1;i<=8;i++) { |
---|
112 | |
---|
113 | QString messageFile = "message"; |
---|
114 | QString textEncrip = "encryptedMessage"; |
---|
115 | QString textDecrip = "decryptedMessage"; |
---|
116 | messageFile.append(QString::number(i)); |
---|
117 | textEncrip.append(QString::number(i)); |
---|
118 | textDecrip.append(QString::number(i)); |
---|
119 | |
---|
120 | qDebug("generar archivo..."); |
---|
121 | // generar registros en archivo de texto |
---|
122 | QString fileName = "registro-superficie-"; |
---|
123 | fileName.append(messageFile+"-"); |
---|
124 | fileName.append(QDateTime::currentDateTime().toString("dd.MM.yy.hh.mm.ss")); |
---|
125 | fileName.append(".txt"); |
---|
126 | |
---|
127 | messageFile.append(".txt"); |
---|
128 | textEncrip.append(".txt"); |
---|
129 | textDecrip.append(".txt"); |
---|
130 | //qDebug("antes del while"); |
---|
131 | int validCount = 0; |
---|
132 | |
---|
133 | while ( true ) { |
---|
134 | |
---|
135 | FILE *fout = fopen(qPrintable(textEncrip), "wb"); |
---|
136 | QFile file( qPrintable(messageFile) ); |
---|
137 | |
---|
138 | if ( file.open(QIODevice::ReadWrite) ) |
---|
139 | { |
---|
140 | QTextStream stream( &file ); |
---|
141 | stream << "something" << endl; |
---|
142 | } |
---|
143 | |
---|
144 | //File in("message1.txt", READ); |
---|
145 | //qDebug(qPrintable(messageFile)); |
---|
146 | File in(qPrintable(messageFile), READ); |
---|
147 | |
---|
148 | pol_t kx = get_rand_pol(KEY_DEG, false); |
---|
149 | pol_t ky = get_rand_pol(KEY_DEG, false); |
---|
150 | |
---|
151 | |
---|
152 | // generacion de clave? |
---|
153 | timer.start(); |
---|
154 | pol3v_t D = get_open_key(kx, ky); |
---|
155 | |
---|
156 | keyTime = timer.elapsed(); //gets the runtime in ms |
---|
157 | |
---|
158 | // cifrado |
---|
159 | timer.start(); |
---|
160 | encrypt(in, fout, D); |
---|
161 | encryptionTime = timer.elapsed(); //gets the runtime in ms |
---|
162 | |
---|
163 | fclose(fout); |
---|
164 | in.~File(); |
---|
165 | |
---|
166 | FILE *fin = fopen(qPrintable(textEncrip), "rb"); |
---|
167 | File out(qPrintable(textDecrip), WRITE); |
---|
168 | |
---|
169 | // descifrado |
---|
170 | timer.start(); |
---|
171 | decrypt(fin, out, kx, ky); |
---|
172 | decryptionTime = timer.elapsed(); //gets the runtime in ms |
---|
173 | |
---|
174 | fclose(fin); |
---|
175 | out.close(); |
---|
176 | |
---|
177 | // verificacion |
---|
178 | //if (identicalFiles("message1.txt", "decryptedMessage.txt")){ |
---|
179 | if (Dialog::identicalFiles(qPrintable(messageFile), qPrintable(textDecrip))) { |
---|
180 | validCount++; |
---|
181 | ExecutionResult * execution = new ExecutionResult(keyTime, encryptionTime, decryptionTime); |
---|
182 | results.insert(validCount, execution); |
---|
183 | |
---|
184 | } |
---|
185 | else{ |
---|
186 | invalidCount++; |
---|
187 | } |
---|
188 | |
---|
189 | //validCount++; |
---|
190 | if (validCount == 1) { |
---|
191 | |
---|
192 | //qDebug("generar archivo..."); |
---|
193 | // generar registros en archivo de texto |
---|
194 | //QString fileName = "registro-"; |
---|
195 | //fileName.append(QDateTime::currentDateTime().toString("dd.MM.yy.hh.mm.ss")); |
---|
196 | //fileName.append(".txt"); |
---|
197 | |
---|
198 | QFile registerFile(fileName); |
---|
199 | if (registerFile.open(QFile::WriteOnly)) { |
---|
200 | QTextStream out(®isterFile); |
---|
201 | |
---|
202 | //out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7; |
---|
203 | // writes "Result: 3.14 2.7 " |
---|
204 | |
---|
205 | QString line; |
---|
206 | for (int j = 1; j <= validCount; j++) { |
---|
207 | ExecutionResult * x = results.value(j); |
---|
208 | int k = x->getKeyGenerationTime(); |
---|
209 | //qDebug("k: %d",k); |
---|
210 | |
---|
211 | int e = x->getEncryptionTime(); |
---|
212 | //qDebug("e: %d",e); |
---|
213 | |
---|
214 | int d = x->getDecryptionTime(); |
---|
215 | //qDebug("d: %d",d); |
---|
216 | |
---|
217 | line = QString::number(k) + " " + QString::number(e) + " " + QString::number(d) + "\n"; |
---|
218 | //qDebug(qPrintable(line)); |
---|
219 | out << line; |
---|
220 | line.clear(); |
---|
221 | } |
---|
222 | |
---|
223 | } |
---|
224 | |
---|
225 | qDebug("Fin del while"); |
---|
226 | break; |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | qDebug("invalidCount %d", invalidCount); |
---|
232 | qDebug("Salida Final."); |
---|
233 | |
---|
234 | for (int i= 0; i< 76; i++){ |
---|
235 | |
---|
236 | int value = this->ui->progressBar->value(); |
---|
237 | this->ui->progressBar->setValue(value+1); |
---|
238 | |
---|
239 | for (int j = 0; j< 2000; j++){ |
---|
240 | cout << value+1 << endl; |
---|
241 | cout << "------" << endl; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | QMessageBox::information(this,"Alarta","Listo......!!!"); |
---|
247 | this->ui->progressBar->deleteLater(); |
---|
248 | |
---|
249 | QString style = "QPushButton{ background-color: rgb(38, 90, 136); background-position: 0 -15px; color: rgb(255, 255, 255);}"; |
---|
250 | ui->pushButton_2->setStyleSheet(style); |
---|
251 | ui->pushButton_3->setStyleSheet(style); |
---|
252 | |
---|
253 | ui->pushButton_2->setEnabled(true); |
---|
254 | ui->pushButton_3->setEnabled(true); |
---|
255 | |
---|
256 | QString stylee = "QPushButton{ }"; |
---|
257 | ui->pushButton->setStyleSheet(stylee); |
---|
258 | ui->pushButton->setEnabled(false); |
---|
259 | |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | |
---|