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