#include <botan/auto_rng.h> <!--more--> #include <botan/rsa.h> #include <botan/pkcs8.h> #include <botan/x509_key.h> #include <iostream> #include <fstream>
void save_key_to_file(const std::string& filename, const std::string& key_data) { std::ofstream file(filename); if (file.is_open()) { file << key_data; file.close(); std::cout << "Key saved to file: " << filename << std::endl; } else { throw std::runtime_error("Unable to open file for writing: " + filename); } }
int main() { try { Botan::AutoSeeded_RNG rng;
Botan::RSA_PrivateKey private_key(rng, 2048); Botan::RSA_PublicKey public_key = private_key;
std::string public_key_pem = Botan::X509::PEM_encode(public_key); std::string private_key_pem = Botan::PKCS8::PEM_encode(private_key);
save_key_to_file("public_key.pem", public_key_pem); save_key_to_file("private_key.pem", private_key_pem);
} catch (std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; return 1; }
return 0; }
|