1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 #include <cstdlib>
5 #include <memory>
6 
7 #include <botan/botan.h>
8 #include <botan/rsa.h>
9 using namespace Botan;
10 
main(int argc,char * argv[])11 int main(int argc, char* argv[])
12    {
13    if(argc != 2 && argc != 3)
14       {
15       std::cout << "Usage: " << argv[0] << " bitsize [passphrase]"
16                 << std::endl;
17       return 1;
18       }
19 
20    const size_t bits = std::atoi(argv[1]);
21    if(bits < 1024 || bits > 16384)
22       {
23       std::cout << "Invalid argument for bitsize" << std::endl;
24       return 1;
25       }
26 
27    try
28       {
29       Botan::LibraryInitializer init;
30 
31       std::ofstream pub("rsapub.pem");
32       std::ofstream priv("rsapriv.pem");
33       if(!priv || !pub)
34          {
35          std::cout << "Couldn't write output files" << std::endl;
36          return 1;
37          }
38 
39       AutoSeeded_RNG rng;
40 
41       RSA_PrivateKey key(rng, bits);
42       pub << X509::PEM_encode(key);
43 
44       if(argc == 2)
45          priv << PKCS8::PEM_encode(key);
46       else
47          priv << PKCS8::PEM_encode(key, rng, argv[2]);
48       }
49    catch(std::exception& e)
50       {
51       std::cout << "Exception caught: " << e.what() << std::endl;
52       }
53    return 0;
54    }
55