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.