[0e18918] | 1 | #include <QCoreApplication> |
---|
| 2 | |
---|
| 3 | #include <QTime> |
---|
| 4 | #include <QElapsedTimer> |
---|
| 5 | #include <QFile> |
---|
| 6 | #include <QTextStream> |
---|
| 7 | |
---|
| 8 | #include <iostream> |
---|
| 9 | |
---|
| 10 | #include "crypto++/rsa.h" |
---|
| 11 | #include "crypto++/elgamal.h" |
---|
| 12 | #include "crypto++/osrng.h" |
---|
| 13 | #include <crypto++/files.h> |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /** |
---|
| 18 | * Programa de Cryptopp para realizar estimaciones de ejecuciones del |
---|
| 19 | * algoritmo EL GAMAL |
---|
| 20 | * |
---|
| 21 | * https://www.cryptopp.com/wiki/ElGamal |
---|
| 22 | * |
---|
| 23 | * |
---|
| 24 | * Es necesario instalar los siguientes paquetes: |
---|
| 25 | * libcrypto++9 y libcrypto++-dev |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | using namespace CryptoPP; |
---|
| 30 | |
---|
| 31 | int main(int argc, char *argv[]) |
---|
| 32 | { |
---|
| 33 | QCoreApplication a(argc, argv); |
---|
| 34 | //return a.exec(); |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | /////////////////////////////////////// |
---|
[24aa974] | 38 | // Pseudo Random Number Generator el gamal |
---|
[0e18918] | 39 | AutoSeededRandomPool rng; |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | //QTime timer; |
---|
| 43 | QElapsedTimer timer; |
---|
| 44 | |
---|
| 45 | //int runtime = timer.elapsed(); //gets the runtime in ms |
---|
| 46 | |
---|
| 47 | // prefijo del nombre del archivo con el texto a cifrar |
---|
| 48 | QString messageFile = "message"; |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | //////////////////////////////////////////////// |
---|
| 52 | // Secret to protect |
---|
| 53 | //static const int SECRET_SIZE = 16; |
---|
| 54 | //SecByteBlock plaintext( SECRET_SIZE ); |
---|
| 55 | //memset( plaintext, 'A', SECRET_SIZE ); |
---|
| 56 | |
---|
[24aa974] | 57 | // iterar por el número de archivos que se van a cifrar (10) |
---|
[c48d843] | 58 | for (int i = 1; i <= 8; i++) |
---|
[0e18918] | 59 | { |
---|
| 60 | messageFile.append(QString::number(i)); |
---|
| 61 | |
---|
| 62 | qDebug("generar archivo..."); |
---|
| 63 | // generar registros en archivo de texto |
---|
| 64 | QString fileName = "registro-elgamal-"; |
---|
| 65 | fileName.append(messageFile+"-"); |
---|
| 66 | fileName.append(QDateTime::currentDateTime().toString("dd.MM.yy.hh.mm.ss")); |
---|
| 67 | fileName.append(".txt"); |
---|
[24aa974] | 68 | |
---|
[0e18918] | 69 | messageFile.append(".txt"); |
---|
[24aa974] | 70 | //qDebug("antes de QFile readFile"); |
---|
| 71 | QFile readFile(messageFile); |
---|
| 72 | assert (readFile.open(QFile::ReadOnly | QFile::Text)); |
---|
| 73 | QTextStream in(&readFile); |
---|
| 74 | //qDebug("despues de Qfile readFile"); |
---|
| 75 | std::string text = in.readAll().toStdString(); |
---|
[0e18918] | 76 | |
---|
| 77 | // archivo de salida de resultados |
---|
| 78 | QFile registerFile(fileName); |
---|
| 79 | if (registerFile.open(QFile::WriteOnly)) |
---|
| 80 | { |
---|
| 81 | // flujo para el archivo de salida de resultados |
---|
| 82 | QTextStream outFile(®isterFile); |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | // linea que mantiene los valores de tiempo |
---|
| 86 | QString line; |
---|
| 87 | |
---|
| 88 | // iterar 50 veces el proceso de cifrado y descifrado del texto |
---|
[c48d843] | 89 | for (int j = 0; j < ; j++) |
---|
[0e18918] | 90 | { |
---|
| 91 | |
---|
| 92 | qDebug("*************************************************"); |
---|
| 93 | std::cout << "Generating private key. This may take some time..." << std::endl; |
---|
| 94 | timer.start(); |
---|
[24aa974] | 95 | /////////////////////////////////////// |
---|
| 96 | // Create Keys |
---|
[0e18918] | 97 | ElGamal::Decryptor gamalDecryptor; |
---|
| 98 | gamalDecryptor.AccessKey().GenerateRandomWithKeySize(rng, 2048); |
---|
| 99 | int runtime = timer.elapsed(); //gets the runtime in ms |
---|
[24aa974] | 100 | |
---|
| 101 | //qDebug("gamalDecryptor.AccessKey().GenerateRandomWithKeySize(rng, 2048);"); |
---|
| 102 | //const ElGamalKeys::PrivateKey& gamalPrivateKey = gamalDecryptor.AccessKey(); |
---|
| 103 | |
---|
| 104 | ElGamal::Encryptor gamalEncryptor(gamalDecryptor); |
---|
| 105 | //const PublicKey& gamalPublicKey = gamalEncryptor.AccessKey(); |
---|
| 106 | |
---|
[0e18918] | 107 | qDebug(qPrintable("key generation: "+QString::number(runtime)+" ms")); |
---|
| 108 | |
---|
| 109 | line.append(QString::number(runtime)); |
---|
| 110 | line.append(" "); |
---|
| 111 | |
---|
[24aa974] | 112 | SecByteBlock plaintext( (const unsigned char *) text.data(), text.size() ); |
---|
| 113 | |
---|
[0e18918] | 114 | //////////////////////////////////////////////// |
---|
| 115 | // Secret to protect |
---|
| 116 | //static const int SECRET_SIZE = 16; |
---|
| 117 | //SecByteBlock plaintext( SECRET_SIZE ); |
---|
| 118 | //memset( plaintext, 'A', SECRET_SIZE ); |
---|
| 119 | |
---|
| 120 | //////////////////////////////////////////////// |
---|
| 121 | // Encrypt |
---|
| 122 | |
---|
| 123 | // Now that there is a concrete object, we can validate |
---|
| 124 | assert( 0 != gamalEncryptor.FixedMaxPlaintextLength() ); |
---|
| 125 | qDebug("gamalEncryptor.FixedMaxPlaintextLength(): %zu", gamalEncryptor.FixedMaxPlaintextLength()); |
---|
| 126 | // para ElGamal mas de 253 bytes hace que el asser que sigue falle |
---|
| 127 | |
---|
| 128 | assert( plaintext.size() <= gamalEncryptor.FixedMaxPlaintextLength() ); |
---|
| 129 | |
---|
| 130 | // Create cipher text space |
---|
| 131 | //size_t ecl = gamalEncryptor.CiphertextLength( plaintext.size() ); |
---|
| 132 | size_t ecl2 = gamalEncryptor.CiphertextLength( plaintext.size() ); |
---|
| 133 | assert( 0 != ecl2 ); |
---|
| 134 | SecByteBlock ciphertext2( ecl2 ); |
---|
| 135 | |
---|
| 136 | timer.start(); |
---|
| 137 | gamalEncryptor.Encrypt( rng, plaintext, plaintext.size(), ciphertext2 ); |
---|
| 138 | runtime = timer.elapsed(); //gets the runtime in ms |
---|
| 139 | qDebug(qPrintable("ElGamal encryption time: "+QString::number(runtime)+" ms")); |
---|
| 140 | line.append(QString::number(runtime)); |
---|
| 141 | line.append(" "); |
---|
| 142 | |
---|
| 143 | //////////////////////////////////////////////// |
---|
| 144 | // Decrypt |
---|
| 145 | |
---|
| 146 | // Now that there is a concrete object, we can check sizes |
---|
| 147 | assert( 0 != gamalDecryptor.FixedCiphertextLength() ); |
---|
| 148 | assert( ciphertext2.size() <= gamalDecryptor.FixedCiphertextLength() ); |
---|
| 149 | |
---|
| 150 | // Create recovered text space |
---|
| 151 | //size_t dpl = gamalDecryptor.MaxPlaintextLength( ciphertext2.size() ); |
---|
| 152 | size_t dpl2 = gamalDecryptor.MaxPlaintextLength( ciphertext2.size() ); |
---|
| 153 | assert( 0 != dpl2 ); |
---|
| 154 | SecByteBlock recovered2( dpl2 ); |
---|
| 155 | |
---|
| 156 | timer.start(); |
---|
| 157 | //DecodingResult result = gamalDecryptor.Decrypt( rng, ciphertext, ciphertext.size(), recovered ); |
---|
| 158 | DecodingResult result2 = gamalDecryptor.Decrypt( rng, ciphertext2, ciphertext2.size(), recovered2 ); |
---|
| 159 | runtime = timer.elapsed(); //gets the runtime in ms |
---|
| 160 | qDebug(qPrintable("ElGamal decryption time: "+QString::number(runtime)+" ms")); |
---|
| 161 | line.append(QString::number(runtime)); |
---|
| 162 | line.append(" "); |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | // More sanity checks |
---|
| 166 | assert( result2.isValidCoding ); |
---|
| 167 | assert( result2.messageLength <= gamalDecryptor.MaxPlaintextLength( ciphertext2.size() ) ); |
---|
| 168 | |
---|
| 169 | // At this point, we can set the size of the recovered |
---|
| 170 | // data. Until decryption occurs (successfully), we |
---|
| 171 | // only know its maximum size |
---|
| 172 | recovered2.resize( result2.messageLength ); |
---|
| 173 | |
---|
| 174 | // SecByteBlock is overloaded for proper results below |
---|
| 175 | assert( plaintext == recovered2 ); |
---|
| 176 | |
---|
| 177 | // If the assert fires, we won't get this far. |
---|
| 178 | if(plaintext == recovered2) |
---|
[24aa974] | 179 | std::cout << "Recovered plain text" << std::endl; |
---|
[0e18918] | 180 | else |
---|
[24aa974] | 181 | std::cout << "Failed to recover plain text" << std::endl; |
---|
| 182 | |
---|
| 183 | outFile << line << endl; |
---|
| 184 | line = ""; |
---|
[0e18918] | 185 | |
---|
| 186 | } |
---|
[24aa974] | 187 | messageFile = "message"; |
---|
[0e18918] | 188 | } |
---|
| 189 | } |
---|
| 190 | qDebug("exit"); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|