#include #include #include #include #include #include #include "crypto++/rsa.h" #include "crypto++/elgamal.h" #include "crypto++/osrng.h" #include /** * Programa de Cryptopp para realizar estimaciones de ejecuciones del * algoritmo EL GAMAL * * https://www.cryptopp.com/wiki/ElGamal * * * Es necesario instalar los siguientes paquetes: * libcrypto++9 y libcrypto++-dev * */ using namespace CryptoPP; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //return a.exec(); /////////////////////////////////////// // Pseudo Random Number Generator el gamal AutoSeededRandomPool rng; //QTime timer; QElapsedTimer timer; //int runtime = timer.elapsed(); //gets the runtime in ms // prefijo del nombre del archivo con el texto a cifrar QString messageFile = "message"; //////////////////////////////////////////////// // Secret to protect //static const int SECRET_SIZE = 16; //SecByteBlock plaintext( SECRET_SIZE ); //memset( plaintext, 'A', SECRET_SIZE ); // iterar por el nĂºmero de archivos que se van a cifrar (10) for (int i = 1; i <= 8; i++) { messageFile.append(QString::number(i)); qDebug("generar archivo..."); // generar registros en archivo de texto QString fileName = "registro-elgamal-"; fileName.append(messageFile+"-"); fileName.append(QDateTime::currentDateTime().toString("dd.MM.yy.hh.mm.ss")); fileName.append(".txt"); messageFile.append(".txt"); //qDebug("antes de QFile readFile"); QFile readFile(messageFile); assert (readFile.open(QFile::ReadOnly | QFile::Text)); QTextStream in(&readFile); //qDebug("despues de Qfile readFile"); std::string text = in.readAll().toStdString(); // archivo de salida de resultados QFile registerFile(fileName); if (registerFile.open(QFile::WriteOnly)) { // flujo para el archivo de salida de resultados QTextStream outFile(®isterFile); // linea que mantiene los valores de tiempo QString line; // iterar 50 veces el proceso de cifrado y descifrado del texto for (int j = 0; j < ; j++) { qDebug("*************************************************"); std::cout << "Generating private key. This may take some time..." << std::endl; timer.start(); /////////////////////////////////////// // Create Keys ElGamal::Decryptor gamalDecryptor; gamalDecryptor.AccessKey().GenerateRandomWithKeySize(rng, 2048); int runtime = timer.elapsed(); //gets the runtime in ms //qDebug("gamalDecryptor.AccessKey().GenerateRandomWithKeySize(rng, 2048);"); //const ElGamalKeys::PrivateKey& gamalPrivateKey = gamalDecryptor.AccessKey(); ElGamal::Encryptor gamalEncryptor(gamalDecryptor); //const PublicKey& gamalPublicKey = gamalEncryptor.AccessKey(); qDebug(qPrintable("key generation: "+QString::number(runtime)+" ms")); line.append(QString::number(runtime)); line.append(" "); SecByteBlock plaintext( (const unsigned char *) text.data(), text.size() ); //////////////////////////////////////////////// // Secret to protect //static const int SECRET_SIZE = 16; //SecByteBlock plaintext( SECRET_SIZE ); //memset( plaintext, 'A', SECRET_SIZE ); //////////////////////////////////////////////// // Encrypt // Now that there is a concrete object, we can validate assert( 0 != gamalEncryptor.FixedMaxPlaintextLength() ); qDebug("gamalEncryptor.FixedMaxPlaintextLength(): %zu", gamalEncryptor.FixedMaxPlaintextLength()); // para ElGamal mas de 253 bytes hace que el asser que sigue falle assert( plaintext.size() <= gamalEncryptor.FixedMaxPlaintextLength() ); // Create cipher text space //size_t ecl = gamalEncryptor.CiphertextLength( plaintext.size() ); size_t ecl2 = gamalEncryptor.CiphertextLength( plaintext.size() ); assert( 0 != ecl2 ); SecByteBlock ciphertext2( ecl2 ); timer.start(); gamalEncryptor.Encrypt( rng, plaintext, plaintext.size(), ciphertext2 ); runtime = timer.elapsed(); //gets the runtime in ms qDebug(qPrintable("ElGamal encryption time: "+QString::number(runtime)+" ms")); line.append(QString::number(runtime)); line.append(" "); //////////////////////////////////////////////// // Decrypt // Now that there is a concrete object, we can check sizes assert( 0 != gamalDecryptor.FixedCiphertextLength() ); assert( ciphertext2.size() <= gamalDecryptor.FixedCiphertextLength() ); // Create recovered text space //size_t dpl = gamalDecryptor.MaxPlaintextLength( ciphertext2.size() ); size_t dpl2 = gamalDecryptor.MaxPlaintextLength( ciphertext2.size() ); assert( 0 != dpl2 ); SecByteBlock recovered2( dpl2 ); timer.start(); //DecodingResult result = gamalDecryptor.Decrypt( rng, ciphertext, ciphertext.size(), recovered ); DecodingResult result2 = gamalDecryptor.Decrypt( rng, ciphertext2, ciphertext2.size(), recovered2 ); runtime = timer.elapsed(); //gets the runtime in ms qDebug(qPrintable("ElGamal decryption time: "+QString::number(runtime)+" ms")); line.append(QString::number(runtime)); line.append(" "); // More sanity checks assert( result2.isValidCoding ); assert( result2.messageLength <= gamalDecryptor.MaxPlaintextLength( ciphertext2.size() ) ); // At this point, we can set the size of the recovered // data. Until decryption occurs (successfully), we // only know its maximum size recovered2.resize( result2.messageLength ); // SecByteBlock is overloaded for proper results below assert( plaintext == recovered2 ); // If the assert fires, we won't get this far. if(plaintext == recovered2) std::cout << "Recovered plain text" << std::endl; else std::cout << "Failed to recover plain text" << std::endl; outFile << line << endl; line = ""; } messageFile = "message"; } } qDebug("exit"); }