Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "cryptopp/rsa.h"
- using CryptoPP::RSA;
- using CryptoPP::RSASS;
- using CryptoPP::InvertibleRSAFunction;
- #include "cryptopp/pssr.h"
- using CryptoPP::PSS;
- #include "cryptopp/hrtimer.h"
- #include "cryptopp/sha.h"
- using CryptoPP::SHA1;
- #include "cryptopp/files.h"
- using CryptoPP::FileSink;
- using CryptoPP::FileSource;
- #include "cryptopp/osrng.h"
- using CryptoPP::AutoSeededRandomPool;
- #include "cryptopp/SecBlock.h"
- using CryptoPP::SecByteBlock;
- using CryptoPP::TimerBase;
- using CryptoPP::ThreadUserTimer;
- #include <string>
- #include <iostream>
- #include <stdlib.h>
- #include <iomanip>
- #include <sstream>
- using namespace std;
- int main(int argc, char* argv[])
- {
- try
- {
- ////////////////////////////////////////////////
- // Generate keys
- cout << "Generating key....." << endl;
- AutoSeededRandomPool rng;
- InvertibleRSAFunction parameters;
- parameters.GenerateRandomWithKeySize( rng, 2048 );
- RSA::PrivateKey privateKey( parameters );
- RSA::PublicKey publicKey( parameters );
- // Message
- ifstream in("100MB.ZIP");
- string message((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
- cout << "Sign Message..... start timer....." << endl;
- //Start timer
- ThreadUserTimer timer(TimerBase::MILLISECONDS);
- timer.StartTimer();
- // Signer object
- RSASS<PSS, SHA1>::Signer signer( privateKey );
- // Create signature space
- size_t length = signer.MaxSignatureLength();
- SecByteBlock signature( length );
- // Sign message
- signer.SignMessage( rng, (const byte*) message.c_str(),
- message.length(), signature );
- cout << "Verifying Message....." << endl;
- // Verifier object
- RSASS<PSS, SHA1>::Verifier verifier( publicKey );
- // Verify
- bool result = verifier.VerifyMessage( (const byte*)message.c_str(),
- message.length(), signature, signature.size() );
- // Result
- if( true == result ) {
- cout << "Signature on message verified" << endl;
- } else {
- cout << "Message verification failed" << endl;
- }
- //Stop timer
- unsigned long elapsed = timer.GetCurrentTimerValue();
- unsigned long ticks = timer.TicksPerSecond();
- unsigned long seconds = elapsed / ticks;
- // days, hours, minutes, seconds, 100th seconds
- unsigned int d=0, h=0, m=0, s=0, p=0;
- p = ((elapsed * 100) / ticks) % 100;
- s = seconds % 60;
- m = (seconds / 60) % 60;
- h = (seconds / 60 / 60) % 60;
- d = (seconds / 60 / 60 / 24) % 24;
- float fs = (seconds + ((float)p/100));
- stringstream ss;
- if(d) {
- ss << d << ((d == 1) ? " day, " : " days, ");
- goto print_hours;
- }
- if(h) {
- print_hours:
- ss << h << ((h == 1) ? " hour, " : " hours, ");
- goto print_minutes;
- }
- if(m) {
- print_minutes:
- ss << m << ((m == 1) ? " minute, " : " minutes, ");
- }
- ss << s << ((s == 1) ? " second" : " seconds");
- //Output
- // system("cls");
- cout << endl << "#####Result#####" << endl;
- cout << "Execution time : ";
- cout << fixed << setprecision(2) << fs << "s";
- if(seconds)
- cout << " (" << ss.str() << ")";
- cout << endl;
- } // try
- catch( CryptoPP::Exception& e ) {
- std::cerr << "Error: " << e.what() << std::endl;
- }
- return 0;
- }
- void SaveKey( const RSA::PublicKey& PublicKey, const string& filename )
- {
- // DER Encode Key - X.509 key format
- PublicKey.Save(
- FileSink( filename.c_str(), true /*binary*/ ).Ref()
- );
- }
- void SaveKey( const RSA::PrivateKey& PrivateKey, const string& filename )
- {
- // DER Encode Key - PKCS #8 key format
- PrivateKey.Save(
- FileSink( filename.c_str(), true /*binary*/ ).Ref()
- );
- }
- void LoadKey( const string& filename, RSA::PublicKey& PublicKey )
- {
- // DER Encode Key - X.509 key format
- PublicKey.Load(
- FileSource( filename.c_str(), true, NULL, true /*binary*/ ).Ref()
- );
- }
- void LoadKey( const string& filename, RSA::PrivateKey& PrivateKey )
- {
- // DER Encode Key - PKCS #8 key format
- PrivateKey.Load(
- FileSource( filename.c_str(), true, NULL, true /*binary*/ ).Ref()
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement