Hmm,
take a look at routines like RSA_new() to create RSA structures. As you coded 'sizeof apub', this will return the size of a _pointer_ - assuming a 32-bit architecture you will get round about four bytes ;-).
See: http://www.openssl.org/docs/crypto/RSA_new.html




The runtime error is caused by calling RSA_size() with a null pointer - unfortnunfortunately RSA_size() doesn't like null pointers.
See: http://www.openssl.org/docs/crypto/RSA_size.html



Good luck, Sebastian


Hi all,
I'm trying to develop a C++ application to encrypt and decrypt data using RSA public key cryptography scheme. I have generated the public/private keys using OpenSSL command line tool. The following C++ code should read a public key, encrypt data, read private key and decrypt the data:
********************************************************************
#include <winsock2.h>
#include <iostream.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/engine.h>
int main()
{ char *message ="Hello World!"; RSA *apub;
RSA *aprivate;
FILE *f;
int ret;
unsigned char *buf;
unsigned char *e_data;
unsigned char *clear_text;


//Get key
f= fopen("a_rsa_public","rb");
if(f == NULL)
{
printf("\nError opening public key file");
return -1;
}
else
printf("\n Public key file opened");
//load the key
if ( fread(&apub,sizeof apub,1,f) != 1)
{
printf("\nError reading public key");
return -1;
}
else
printf("\nPublic key read");
//close the key file
fclose(f);
buf = (unsigned char *) malloc(strlen(message)); memcpy(buf,message,strlen(message));
e_data = (unsigned char *) malloc(RSA_size(apub)); // THIS is where i get a run time error
//encrypt data
RSA_public_encrypt(strlen(message),buf, e_data, apub, RSA_PKCS1_OAEP_PADDING);


//------------------decrypt
//Get key
f= fopen("a_rsa_private","rb");
if(f == NULL)
{
printf("\nError opening private key file");
return -1;
}
//load the key
ret = fread(&aprivate,sizeof(aprivate),1,f);
//close the key file
fclose(f);
//make sure we loaded ok
if(ret != 1)
{
printf("\nError reading private key");
return -1;
}


clear_text= (unsigned char *) malloc(strlen(message));
RSA_private_decrypt(strlen((char*)e_data), e_data, clear_text, aprivate, RSA_PKCS1_OAEP_PADDING);
return 0;
}
*******************************************************************************
At first I used to get a run time error in the RSA_public_encrypt(...); and I figured caused I had e_data initialized as:
e_data = (unsigned char *) malloc(strlen(message)*4);
So instead I used :
e_data = (unsigned char *) malloc(RSA_size(apub));
and now I'm getting a run time as this line is encountered.
I'm sure someone with experience would be able to spot my mistake.
I thank you all in advance for your help.




__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

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

Reply via email to