Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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. So far googled it and trying hard to find the answer, all are in vain.

Anyone pls suggest how to convert a character buffer to a character Binary array?

CODE SNIPPET:
void EncrCardPin(char *pin,char *encpin)
 {
     unsigned char* encrypted = (unsigned char *) malloc(256);


    int bufSize;
    char *pubname=publkey.pem;

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

    test=fopen("test.txt");

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

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

    }
    printf("Reading the public key block from: [%s] \npubname);
    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");
    }
    printf("Encrypting the message [%s] using public key \n",pin);
    bufSize = RSA_public_encrypt(strlen(pin), (unsigned char *) pin, encrypted, rsa, RSA_PKCS1_PADDING);
    if (bufSize == -1)
    {
        printf("Badness has occured! encryption failed\n");
        RSA_free(rsa);

    }
    else
    {
        printf("Encrypted the message OK! \n");
        //memcpy(encrypted,encpin,1500);
        sprintf(encpin,%s,encrypted);
        fprintf(test,%s,encpin);
        
    }
    RSA_free(rsa);
    fclose(keyfile);
    fclose(test);
 }



******************************************************

Hi,

I have added the RSA_size(rsa) command and free( encrypted). But I am getting coredump on RSA_size(rsa).

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.
Posted
Updated 12-Apr-11 22:39pm
v5

The array encrypted is already a binary array. No conversion is needed. Your code has some other errors that need fixing. For example, you forgot to free() the encrypted array, and its size should be RSA_size(rsa) instead of being fixed to 256 bytes.
 
Share this answer
 
If you want to write binary data to a file, you must open it with "wb" argument (write binary). And since your output file will contain binary data, I suggest not to name it ".txt".

Then, you shouldn't use fprintf but fwrite:

FILE* test = NULL;
...
test = fopen("test.bin", "wb");
...
fwrite(buffer, 1, numberOfBytesToWrite, test);
...


If you want to open and read your binary file, use fopen(..., "rb") and fread(...).
 
Share this answer
 
rprtech wrote:
printf("Reading the public key block from: [%s] \npubname);

I see a mistake in the above line.



rprtech wrote:
Anyone pls suggest how to convert a character buffer to a character Binary array?

You simply don't need that (as a character buffer already is a binary array or, in other words, a binary array is a superset of a character buffer - if you mean ASCII buffer -).





 
Share this answer
 

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