Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1. #include "cryptopp/rsa.h"
  2. using CryptoPP::RSA;
  3. using CryptoPP::RSASS;
  4. using CryptoPP::InvertibleRSAFunction;
  5.  
  6. #include "cryptopp/pssr.h"
  7. using CryptoPP::PSS;
  8. #include "cryptopp/hrtimer.h"
  9. #include "cryptopp/sha.h"
  10. using CryptoPP::SHA1;
  11.  
  12. #include "cryptopp/files.h"
  13. using CryptoPP::FileSink;
  14. using CryptoPP::FileSource;
  15.  
  16. #include "cryptopp/osrng.h"
  17. using CryptoPP::AutoSeededRandomPool;
  18.  
  19. #include "cryptopp/SecBlock.h"
  20. using CryptoPP::SecByteBlock;
  21.  
  22. using CryptoPP::TimerBase;
  23. using CryptoPP::ThreadUserTimer;
  24.  
  25. #include <string>
  26. #include <iostream>
  27. #include <stdlib.h>
  28. #include <iomanip>
  29. #include <sstream>
  30.  
  31. using namespace std;
  32.  
  33. int main(int argc, char* argv[])
  34. {
  35.  
  36.  
  37.     try
  38.     {
  39.         ////////////////////////////////////////////////
  40.         // Generate keys
  41.         cout << "Generating key....." << endl;
  42.         AutoSeededRandomPool rng;
  43.  
  44.         InvertibleRSAFunction parameters;
  45.         parameters.GenerateRandomWithKeySize( rng, 2048 );
  46.  
  47.         RSA::PrivateKey privateKey( parameters );
  48.         RSA::PublicKey publicKey( parameters );
  49.  
  50.         // Message
  51.         ifstream in("100MB.ZIP");
  52.         string message((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
  53.         cout << "Sign Message..... start timer....." << endl;
  54.  
  55.           //Start timer
  56.         ThreadUserTimer timer(TimerBase::MILLISECONDS);
  57.         timer.StartTimer();
  58.  
  59.         // Signer object
  60.         RSASS<PSS, SHA1>::Signer signer( privateKey );
  61.         // Create signature space
  62.         size_t length = signer.MaxSignatureLength();
  63.         SecByteBlock signature( length );
  64.  
  65.         // Sign message
  66.         signer.SignMessage( rng, (const byte*) message.c_str(),
  67.             message.length(), signature );
  68.         cout << "Verifying Message....." << endl;
  69.         // Verifier object
  70.         RSASS<PSS, SHA1>::Verifier verifier( publicKey );
  71.  
  72.         // Verify
  73.         bool result = verifier.VerifyMessage( (const byte*)message.c_str(),
  74.             message.length(), signature, signature.size() );
  75.  
  76.         // Result
  77.         if( true == result ) {
  78.             cout << "Signature on message verified" << endl;
  79.         } else {
  80.             cout << "Message verification failed" << endl;
  81.         }
  82.  
  83.  
  84.          //Stop timer
  85.         unsigned long elapsed = timer.GetCurrentTimerValue();
  86.         unsigned long ticks = timer.TicksPerSecond();
  87.         unsigned long seconds = elapsed / ticks;
  88.  
  89.         // days, hours, minutes, seconds, 100th seconds
  90.         unsigned int d=0, h=0, m=0, s=0, p=0;
  91.  
  92.         p = ((elapsed * 100) / ticks) % 100;
  93.         s = seconds % 60;
  94.         m = (seconds / 60) % 60;
  95.         h = (seconds / 60 / 60) % 60;
  96.         d = (seconds / 60 / 60 / 24) % 24;
  97.  
  98.         float fs = (seconds + ((float)p/100));
  99.  
  100.         stringstream ss;
  101.  
  102.         if(d) {
  103.                 ss << d << ((d == 1) ? " day, " : " days, ");
  104.                 goto print_hours;
  105.         }
  106.  
  107.         if(h) {
  108.         print_hours:
  109.                 ss << h << ((h == 1) ? " hour, " : " hours, ");
  110.                 goto print_minutes;
  111.         }
  112.  
  113.         if(m) {
  114.         print_minutes:
  115.                 ss << m << ((m == 1) ? " minute, " : " minutes, ");
  116.         }
  117.  
  118.         ss << s << ((s == 1) ? " second" : " seconds");
  119.  
  120.         //Output
  121.        // system("cls");
  122.         cout << endl << "#####Result#####" << endl;
  123.         cout << "Execution time : ";
  124.         cout << fixed << setprecision(2) << fs << "s";
  125.         if(seconds)
  126.                 cout << " (" << ss.str() << ")";
  127.         cout << endl;
  128.  
  129.  
  130.  
  131.  
  132.     } // try
  133.  
  134.     catch( CryptoPP::Exception& e ) {
  135.         std::cerr << "Error: " << e.what() << std::endl;
  136.     }
  137.  
  138.     return 0;
  139. }
  140.  
  141. void SaveKey( const RSA::PublicKey& PublicKey, const string& filename )
  142. {
  143.     // DER Encode Key - X.509 key format
  144.     PublicKey.Save(
  145.         FileSink( filename.c_str(), true /*binary*/ ).Ref()
  146.     );
  147. }
  148.  
  149. void SaveKey( const RSA::PrivateKey& PrivateKey, const string& filename )
  150. {
  151.     // DER Encode Key - PKCS #8 key format
  152.     PrivateKey.Save(
  153.         FileSink( filename.c_str(), true /*binary*/ ).Ref()
  154.     );
  155. }
  156.  
  157. void LoadKey( const string& filename, RSA::PublicKey& PublicKey )
  158. {
  159.     // DER Encode Key - X.509 key format
  160.     PublicKey.Load(
  161.         FileSource( filename.c_str(), true, NULL, true /*binary*/ ).Ref()
  162.     );
  163. }
  164.  
  165. void LoadKey( const string& filename, RSA::PrivateKey& PrivateKey )
  166. {
  167.     // DER Encode Key - PKCS #8 key format
  168.     PrivateKey.Load(
  169.         FileSource( filename.c_str(), true, NULL, true /*binary*/ ).Ref()
  170.     );
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement