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