//for generate IV and Key
#include "D:/Crypto++/cryptopp820/osrng.h"
using CryptoPP::AutoSeededRandomPool;
#include <string>
using std::string;
//for exceptions
#include "D:/Crypto++/cryptopp820/cryptlib.h"
using CryptoPP::Exception;
//for hex viewing
#include "D:/Crypto++/cryptopp820/hex.h"
using CryptoPP::HexDecoder;
using CryptoPP::HexEncoder;
//converter input into cipher
#include "D:/Crypto++/cryptopp820/filters.h"
using CryptoPP::StringSink;
using CryptoPP::StreamTransformation;
using CryptoPP::StreamTransformationFilter;
//add AES
#include "D:/Crypto++/cryptopp820/aes.h"
using CryptoPP::AES;
#include "D:/Crypto++/cryptopp820/modes.h"
using CryptoPP::CBC_Mode;
#include "D:/Crypto++/cryptopp820/md5.h"
using CryptoPP::MD5;
#include "D:/Crypto++/cryptopp820/blowfish.h"
#include "D:/Crypto++/cryptopp820/rsa.h"
#include "D:/Crypto++/cryptopp820/eccrypto.h"
#include "D:/Crypto++/cryptopp820/base64.h"
using CryptoPP::Blowfish;
#include <iostream>
using namespace std;
using namespace CryptoPP;
int main()
{
//create public+private keys
//encrypt some text
//pack public key into the string or vector char
//unpack public key and decrypt text
std::string testText = "Text text";
std::string encryptText, decryptText, for_save_key, packedKey, unpackedKey;
AutoSeededRandomPool rng(true);
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rng, 3072);
RSA::PrivateKey privateKey(params);
RSA::PublicKey publicKey(params);
RSAES_OAEP_SHA_Encryptor encryptor(publicKey);
StringSource(testText, true, new PK_EncryptorFilter(rng, encryptor, new StringSink(encryptText)));
//try to pack public key
RSA::PublicKey newPublicKey;
try
{
StringSink tmp_key(for_save_key);
publicKey.Save(tmp_key);
newPublicKey.Load(CryptoPP::StringStore((const byte*)for_save_key.data(), for_save_key.size()).Ref());
}
catch (const BERDecodeErr& ex)
{
cerr << ex.what() << endl;
}
try
{
RSAES_OAEP_SHA_Decryptor decryptor(newPublicKey);
StringSource(encryptText, true, new PK_DecryptorFilter(rng, decryptor, new StringSink(decryptText)));
}
catch (InvalidArgument& ex)
{
cerr << ex.what() << endl;
}
std::cout << "Decrypt text: " << decryptText << endl;
system("pause");
return 0;
}