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