Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I have done RSA ENCRYPTION using C program and got the encrypted message in character buffer. I need to send this encrypted message to a different system in a binary format.
I have added the RSA_size(rsa) command and free( encrypted). But I am getting coredump on RSA_size(rsa). So I removed it.
Also I want to calculate the size of encrypted variable. But I tried with strlen(), I am getting different length value while execution. 'Bufsize' value always = 256 but the encrypted character value always varies with different values. It should be 256, I hope ...Please suggest.
I need to copy this encrypted value into another character buffer and I have to send as a packet via TCP / IP. I am confused while copying to character buffer.
Need some idea on the above.
Thanks in Advance
Pattabi.

XML
#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>

void EncrCardPin(char *,char *);
int main(int argc, char *argv[])
{

    char* pin="2345";
    //char storebuf[256];

    unsigned char* storebuf = (unsigned char *) malloc(500);
    memset(storebuf,'\0',sizeof(storebuf));

    EncrCardPin(pin,storebuf);

    return 0;
}

void EncrCardPin(char *pin,char *encpin)
 {
    int bufSize;
    char *pubname="publkey.pem"; /***** 2048 bit key********/

    FILE *keyfile = NULL;
    RSA* rsa=NULL;
    FILE *test = NULL;

    //int len=RSA_size(rsa) - 11; /********* Giving Core Dump Always *******/

    unsigned char* encrypted = (unsigned char *) malloc(500);

    test=fopen("test.bin","wb");

    printf("Opening the puclic key file: [%s]\n",pubname);
    keyfile = fopen(pubname, "r");

    if (!keyfile)
    { printf("error occured while reading public key file");    }

    rsa =   PEM_read_RSA_PUBKEY(keyfile, NULL, NULL, NULL);

    if (rsa == NULL)
    {       printf("Badness has occured! Did not read  public key file\n"); }
    else
    {       printf("Opened public key file OK!\n"); }

    bufSize = RSA_public_encrypt(strlen(pin), (unsigned char *) pin, encpin, rsa, RSA_PKCS1_PADDING);
    if (bufSize == -1)
    {   printf("Badness has occured! encryption failed\n");
        RSA_free(rsa);
    }
    else
    {
        printf("Encrypted the message OK! \n");
        fprintf(test,"%s",encpin);
        printf("The buffersize is : [%d]\n ================",bufSize);
    }
    free(encrypted);
    RSA_free(rsa);
    fclose(keyfile);
    fclose(test);


 }


[edit]Comment edited to remove hyphen followed by greater than symbol. This allows the rest of your code to be visible! - OriginalGriff [/edit]
Posted
Updated 12-Apr-11 23:34pm
v2

1 solution

The problem is likely to be here:
RSA* rsa=NULL;
FILE *test = NULL;
//int len=RSA_size(rsa) - 11; /********* Giving Core Dump Always *******/
Since RSA_size requires a pointer to an RSA, handing it a null, will probably give a memory access violation, triggering a core dump.

Try handing it a pointer to an actual RSA object!

[edit]Typo, I missed the "_size" off "RSA_size" - OriginalGriff[/edit]


"Hi ,

I have modified like this
RSA *rsa;
int len = RSA_size(rsa);
But still gives coredump message as "in BN_num_bits()". Don't know from where its referring this . Any clue ????"

Yes - read what I said.

rsa is a variable, which holds a pointer to an RSA object.

You do not assign a valid RSA object to the rsa variable.

When RSA_Size tries to use the RSA object you hand it the pointer of, it accesses the wrong memory.

Would you expect this to work?
int *i;
*i = 6;
Or should it be this?
int *i;
int j;
i = &j;
*i = 6;
 
Share this answer
 
v3
Comments
rprtech 13-Apr-11 5:51am    
Hi ,


I have modified like this
RSA *rsa;
int len = RSA_size(rsa);

But still gives coredump message as "in BN_num_bits()". Don't know from where its referring this . Any clue ????
OriginalGriff 13-Apr-11 5:57am    
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900