#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 RSA * * https://www.cryptopp.com/wiki/RSA_Cryptography * * * 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 AutoSeededRandomPool rng; /////////////////////////////////////// // Generate Parameters //InvertibleRSAFunction params; //QTime timer; QElapsedTimer timer; // prefijo del nombre del archivo con el texto a cifrar QString messageFile = "message"; // iterar por el nĂºmero de archivos (10) for (int i = 1; i <= 2; i++) { messageFile.append(QString::number(i)); qDebug("generar archivo..."); // generar registros en archivo de texto QString fileName = "registro-rsa-"; fileName.append(messageFile+"-"); fileName.append(QDateTime::currentDateTime().toString("dd.MM.yy.hh.mm.ss")); fileName.append(".txt"); messageFile.append(".txt"); //CryptoPP::FileSource file( qPrintable(messageFile), true, new StringSink( s ) ); QFile readFile(messageFile); assert (readFile.open(QFile::ReadOnly | QFile::Text)); QTextStream in(&readFile); std::string text = in.readAll().toStdString(); //std::cout << s ;//<< std::endl; //std::cout << s.size() << std::endl; // 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 < 50; j++) { qDebug("*************************************************"); InvertibleRSAFunction params; timer.start(); params.GenerateRandomWithKeySize(rng, 4096); int runtime = timer.elapsed(); //gets the runtime in ms qDebug(qPrintable("key generation: "+QString::number(runtime)+" ms")); line.append(QString::number(runtime)); line.append(" "); /////////////////////////////////////// // Create Keys RSA::PrivateKey privateKey(params); RSA::PublicKey publicKey(params); SecByteBlock plaintext( (const unsigned char *) text.data(), text.size() ); //////////////////////////////////////////////// // Encrypt RSAES_OAEP_SHA_Encryptor encryptor( publicKey ); // Now that there is a concrete object, we can validate assert( 0 != encryptor.FixedMaxPlaintextLength() ); //qDebug("encryptor.FixedMaxPlaintextLength(): %zu", encryptor.FixedMaxPlaintextLength()); // vale la pena leer la siguiente pregunta: // https://stackoverflow.com/questions/2033809/im-using-crypto-for-rsa-encryption-my-plain-text-exceeds-fixedmaxplaintextle // // para RSA mas de 470 bytes hace que el asser que sigue falle assert( plaintext.size() <= encryptor.FixedMaxPlaintextLength() ); // Create cipher text space size_t ecl = encryptor.CiphertextLength( plaintext.size() ); assert( 0 != ecl ); SecByteBlock ciphertext( ecl ); timer.start(); encryptor.Encrypt( rng, plaintext, plaintext.size(), ciphertext ); runtime = timer.elapsed(); //gets the runtime in ms qDebug(qPrintable("RSA encryption time: "+QString::number(runtime)+" ms")); line.append(QString::number(runtime)); line.append(" "); //////////////////////////////////////////////// // Decrypt RSAES_OAEP_SHA_Decryptor decryptor( privateKey ); // Now that there is a concrete object, we can check sizes assert( 0 != decryptor.FixedCiphertextLength() ); assert( ciphertext.size() <= decryptor.FixedCiphertextLength() ); // Create recovered text space size_t dpl = decryptor.MaxPlaintextLength( ciphertext.size() ); assert( 0 != dpl ); SecByteBlock recovered( dpl ); timer.start(); DecodingResult result = decryptor.Decrypt( rng, ciphertext, ciphertext.size(), recovered ); runtime = timer.elapsed(); //gets the runtime in ms qDebug(qPrintable("RSA decryption time: "+QString::number(runtime)+" ms")); line.append(QString::number(runtime)); line.append(" "); // More sanity checks assert( result.isValidCoding ); assert( result.messageLength <= decryptor.MaxPlaintextLength( ciphertext.size() ) ); // At this point, we can set the size of the recovered // data. Until decryption occurs (successfully), we // only know its maximum size recovered.resize( result.messageLength ); // SecByteBlock is overloaded for proper results below assert( plaintext == recovered ); std::cout << "Recovered plain text" << std::endl; outFile << line << endl; line = ""; } messageFile = "message"; } } qDebug("salida"); }