Changeset e5b15b7 in comparacioncriptosistemas for testVarieties/main.cpp


Ignore:
Timestamp:
Jan 8, 2016, 11:41:41 AM (8 years ago)
Author:
usuario <usuario@…>
Branches:
master, interfaz
Children:
d828a1a
Parents:
ec3ce2c
Message:

Se agregó la ejecución múltiple del proceso de generación, cifrado y descifrado. Se almacenan los resultados en un archivo de texto separado por espacios con los valores tiempo de generación de claves, cifrado y descifrado.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • testVarieties/main.cpp

    rec3ce2c re5b15b7  
    11#include <QCoreApplication>
    22#include <QTime>
     3#include <QHash>
     4#include <QCryptographicHash>
     5#include <QFile>
     6#include <QTextStream>
     7#include <QDateTime>
    38
    49
     
    914#include "polynomial.h"
    1015
     16#include "executionresult.h"
     17
     18bool 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}
    1152
    1253
     
    2263    //return a.exec();
    2364
     65    int invalidCount = 0;
     66    int validCount = 0;
     67
     68
    2469    if (argc > 0 && argv) {
    2570        try {
    2671            hash_nm::precalc_mul_table();
    2772
    28             FILE *fout = fopen("encryptedMessage", "wb");
    29             File in("message.txt", READ);
    30 
     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();
     105
     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(&registerFile);
     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            /*
    31174            pol_t kx = get_rand_pol(KEY_DEG, false);
    32175            pol_t ky = get_rand_pol(KEY_DEG, false);
     
    59202            fclose(fin);
    60203            out.close();
     204            */
     205
    61206
    62207        }
     
    72217        }
    73218    }
    74 
    75 
    76 
     219    qDebug("invalidCount %d", invalidCount);
    77220    qDebug("salida.");
    78221
    79222    return 0;
    80223}
     224
     225
Note: See TracChangeset for help on using the changeset viewer.