[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; |
---|
| 66 | int validCount = 0; |
---|
| 67 | |
---|
| 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 | while ( true ) { |
---|
| 84 | |
---|
| 85 | FILE *fout = fopen("encryptedMessage", "wb"); |
---|
| 86 | File in("message.txt", READ); |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | pol_t kx = get_rand_pol(KEY_DEG, false); |
---|
| 90 | pol_t ky = get_rand_pol(KEY_DEG, false); |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | // generacion de clave? |
---|
| 94 | timer.start(); |
---|
| 95 | pol3v_t D = get_open_key(kx, ky); |
---|
| 96 | keyTime = timer.elapsed(); //gets the runtime in ms |
---|
| 97 | |
---|
| 98 | // cifrado |
---|
| 99 | timer.start(); |
---|
| 100 | encrypt(in, fout, D); |
---|
| 101 | encryptionTime = timer.elapsed(); //gets the runtime in ms |
---|
| 102 | |
---|
| 103 | fclose(fout); |
---|
| 104 | in.~File(); |
---|
[b939736] | 105 | |
---|
[e5b15b7] | 106 | FILE *fin = fopen("encryptedMessage", "rb"); |
---|
| 107 | File out("decryptedMessage.txt", WRITE); |
---|
| 108 | |
---|
| 109 | // descifrado |
---|
| 110 | timer.start(); |
---|
| 111 | decrypt(fin, out, kx, ky); |
---|
| 112 | decryptionTime = timer.elapsed(); //gets the runtime in ms |
---|
| 113 | |
---|
| 114 | fclose(fin); |
---|
| 115 | out.close(); |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | // verificacion |
---|
| 119 | if (identicalFiles("message.txt", "decryptedMessage.txt")){ |
---|
| 120 | validCount++; |
---|
| 121 | ExecutionResult * execution = new ExecutionResult(keyTime, encryptionTime, decryptionTime); |
---|
| 122 | results.insert(validCount, execution); |
---|
| 123 | |
---|
| 124 | } |
---|
| 125 | else{ |
---|
| 126 | invalidCount++; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | //validCount++; |
---|
| 132 | if (validCount == 1){ |
---|
| 133 | |
---|
| 134 | qDebug("generar archivo..."); |
---|
| 135 | // generar registros en archivo de texto |
---|
| 136 | QString fileName = "registro-"; |
---|
| 137 | fileName.append(QDateTime::currentDateTime().toString("dd.MM.yy.hh.mm.ss")); |
---|
| 138 | fileName.append(".txt"); |
---|
| 139 | |
---|
| 140 | QFile registerFile(fileName); |
---|
| 141 | if (registerFile.open(QFile::WriteOnly)) { |
---|
| 142 | QTextStream out(®isterFile); |
---|
| 143 | |
---|
| 144 | //out << "Result: " << qSetFieldWidth(10) << left << 3.14 << 2.7; |
---|
| 145 | // writes "Result: 3.14 2.7 " |
---|
| 146 | |
---|
| 147 | QString line; |
---|
| 148 | for (int j = 1; j <= validCount; j++){ |
---|
| 149 | ExecutionResult * x = results.value(j); |
---|
| 150 | int k = x->getKeyGenerationTime(); |
---|
| 151 | //qDebug("k: %d",k); |
---|
| 152 | |
---|
| 153 | int e = x->getEncryptionTime(); |
---|
| 154 | //qDebug("e: %d",e); |
---|
| 155 | |
---|
| 156 | int d = x->getDecryptionTime(); |
---|
| 157 | //qDebug("d: %d",d); |
---|
| 158 | |
---|
| 159 | line = QString::number(k) + " " + QString::number(e) + " " + QString::number(d); |
---|
| 160 | //qDebug(qPrintable(line)); |
---|
| 161 | out << line; |
---|
| 162 | line.clear(); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | break; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /* |
---|
[b939736] | 174 | pol_t kx = get_rand_pol(KEY_DEG, false); |
---|
| 175 | pol_t ky = get_rand_pol(KEY_DEG, false); |
---|
| 176 | |
---|
| 177 | QTime timer; |
---|
| 178 | timer.start(); |
---|
| 179 | |
---|
| 180 | // generacion de clave? |
---|
| 181 | pol3v_t D = get_open_key(kx, ky); |
---|
| 182 | |
---|
| 183 | int runtime = timer.elapsed(); //gets the runtime in ms |
---|
| 184 | qDebug(qPrintable("key generation: "+QString::number(runtime)+" ms")); |
---|
| 185 | |
---|
| 186 | timer.start(); |
---|
| 187 | encrypt(in, fout, D); |
---|
| 188 | runtime = timer.elapsed(); //gets the runtime in ms |
---|
| 189 | qDebug(qPrintable("encryption duration: "+QString::number(runtime)+" ms")); |
---|
| 190 | |
---|
| 191 | fclose(fout); |
---|
| 192 | in.~File(); |
---|
| 193 | |
---|
[ec3ce2c] | 194 | FILE *fin = fopen("encryptedMessage", "rb"); |
---|
| 195 | File out("decryptedMessage.txt", WRITE); |
---|
[b939736] | 196 | |
---|
| 197 | timer.start(); |
---|
| 198 | decrypt(fin, out, kx, ky); |
---|
| 199 | runtime = timer.elapsed(); //gets the runtime in ms |
---|
| 200 | qDebug(qPrintable("decryption duration: "+QString::number(runtime)+" ms")); |
---|
| 201 | |
---|
| 202 | fclose(fin); |
---|
| 203 | out.close(); |
---|
[e5b15b7] | 204 | */ |
---|
| 205 | |
---|
[b939736] | 206 | |
---|
| 207 | } |
---|
| 208 | catch (char *str) { |
---|
| 209 | |
---|
| 210 | printf("exception"); |
---|
| 211 | |
---|
| 212 | cout << str << endl; |
---|
| 213 | |
---|
| 214 | qDebug("catch"); |
---|
| 215 | |
---|
| 216 | scanf("\n"); |
---|
| 217 | } |
---|
| 218 | } |
---|
[e5b15b7] | 219 | qDebug("invalidCount %d", invalidCount); |
---|
[b939736] | 220 | qDebug("salida."); |
---|
| 221 | |
---|
| 222 | return 0; |
---|
| 223 | } |
---|
[e5b15b7] | 224 | |
---|
| 225 | |
---|