Hello:
 
I send you a function that read RSA public and private key from a file:
 
RSA *RecuperaClavesRSA(int type, char *pemfile)
{
  FILE *fp;
  RSA *key=NULL;
  switch (type){
   case READPUB:
      if((fp = fopen(pemfile,"r")) == NULL) {
         fprintf(stderr,"Error: Public Key file doesn't exists.\n");
         exit(EXIT_FAILURE);
      }
      if((key = PEM_read_RSAPublicKey(fp,NULL,NULL,NULL)) == NULL) {
         fprintf(stderr,"Error: problems while reading Public Key.\n");
         exit(EXIT_FAILURE);
      }
      fclose(fp);
    printf("RSA size: %d", RSA_size(key));
 
      return key;
      break;
   case READSEC:
      if((fp = fopen(pemfile,"r")) == NULL) {
         fprintf(stderr,"Error: Private Key file doesn't exists.\n");
         exit(EXIT_FAILURE);
      }
      if((key = PEM_read_RSAPrivateKey(fp,NULL,NULL,NULL)) == NULL) {
         fprintf(stderr,"Error: problmes while reading Private Key. %d %s\n",type,pemfile);
         exit(EXIT_FAILURE);
      }
      fclose(fp);
      if(RSA_check_key(key) == -1) {
         fprintf(stderr,"Error: Problems while reading RSA Private Key in \
         '%s' file.\n",pemfile);
         exit(EXIT_FAILURE);
      } else if(RSA_check_key(key) == 0) {
         fprintf(stderr,"Error: Bad RSA Private Key readed in '%s' \
         file.\n",pemfile);
         exit(EXIT_FAILURE);
      }
      else
         return key;
      break;
  }
  return key;
}
If the parameter "type" is "READPUB" it read the public key from the file "pemfile", and if this parameter is "READSEC", it read the private key.
 
Too, this function show RSA size correctly.
 
I hope this function solve your problem.
 
Regards.
 
P.D.: Your name remember me a very famous song of Eric Clapton ... ;-)
----- Original Message -----
From: Layla
Sent: Tuesday, May 10, 2005 3:55 PM
Subject: Re: Loading RSA keys from file.

Hi Angel,
Thank you for your response. I have changed my code with accordance to your suggestion but I'm still getting a runtime error when attempting to read the key from its file.
So far I'm having trouble with 1- reading the key from file, and 2- RSA_size() , this function generates a runtime error when encountered as well. I can't think of anything since I'm initializing my RSA object.
Suggestions ?

Angel Martinez Gonzalez <[EMAIL PROTECTED]> wrote:
Hello Layla:
 
Maybe, your error disapear if you will change the following:
 
apub = PEM_read_RSAPublicKey(f, NULL, NULL, NULL);  
 
Regards.
----- Original Message -----
From: Layla
Sent: Tuesday, May 10, 2005 9:30 AM
Subject: Loading RSA keys from file.

I'm still not able to load the key from its file and I'm still encountering an error when I use RSA_size(); a run time error occured when I try to print the returned size. I'm including a segment of my code after modification:
 
************************************************************
RSA *apub;
FILE *f;
 
 
seed_prng(); // my function for seeding PRNG
 
//Allocating apub
apub = RSA_new();
 
if ( apub == NULL)
     //print error mesage
 
//open key file
f= fopen ("a_rsa_public","r");
if (f == NULL)
   //print error message
 
//Loading key
apub = PEM_read_RSAPublicKey(f, &apub, 0,0);   //a run time error occurs here
if (apub == NULL)
{
    // print error message
   return -1;
}
 
/* if I try the following line after the allocation of the RSA object I get a runtime error as well*/
printf("RSA size: %d", RSA_size(apub);
*******************************************************************************
I'm thankful for any help I can get.
 

Sebastian <[EMAIL PROTECTED]> wrote:
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
> #include
> #include
> #include
> #include
>
>
> 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]


Yahoo! Mail Mobile
Take Yahoo! Mail with you! Check email on your mobile phone.


Yahoo! Mail Mobile
Take Yahoo! Mail with you! Check email on your mobile phone.

Reply via email to