/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/evp.h>
#include <openssl/applink.c>

int main()
{
    RSA *keypair;
    FILE *fppub;
    FILE *fppriv;
    char *password = "Secret";
    /* generate keys */
    keypair = RSA_generate_key(2048, RSA_F4, NULL, NULL);
    /* write public key to file */
    fppub = fopen("pubkey.dat", "wb");
    PEM_write_RSAPublicKey(fppub, keypair);
    fclose(fppub);
    /* write private kety to file */
    fppriv = fopen("privkey.dat", "wb");
    PEM_write_RSAPrivateKey(fppriv, keypair, EVP_des_ede3_cbc(), (unsigned char *)password, strlen(password), NULL, NULL);
    fclose(fppriv);
    return 0;
}
