Dirk Janssens wrote:

> Hello,
>
> I want to write a private RSA key to a file with PEM encoding.
> I found the PEM_write_RSAPrivateKey function in the pem.h headerfile.
> It seems to me that it does exactly what I want. But when I use it, all I
> can do is write to stdout. Even with the code below, the PEM encoded key
> appears
> on stdout.
>
>      //
>      // Open the file (some errorcheckingcode is removed in this post)
>      //
>      fdOut=fopen(sOutputKey,"w");
>
>      //
>      // Write PEM (rsa is a pointer to the RSA structure holding the key)
>      //
>      PEM_write_RSAPrivateKey(fdOut, rsa, NULL,NULL,0,NULL,NULL);
>

In this case the private key is not encrypted, and this is not good ;try this
procedure:

EVP_PKEY *pkey;
int len;
FILE *fp;

/*save rsa key in EVP_PKEY structure*/
pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(pkey, rsa)

fp = fopen(priv_file, "wb");
len = strlen(pwd);
PEM_write_PrivateKey(fp, pkey, cipher, (unsigned char *)pwd, len, NULL, NULL);
fclose(fp1);

where:
cipher is the symmetric algorithm used to encrypt the key; (example cipher =
EVP_des_ede3_cbc() )
pwd is the password used to encrypt the key;


>
>      //
>      // Close file
>      //
>      fclose(fdOut);
>
> I also tried it with BIO structures (code found in the documentation
> pages), but
> it gave me identical results... The PEM encoded string is always written to
>  stdout.
> I wanted to try the latest openssl version, but although it compiles
> without a problem,
> my program crashes at the first call to an openssl routine. (I use Visual
> C++ v6).
>
> Am I doing something wrong? I'm using Openssl 0.9.4.
>
> Thanks on advance,
> Dirk
>

I think you need /MDd option for multithreading in VC project options;
and did you call  OpenSSL_add_all_algorithms() and RAND_seed() functions?

>
> ______________________________________________________________________
> OpenSSL Project                               http://www.openssl.org
> User Support Mailing List                    [EMAIL PROTECTED]
> Automated List Manager                           [EMAIL PROTECTED]

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to