'How to modify this sample code from Crypto++ for RSA to work as it should?
I'm a noob with Crypto++, still trying to understand all the pipeline stuff etc... The first trivial problem I have is that SavePrivateKey isn't found in the library by the compiler but when I look it up on Wiki Crypto++ the link is broken and I can't find what to #include... The second problem is that I'd like to access the privkey and pubkey generate by the first snippet as strings in X.509/PKCS#8 format in order to manipulate them but it's not clear to me how to do that. The last problem is that I haven't understood how to input the keys to the decrypt function as a string. Thank you in advance if you can help me
#include <cryptopp/files.h>
#include <cryptopp/modes.h>
#include <cryptopp/osrng.h>
#include <cryptopp/rsa.h>
#include <cryptopp/sha.h>
#include "cryptopp/filters.h"
#include "cryptopp/hex.h"
#include "cryptopp/cryptlib.h"
#include <typeinfo>
int main() {
// Generate keys
AutoSeededRandomPool rng;
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rng, 3072);
RSA::PrivateKey privateKey(params);
RSA::PublicKey publicKey(params);
std::string plain="RSA Encryption", cipher, recovered;
// Encryption
RSAES_OAEP_SHA_Encryptor e(publicKey);
StringSource ss1(plain, true,new PK_EncryptorFilter(rng, e, new StringSink(cipher))); // StringSource
// Decryption
RSAES_OAEP_SHA_Decryptor d(privateKey);
StringSource ss2(cipher, true, new PK_DecryptorFilter(rng, d, new StringSink(recovered))); // StringSource
SavePrivateKey("rsa-private.key", privateKey);
SavePublicKey("rsa-public.key", publicKey);
std::cout << "Recovered plain text" << std::endl;
std::cout<<rsaPrivate<<std::endl<<rsaPublic<<std::endl;
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
