Sunday, 8 June 2008

Generating RSA Key Pair Programmatically using OpenSSL

#include "stdafx.h"
#include <conio.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>
#include <openssl/x509.h>
#include <stdlib.h>
#include <stdio.h>


int _tmain(int argc, _TCHAR* argv[])
{
RSA *myrsa;
unsigned long e = RSA_3;
BIO* out = NULL
FILE* fp;
myrsa = RSA_generate_key(2048,e,NULL,NULL);

out=BIO_new(BIO_s_file());


if(myrsa==NULL){
printf("error in generating keypair..");
printf("press any key to exit..");
_getch();
}

fp=fopen("rsakeypair.txt","wb");
out = BIO_new_fp(fp,BIO_CLOSE);
BIO_printf(out,"\n");
RSA_print(out,myrsa,0);
fclose(fp); _getch(); return(0);
}




The above example program generates a 2048 bit RSA Key pair. It also generates the p,q,n,e and d sections into the text file. In order to build this sample using Visual C++, you will need to build OpenSSL first. After you build OpenSSL, you can then include the generated headers to your VC/Include folder. You will also need to include the the lib files generated by OpenSSL onto VC/Lib. The above example program is written in VS05. OpenSSL simply rocks!



Code + Demo + ReadmeFile : Here

5 comments:

  1. I've followed what you said, but i've got this error-link:
    Linking...
    main.obj : error LNK2019: unresolved external symbol _RSA_print referenced in function _main
    main.obj : error LNK2019: unresolved external symbol _BIO_printf referenced in function _main
    main.obj : error LNK2019: unresolved external symbol _BIO_new_fp referenced in function _main
    main.obj : error LNK2019: unresolved external symbol _BIO_new referenced in function _main
    main.obj : error LNK2019: unresolved external symbol _BIO_s_file referenced in function _main
    main.obj : error LNK2019: unresolved external symbol _RSA_generate_key referenced in function _mai

    Ray

    ReplyDelete
  2. Hi,
    The unresolved externals are because of the missing 'Library' files.
    Include references to the following .lib files.

    1] libeay32.lib and
    2] ssleay32.lib. (copy them to VC++ lib directory).

    This can be done by right clicking on your Project > Properties > Linker > Input > Additional Dependencies > type the above names with a space.

    Rebuild your application. It should work smoothly.

    I've included the demo and the source(has the above lib files) Check it out and do let me know.

    ReplyDelete
  3. Hi,
    thank you.
    I was missing the input>additional dependencies.

    It's working on VS2005, but not on VS2008:
    'Microsoft Visual Studio C Runtime Library has detected a fatal error'
    BTW: it doesn't matter. thanks again.

    Ray

    ReplyDelete
  4. I found this: "I've compiled a program under Windows and it crashes: why?"

    and now everything seems fine (in VS2008, as well).

    I'm really a newbie about openSSL.
    Have you ever tried to sign a message with a RSA key? 'cos that's my goal.

    Ray

    ReplyDelete
  5. Hello,

    I got the same error in Embarcadero C++, what should I do to include those references?

    Thanks in advance,

    Javi

    ReplyDelete