On Tue, 14 Apr 1998, Ivan Pau wrote:
> I am doing a project about security using the SSLeay but i have some
> troubles using the "d2i_RSAPublicKey" and "d2i_RSAPrivateKey" routines. I
> need to write the publicKey and the privateKey into a file, and i got it
> using "i2d_RSAPublicKey" and "i2d_RSAPrivateKey" routines. But if i read
> the keys from the file, i can not keep them in a RSA structure when i  use
> the routines mentioned above. 

Example code follows.  I only load the values into a char array, writing to
a file is left as an excersise.  There is also the option of writing them
base64 encoced.

eric
---
#include <stdio.h>
#include "rsa.h"

/* This is a simple program to generate an RSA private key.  It then
 * saves both the public and private key into a char array, then
 * re-reads them.  It saves them as DER encoded binary data.
 */

void callback(stage,count,arg)
int stage,count;
char *arg;
        {
        FILE *out;

        out=(FILE *)arg;
        fprintf(out,"%d",stage);
        if (stage == 3)
                fprintf(out,"\n");
        fflush(out);
        }

main()
        {
        RSA *rsa,*pub_rsa,*priv_rsa;
        int len;
        unsigned char buf[1024],*p;

        rsa=RSA_generate_key(512,RSA_F4,callback,(char *)stdout);

        p=buf;

        /* Save the public key into buffer, we know it will be big enough
         * but we should really check how much space we need by calling the
         * i2d functions with a NULL second parameter */
        len=i2d_RSAPublicKey(rsa,&p);
        len+=i2d_RSAPrivateKey(rsa,&p);

        printf("The public and private key are now both in a char array\n");
        printf("and are taking up %d bytes\n",len);

        RSA_free(rsa);

        p=buf;
        pub_rsa=d2i_RSAPublicKey(NULL,&p,(long)len);
        len-=(p-buf);
        priv_rsa=d2i_RSAPrivateKey(NULL,&p,(long)len);

        if ((pub_rsa == NULL) || (priv_rsa == NULL))
                ERR_print_errors_fp(stderr);

        RSA_free(pub_rsa);
        RSA_free(priv_rsa);
        }


+-------------------------------------------------------------------------+
| Administrative requests should be sent to [EMAIL PROTECTED] |
| List service provided by Open Software Associates, http://www.osa.com/  |
+-------------------------------------------------------------------------+

Reply via email to