int main(int argc, char** argv) { std::ios_base::sync_with_stdio(false); // http://www.cryptopp.com/docs/ref/class_auto_seeded_random_pool.html AutoSeededRandomPool rnd; try { // http://www.cryptopp.com/docs/ref/rsa_8h.html RSA::PrivateKey rsaPrivate; rsaPrivate.GenerateRandomWithKeySize(rnd, 1024); RSA::PublicKey rsaPublic(rsaPrivate); if(!rsaPrivate.Validate(rnd, 3)) { throw runtime_error("Validation failed"); } SavePrivateKey("rsa-private.key", rsaPrivate); SavePublicKey("rsa-public.key", rsaPublic); cout << "Successfully generated and saved RSA keys:" << endl; cout << "Values:" << endl; cout << "N: " << rsaPrivate.GetModulus() << endl; cout << "E: " << rsaPrivate.GetPublicExponent() << endl; cout << "D: " << rsaPrivate.GetPrivateExponent() << endl; } catch(CryptoPP::Exception& e) { cerr << e.what() << endl; return -2; } catch(std::exception& e) { cerr << e.what() << endl; return -1; } return 0; }
void BackupSystem::generate_keypair(const std::wstring &recipient, const std::wstring &filename, const std::string &symmetric_key){ while (1){ CryptoPP::RSA::PrivateKey rsaPrivate; rsaPrivate.GenerateRandomWithKeySize(*random_number_generator, 4 << 10); if (!rsaPrivate.Validate(*random_number_generator, 3)) continue; CryptoPP::RSA::PublicKey rsaPublic(rsaPrivate); if (!rsaPublic.Validate(*random_number_generator, 3)) continue; auto pri = to_vector(rsaPrivate); auto pub = to_vector(rsaPublic); RsaKeyPair pair(pri, pub, symmetric_key); boost::filesystem::ofstream file(filename, std::ios::binary); SerializerStream ss(file); ss.begin_serialization(pair, false); break; } }