source: comparacioncriptosistemas/elgamal/main-original.cpp

interfaz
Last change on this file was 24aa974, checked in by usuario <usuario@…>, 8 years ago

se modifico el archivo main y se agrego nuevos mensajes

  • Property mode set to 100644
File size: 6.9 KB
Line 
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
29using namespace CryptoPP;
30
31int main(int argc, char *argv[])
32{
33    QCoreApplication a(argc, argv);
34    //return a.exec();
35
36
37    ///////////////////////////////////////
38    // Pseudo Random Number Generator
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
57    std::string s;
58    //CryptoPP::FileSource file( "message.txt", true, new StringSink( s ) );
59
60    std::cout << s << std::endl;
61    std::cout << s.size() << std::endl;
62
63    SecByteBlock plaintext( (const unsigned char *) s.data(), s.size() );
64
65
66    // iterar por el número de archivos (10)
67    for (int i = 1; i <= 2; i++)
68    {
69        messageFile.append(QString::number(i));
70
71        qDebug("generar archivo...");
72        // generar registros en archivo de texto
73        QString fileName = "registro-elgamal-";
74        fileName.append(messageFile+"-");
75        fileName.append(QDateTime::currentDateTime().toString("dd.MM.yy.hh.mm.ss"));
76        fileName.append(".txt");
77
78        messageFile.append(".txt");
79
80        std::string s;
81        //CryptoPP::FileSource file( qPrintable(messageFile), true, new StringSink( s ) );
82
83        //std::cout << s ;//<< std::endl;
84        //std::cout << s.size() << std::endl;
85
86        // archivo de salida de resultados
87        QFile registerFile(fileName);
88        if (registerFile.open(QFile::WriteOnly))
89        {
90            // flujo para el archivo de salida de resultados
91            QTextStream outFile(&registerFile);
92
93
94            // linea que mantiene los valores de tiempo
95            QString line;
96
97            // iterar 50 veces el proceso de cifrado y descifrado del texto
98            for (int j = 0; j < 1; j++)
99            {
100
101                qDebug("*************************************************");
102                std::cout << "Generating private key. This may take some time..." << std::endl;
103                timer.start();
104                ElGamal::Decryptor gamalDecryptor;
105                gamalDecryptor.AccessKey().GenerateRandomWithKeySize(rng, 2048);
106                qDebug("gamalDecryptor.AccessKey().GenerateRandomWithKeySize(rng, 2048);");
107
108                int runtime = timer.elapsed(); //gets the runtime in ms
109                qDebug(qPrintable("key generation: "+QString::number(runtime)+" ms"));
110
111                line.append(QString::number(runtime));
112                line.append(" ");
113
114                const ElGamalKeys::PrivateKey& gamalPrivateKey = gamalDecryptor.AccessKey();
115
116                ElGamal::Encryptor gamalEncryptor(gamalDecryptor);
117                const PublicKey& gamalPublicKey = gamalEncryptor.AccessKey();
118                qDebug("const PublicKey& gamalPublicKey = gamalEncryptor.AccessKey();");
119                ////////////////////////////////////////////////
120                // Secret to protect
121                //static const int SECRET_SIZE = 16;
122                //SecByteBlock plaintext( SECRET_SIZE );
123                //memset( plaintext, 'A', SECRET_SIZE );
124
125                ////////////////////////////////////////////////
126                // Encrypt
127
128                // Now that there is a concrete object, we can validate
129                assert( 0 != gamalEncryptor.FixedMaxPlaintextLength() );
130                qDebug("gamalEncryptor.FixedMaxPlaintextLength(): %zu", gamalEncryptor.FixedMaxPlaintextLength());
131                // para ElGamal mas de 253 bytes hace que el asser que sigue falle
132
133                assert( plaintext.size() <= gamalEncryptor.FixedMaxPlaintextLength() );
134
135                // Create cipher text space
136                //size_t ecl = gamalEncryptor.CiphertextLength( plaintext.size() );
137                size_t ecl2 = gamalEncryptor.CiphertextLength( plaintext.size() );
138                assert( 0 != ecl2 );
139                SecByteBlock ciphertext2( ecl2 );
140
141                timer.start();
142                gamalEncryptor.Encrypt( rng, plaintext, plaintext.size(), ciphertext2 );
143                runtime = timer.elapsed(); //gets the runtime in ms
144                qDebug(qPrintable("ElGamal encryption time: "+QString::number(runtime)+" ms"));
145                line.append(QString::number(runtime));
146                line.append(" ");
147
148                ////////////////////////////////////////////////
149                // Decrypt
150
151                // Now that there is a concrete object, we can check sizes
152                assert( 0 != gamalDecryptor.FixedCiphertextLength() );
153                assert( ciphertext2.size() <= gamalDecryptor.FixedCiphertextLength() );
154
155                // Create recovered text space
156                //size_t dpl = gamalDecryptor.MaxPlaintextLength( ciphertext2.size() );
157                size_t dpl2 = gamalDecryptor.MaxPlaintextLength( ciphertext2.size() );
158                assert( 0 != dpl2 );
159                SecByteBlock recovered2( dpl2 );
160
161                timer.start();
162                //DecodingResult result = gamalDecryptor.Decrypt( rng, ciphertext, ciphertext.size(), recovered );
163                DecodingResult result2 = gamalDecryptor.Decrypt( rng, ciphertext2, ciphertext2.size(), recovered2 );
164                runtime = timer.elapsed(); //gets the runtime in ms
165                qDebug(qPrintable("ElGamal decryption time: "+QString::number(runtime)+" ms"));
166                line.append(QString::number(runtime));
167                line.append(" ");
168
169
170                // More sanity checks
171                assert( result2.isValidCoding );
172                assert( result2.messageLength <= gamalDecryptor.MaxPlaintextLength( ciphertext2.size() ) );
173
174                // At this point, we can set the size of the recovered
175                //  data. Until decryption occurs (successfully), we
176                //  only know its maximum size
177                recovered2.resize( result2.messageLength );
178
179                // SecByteBlock is overloaded for proper results below
180                assert( plaintext == recovered2 );
181
182                // If the assert fires, we won't get this far.
183                if(plaintext == recovered2)
184                std::cout << "Recovered plain text" << std::endl;
185                else
186               std::cout << "Failed to recover plain text" << std::endl;
187
188           }
189        }
190    }
191                qDebug("exit");
192}       
193
194
Note: See TracBrowser for help on using the repository browser.