source: comparacioncriptosistemas/cryptopp/main.cpp @ 9d44c3c

interfaz
Last change on this file since 9d44c3c was 9d44c3c, checked in by aaraujo <aaraujo@…>, 8 years ago

Se describen los paquetes necesarios para compilar: libcrypto++-dev libcrypto++9 en Debian GNU/Linux

  • Property mode set to 100644
File size: 7.9 KB
Line 
1#include <QCoreApplication>
2#include <QTime>
3#include <QElapsedTimer>
4
5#include <iostream>
6
7#include "crypto++/rsa.h"
8#include "crypto++/elgamal.h"
9#include "crypto++/osrng.h"
10#include <crypto++/files.h>
11
12/**
13 * Programa de prueba de Cryptopp para RSA y El Gamal
14 *
15 * https://www.cryptopp.com/wiki/RSA_Cryptography
16 *
17 *
18 * Es necesario instalar los siguientes paquetes:
19 * libcrypto++9 y libcrypto++-dev
20 *
21 */
22
23
24using namespace CryptoPP;
25
26
27
28
29int main(int argc, char *argv[])
30{
31    QCoreApplication a(argc, argv);
32    //return a.exec();
33
34    ///////////////////////////////////////
35    // Pseudo Random Number Generator
36    AutoSeededRandomPool rng;
37
38    ///////////////////////////////////////
39    // Generate Parameters
40    InvertibleRSAFunction params;
41
42    //QTime timer;
43    QElapsedTimer timer;
44    timer.start();
45
46    params.GenerateRandomWithKeySize(rng, 4096);
47
48    int runtime = timer.elapsed(); //gets the runtime in ms
49    qDebug(qPrintable("key generation: "+QString::number(runtime)+" ms"));
50
51
52
53    ///////////////////////////////////////
54    // Generated Parameters
55    const Integer& n = params.GetModulus();
56    const Integer& p = params.GetPrime1();
57    const Integer& q = params.GetPrime2();
58    const Integer& d = params.GetPrivateExponent();
59    const Integer& e = params.GetPublicExponent();
60
61    ///////////////////////////////////////
62    // Dump
63    std::cout << "RSA Parameters:" << std::endl;
64    std::cout << " n: " << n << std::endl;
65    std::cout << " p: " << p << std::endl;
66    std::cout << " q: " << q << std::endl;
67    std::cout << " d: " << d << std::endl;
68    std::cout << " e: " << e << std::endl;
69    std::cout << std::endl;
70
71    ///////////////////////////////////////
72    // Create Keys
73    RSA::PrivateKey privateKey(params);
74    RSA::PublicKey publicKey(params);
75
76    // también se puede hacer así:
77    // RSA::PrivateKey privateKey;
78    // privateKey.GenerateRandomWithKeySize(rng, 3072);
79    // RSA::PublicKey publicKey(privateKey);
80
81
82    std::string s;
83    CryptoPP::FileSource file( "message.txt", true, new StringSink( s ) );
84
85    std::cout << s << std::endl;
86    std::cout << s.size() << std::endl;
87
88
89
90
91
92
93    ////////////////////////////////////////////////
94    // Secret to protect
95    //static const int SECRET_SIZE = 16;
96    //SecByteBlock plaintext( SECRET_SIZE );
97    //memset( plaintext, 'A', SECRET_SIZE );
98
99    SecByteBlock plaintext( (const unsigned char *) s.data(), s.size() );
100
101
102    ////////////////////////////////////////////////
103    // Encrypt
104    RSAES_OAEP_SHA_Encryptor encryptor( publicKey );
105
106    // Now that there is a concrete object, we can validate
107    assert( 0 != encryptor.FixedMaxPlaintextLength() );
108
109    qDebug("encryptor.FixedMaxPlaintextLength(): %zu", encryptor.FixedMaxPlaintextLength());
110
111    // vale la pena leer la siguiente pregunta:
112    // https://stackoverflow.com/questions/2033809/im-using-crypto-for-rsa-encryption-my-plain-text-exceeds-fixedmaxplaintextle
113    //
114    // para RSA mas de 470 bytes hace que el asser que sigue falle
115    assert( plaintext.size() <= encryptor.FixedMaxPlaintextLength() );
116
117    // Create cipher text space
118    size_t ecl = encryptor.CiphertextLength( plaintext.size() );
119    assert( 0 != ecl );
120    SecByteBlock ciphertext( ecl );
121
122    timer.start();
123    encryptor.Encrypt( rng, plaintext, plaintext.size(), ciphertext );
124    runtime = timer.elapsed(); //gets the runtime in ms
125    qDebug(qPrintable("RSA encryption time: "+QString::number(runtime)+" ms"));
126
127
128    ////////////////////////////////////////////////
129    // Decrypt
130    RSAES_OAEP_SHA_Decryptor decryptor( privateKey );
131
132    // Now that there is a concrete object, we can check sizes
133    assert( 0 != decryptor.FixedCiphertextLength() );
134    assert( ciphertext.size() <= decryptor.FixedCiphertextLength() );
135
136    // Create recovered text space
137    size_t dpl = decryptor.MaxPlaintextLength( ciphertext.size() );
138    assert( 0 != dpl );
139    SecByteBlock recovered( dpl );
140
141    timer.start();
142    DecodingResult result = decryptor.Decrypt( rng, ciphertext, ciphertext.size(), recovered );
143    runtime = timer.elapsed(); //gets the runtime in ms
144    qDebug(qPrintable("RSA decryption time: "+QString::number(runtime)+" ms"));
145
146    // More sanity checks
147    assert( result.isValidCoding );
148    assert( result.messageLength <= decryptor.MaxPlaintextLength( ciphertext.size() ) );
149
150    // At this point, we can set the size of the recovered
151    //  data. Until decryption occurs (successfully), we
152    //  only know its maximum size
153    recovered.resize( result.messageLength );
154
155    // SecByteBlock is overloaded for proper results below
156    assert( plaintext == recovered );
157
158    std::cout << "Recovered plain text" << std::endl;
159
160
161    ///////////////////////////////////////////////////////////////////////////
162    ///////////////////////////////////////////////////////////////////////////
163    ///////////////////////////////////////////////////////////////////////////
164    // ejemplo de El Gamal
165    // https://www.cryptopp.com/wiki/ElGamal
166
167    std::cout << "Generating private key. This may take some time..." << std::endl;
168
169    ElGamal::Decryptor gamalDecryptor;
170    gamalDecryptor.AccessKey().GenerateRandomWithKeySize(rng, 2048);
171    const ElGamalKeys::PrivateKey& gamalPrivateKey = gamalDecryptor.AccessKey();
172
173    ElGamal::Encryptor gamalEncryptor(gamalDecryptor);
174    const PublicKey& gamalPublicKey = gamalEncryptor.AccessKey();
175
176    ////////////////////////////////////////////////
177    // Secret to protect
178    //static const int SECRET_SIZE = 16;
179    //SecByteBlock plaintext( SECRET_SIZE );
180    //memset( plaintext, 'A', SECRET_SIZE );
181
182    ////////////////////////////////////////////////
183    // Encrypt
184
185    // Now that there is a concrete object, we can validate
186    assert( 0 != gamalEncryptor.FixedMaxPlaintextLength() );
187    qDebug("gamalEncryptor.FixedMaxPlaintextLength(): %zu", gamalEncryptor.FixedMaxPlaintextLength());
188    // para ElGamal mas de 253 bytes hace que el asser que sigue falle
189    assert( plaintext.size() <= gamalEncryptor.FixedMaxPlaintextLength() );
190
191    // Create cipher text space
192    //size_t ecl = gamalEncryptor.CiphertextLength( plaintext.size() );
193    size_t ecl2 = gamalEncryptor.CiphertextLength( plaintext.size() );
194    assert( 0 != ecl2 );
195    SecByteBlock ciphertext2( ecl2 );
196
197    timer.start();
198    gamalEncryptor.Encrypt( rng, plaintext, plaintext.size(), ciphertext2 );
199    runtime = timer.elapsed(); //gets the runtime in ms
200    qDebug(qPrintable("ElGamal encryption time: "+QString::number(runtime)+" ms"));
201
202    ////////////////////////////////////////////////
203    // Decrypt
204
205    // Now that there is a concrete object, we can check sizes
206    assert( 0 != gamalDecryptor.FixedCiphertextLength() );
207    assert( ciphertext2.size() <= gamalDecryptor.FixedCiphertextLength() );
208
209    // Create recovered text space
210    //size_t dpl = gamalDecryptor.MaxPlaintextLength( ciphertext2.size() );
211    size_t dpl2 = gamalDecryptor.MaxPlaintextLength( ciphertext2.size() );
212    assert( 0 != dpl2 );
213    SecByteBlock recovered2( dpl2 );
214
215    timer.start();
216    //DecodingResult result = gamalDecryptor.Decrypt( rng, ciphertext, ciphertext.size(), recovered );
217    DecodingResult result2 = gamalDecryptor.Decrypt( rng, ciphertext2, ciphertext2.size(), recovered2 );
218    runtime = timer.elapsed(); //gets the runtime in ms
219    qDebug(qPrintable("ElGamal decryption time: "+QString::number(runtime)+" ms"));
220
221
222    // More sanity checks
223    assert( result2.isValidCoding );
224    assert( result2.messageLength <= gamalDecryptor.MaxPlaintextLength( ciphertext2.size() ) );
225
226    // At this point, we can set the size of the recovered
227    //  data. Until decryption occurs (successfully), we
228    //  only know its maximum size
229    recovered2.resize( result2.messageLength );
230
231    // SecByteBlock is overloaded for proper results below
232    assert( plaintext == recovered2 );
233
234    // If the assert fires, we won't get this far.
235    if(plaintext == recovered2)
236        std::cout << "Recovered plain text" << std::endl;
237    else
238        std::cout << "Failed to recover plain text" << std::endl;
239
240
241    qDebug("exit");
242
243
244}
245
Note: See TracBrowser for help on using the repository browser.