2 #include <openssl/rsa.h>
4 /* This is a simple program to generate an RSA private key. It then
5 * saves both the public and private key into a char array, then
6 * re-reads them. It saves them as DER encoded binary data.
9 void callback(stage
,count
,arg
)
16 fprintf(out
,"%d",stage
);
24 RSA
*rsa
,*pub_rsa
,*priv_rsa
;
26 unsigned char buf
[1024],*p
;
28 rsa
=RSA_generate_key(512,RSA_F4
,callback
,(char *)stdout
);
32 /* Save the public key into buffer, we know it will be big enough
33 * but we should really check how much space we need by calling the
34 * i2d functions with a NULL second parameter */
35 len
=i2d_RSAPublicKey(rsa
,&p
);
36 len
+=i2d_RSAPrivateKey(rsa
,&p
);
38 printf("The public and private key are now both in a char array\n");
39 printf("and are taking up %d bytes\n",len
);
44 pub_rsa
=d2i_RSAPublicKey(NULL
,&p
,(long)len
);
46 priv_rsa
=d2i_RSAPrivateKey(NULL
,&p
,(long)len
);
48 if ((pub_rsa
== NULL
) || (priv_rsa
== NULL
))
49 ERR_print_errors_fp(stderr
);