[EMAIL PROTECTED] wrote:
> 
> Hi,
> 
> I hope somebody will help me.
> For the last two weeks i am trying to write RSA keys to disk.
> I want two files, one with the private Key and one with the Public key (this one
> is distributed).
> For some unknown reason nothing I have tried will work. I tried to write the
> files with and without BIO and FP, I have tried to convert from ASN to DER and
> then write to disk. But always something fails, then the write succeeds but the
> readback fails, then the write fails en the readback succeeds.
> 

OK if it'll make you happy :-)

#include <openssl/pem.h>
main()
{
RSA *rsa, *rsa_pub, *rsa_priv;
FILE *in, *out;

/* Generate RSA key */
rsa =RSA_generate_key(1024,0x10001,NULL,NULL);

out = fopen("pubkey.pem", "w");
PEM_write_RSAPublicKey(out, rsa);
fclose(out);

out = fopen("privkey.pem", "w");
PEM_write_RSAPrivateKey(out, rsa, NULL, NULL, 0, NULL, NULL);
fclose(out);

in = fopen("pubkey.pem", "r");
rsa_pub = PEM_read_RSAPublicKey(in, NULL, NULL, NULL);
fclose(in);

in = fopen("privkey.pem", "r");
rsa_priv = PEM_read_RSAPrivateKey(in, NULL, NULL, NULL);
fclose(in);

printf("Public Key:\n");
RSA_print_fp(stdout, rsa_pub, 0);

printf("\n\nPrivate Key:\n");
RSA_print_fp(stdout, rsa_priv, 0);

}

In practice error checking would be performed after the reads and the
fopen calls but that's been omitted to keep it simple.

Steve.
-- 
Dr Stephen N. Henson.   http://www.drh-consultancy.demon.co.uk/
Personal Email: [EMAIL PROTECTED] 
Senior crypto engineer, Celo Communications: http://www.celocom.com/
Core developer of the   OpenSSL project: http://www.openssl.org/
Business Email: [EMAIL PROTECTED] PGP key: via homepage.

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

Reply via email to