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 RSA |
---|
20 | * |
---|
21 | * https://www.cryptopp.com/wiki/RSA_Cryptography |
---|
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 | /////////////////////////////////////// |
---|
38 | // Pseudo Random Number Generator |
---|
39 | AutoSeededRandomPool rng; |
---|
40 | |
---|
41 | /////////////////////////////////////// |
---|
42 | // Generate Parameters |
---|
43 | //InvertibleRSAFunction params; |
---|
44 | |
---|
45 | //QTime timer; |
---|
46 | QElapsedTimer timer; |
---|
47 | |
---|
48 | // prefijo del nombre del archivo con el texto a cifrar |
---|
49 | QString messageFile = "message"; |
---|
50 | |
---|
51 | // iterar por el número de archivos (10) |
---|
52 | for (int i = 1; i <= 2; i++) |
---|
53 | { |
---|
54 | messageFile.append(QString::number(i)); |
---|
55 | |
---|
56 | qDebug("generar archivo..."); |
---|
57 | // generar registros en archivo de texto |
---|
58 | QString fileName = "registro-rsa-"; |
---|
59 | fileName.append(messageFile+"-"); |
---|
60 | fileName.append(QDateTime::currentDateTime().toString("dd.MM.yy.hh.mm.ss")); |
---|
61 | fileName.append(".txt"); |
---|
62 | |
---|
63 | messageFile.append(".txt"); |
---|
64 | |
---|
65 | |
---|
66 | //CryptoPP::FileSource file( qPrintable(messageFile), true, new StringSink( s ) ); |
---|
67 | QFile readFile(messageFile); |
---|
68 | assert (readFile.open(QFile::ReadOnly | QFile::Text)); |
---|
69 | QTextStream in(&readFile); |
---|
70 | |
---|
71 | std::string text = in.readAll().toStdString(); |
---|
72 | |
---|
73 | //std::cout << s ;//<< std::endl; |
---|
74 | //std::cout << s.size() << std::endl; |
---|
75 | |
---|
76 | // archivo de salida de resultados |
---|
77 | QFile registerFile(fileName); |
---|
78 | if (registerFile.open(QFile::WriteOnly)) |
---|
79 | { |
---|
80 | // flujo para el archivo de salida de resultados |
---|
81 | QTextStream outFile(®isterFile); |
---|
82 | |
---|
83 | |
---|
84 | // linea que mantiene los valores de tiempo |
---|
85 | QString line; |
---|
86 | |
---|
87 | // iterar 50 veces el proceso de cifrado y descifrado del texto |
---|
88 | for (int j = 0; j < 50; j++) |
---|
89 | { |
---|
90 | |
---|
91 | qDebug("*************************************************"); |
---|
92 | InvertibleRSAFunction params; |
---|
93 | |
---|
94 | timer.start(); |
---|
95 | params.GenerateRandomWithKeySize(rng, 4096); |
---|
96 | |
---|
97 | int runtime = timer.elapsed(); //gets the runtime in ms |
---|
98 | qDebug(qPrintable("key generation: "+QString::number(runtime)+" ms")); |
---|
99 | |
---|
100 | line.append(QString::number(runtime)); |
---|
101 | line.append(" "); |
---|
102 | |
---|
103 | /////////////////////////////////////// |
---|
104 | // Create Keys |
---|
105 | RSA::PrivateKey privateKey(params); |
---|
106 | RSA::PublicKey publicKey(params); |
---|
107 | |
---|
108 | SecByteBlock plaintext( (const unsigned char *) text.data(), text.size() ); |
---|
109 | |
---|
110 | |
---|
111 | //////////////////////////////////////////////// |
---|
112 | // Encrypt |
---|
113 | RSAES_OAEP_SHA_Encryptor encryptor( publicKey ); |
---|
114 | |
---|
115 | // Now that there is a concrete object, we can validate |
---|
116 | assert( 0 != encryptor.FixedMaxPlaintextLength() ); |
---|
117 | |
---|
118 | //qDebug("encryptor.FixedMaxPlaintextLength(): %zu", encryptor.FixedMaxPlaintextLength()); |
---|
119 | |
---|
120 | // vale la pena leer la siguiente pregunta: |
---|
121 | // https://stackoverflow.com/questions/2033809/im-using-crypto-for-rsa-encryption-my-plain-text-exceeds-fixedmaxplaintextle |
---|
122 | // |
---|
123 | // para RSA mas de 470 bytes hace que el asser que sigue falle |
---|
124 | assert( plaintext.size() <= encryptor.FixedMaxPlaintextLength() ); |
---|
125 | |
---|
126 | // Create cipher text space |
---|
127 | size_t ecl = encryptor.CiphertextLength( plaintext.size() ); |
---|
128 | assert( 0 != ecl ); |
---|
129 | SecByteBlock ciphertext( ecl ); |
---|
130 | |
---|
131 | timer.start(); |
---|
132 | encryptor.Encrypt( rng, plaintext, plaintext.size(), ciphertext ); |
---|
133 | runtime = timer.elapsed(); //gets the runtime in ms |
---|
134 | qDebug(qPrintable("RSA encryption time: "+QString::number(runtime)+" ms")); |
---|
135 | |
---|
136 | line.append(QString::number(runtime)); |
---|
137 | line.append(" "); |
---|
138 | |
---|
139 | |
---|
140 | //////////////////////////////////////////////// |
---|
141 | // Decrypt |
---|
142 | RSAES_OAEP_SHA_Decryptor decryptor( privateKey ); |
---|
143 | |
---|
144 | // Now that there is a concrete object, we can check sizes |
---|
145 | assert( 0 != decryptor.FixedCiphertextLength() ); |
---|
146 | assert( ciphertext.size() <= decryptor.FixedCiphertextLength() ); |
---|
147 | |
---|
148 | // Create recovered text space |
---|
149 | size_t dpl = decryptor.MaxPlaintextLength( ciphertext.size() ); |
---|
150 | assert( 0 != dpl ); |
---|
151 | SecByteBlock recovered( dpl ); |
---|
152 | |
---|
153 | timer.start(); |
---|
154 | DecodingResult result = decryptor.Decrypt( rng, ciphertext, ciphertext.size(), recovered ); |
---|
155 | runtime = timer.elapsed(); //gets the runtime in ms |
---|
156 | qDebug(qPrintable("RSA decryption time: "+QString::number(runtime)+" ms")); |
---|
157 | |
---|
158 | line.append(QString::number(runtime)); |
---|
159 | line.append(" "); |
---|
160 | |
---|
161 | // More sanity checks |
---|
162 | assert( result.isValidCoding ); |
---|
163 | assert( result.messageLength <= decryptor.MaxPlaintextLength( ciphertext.size() ) ); |
---|
164 | |
---|
165 | // At this point, we can set the size of the recovered |
---|
166 | // data. Until decryption occurs (successfully), we |
---|
167 | // only know its maximum size |
---|
168 | recovered.resize( result.messageLength ); |
---|
169 | |
---|
170 | // SecByteBlock is overloaded for proper results below |
---|
171 | assert( plaintext == recovered ); |
---|
172 | |
---|
173 | std::cout << "Recovered plain text" << std::endl; |
---|
174 | |
---|
175 | |
---|
176 | outFile << line << endl; |
---|
177 | line = ""; |
---|
178 | |
---|
179 | |
---|
180 | } |
---|
181 | messageFile = "message"; |
---|
182 | } |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | qDebug("salida"); |
---|
187 | } |
---|