#include <cryptopp/base64.h>#include <cryptopp/filters.h>#include <cryptopp/osrng.h>#include <cryptopp/rsa.h>#include <string>
CryptoPP::AutoSeededRandomPool rng;
int main() { CryptoPP::RSA::PrivateKey private_key; private_key.GenerateRandomWithKeySize(rng, 2048);
std::string base64_private_key; CryptoPP::Base64Encoder private_key_sink(new CryptoPP::StringSink(base64_private_key)); private_key.Save(private_key_sink);
CryptoPP::ArraySource as((const byte *)base64_private_key.data(), base64_private_key.size(), true, new CryptoPP::Base64Decoder()); private_key.Load(as);
return 0;}g++ save_then_load.cc -lcrypto++ && ./a.outterminate called after throwing an instance of 'CryptoPP::BERDecodeErr' what(): BER decode errorAborted (core dumped)ii libcrypto++-dev 5.6.4-9build1 amd64 General purpose cryptographic library - C++ developmentii libcrypto++6 5.6.4-9build1 amd64 General purpose cryptographic library - shared libraryDistributor ID: UbuntuDescription: Ubuntu 20.04 LTSRelease: 20.04Codename: focal#include <cryptopp/osrng.h>#include <cryptopp/rsa.h>#include <cstdint>
CryptoPP::AutoSeededRandomPool rng;
int main() { CryptoPP::RSA::PrivateKey private_key; private_key.GenerateRandomWithKeySize(rng, 2048);
std::vector<uint8_t> signed_bytes(2048); CryptoPP::ArraySink sink(signed_bytes.data(), 2048); private_key.Save(sink); signed_bytes.resize(sink.TotalPutLength());
CryptoPP::ArraySource source(signed_bytes.data(), signed_bytes.size(), /*pumpAll=*/true); private_key.Load(source);
return 0;}This code works:#include <cryptopp/osrng.h>#include <cryptopp/rsa.h>#include <cstdint>CryptoPP::AutoSeededRandomPool rng;int main() {CryptoPP::RSA::PrivateKey private_key;private_key.GenerateRandomWithKeySize(rng, 2048);std::vector<uint8_t> signed_bytes(2048);CryptoPP::ArraySink sink(signed_bytes.data(), 2048);private_key.Save(sink);signed_bytes.resize(sink.TotalPutLength());CryptoPP::ArraySource source(signed_bytes.data(), signed_bytes.size(), /*pumpAll=*/true);private_key.Load(source);return 0;}Apparently there is something wrong with how I used the base64 encoder/decoder. But I don't want to spend anymore time on this issue. I'll go ahead and store raw bytes intead.