Friends,

This is the first time I'm using Openssl for some small job to encrypt and
decrypt buffers for my application usage. My requirement is simple:

1) My application gets a buffer that needs to be encrypted.
2) My application gets a buffer that needs to be decrypted. This buffer can
be exactly same as the one I get after encryption in step 1. Alternatively,
I'll copy this in a file, save it to the disk, decrypt it using Openssl
command line tools, encrypt again (using the same password as used in step
1) and feed it to the application. I should get what I originally had or
with modifications I did.

Here's the code I'm using:
+++++++++++++++++++++++++++++++++++++++++++++++++
function1()
{

//get the buffer *rg_conf_buf *......read from a file or anywhere else

        char *rg_conf_buf_dup=NULL; //buffer for encrypted data
char *rg_conf_buf_dup_2=NULL; //buffer for decrypted data
int rg_conf_len = strlen(rg_conf_buf);
 //int i=0;
 rg_conf_buf_dup = (char *)zalloc_e(rg_conf_len);
rg_conf_buf_dup_2 = (char *)zalloc_e(rg_conf_len);
memcpy(rg_conf_buf_dup,rg_conf_buf,rg_conf_len); //make copy of original buf

rg_error(LCONSOLE, "\n%s : rg_conf buf size = %d\n",
__FUNCTION__,rg_conf_len); //print the size

//for (i=0;i<len;i++)
// console_printf("%c",rg_conf_buf_dup[i]);

encode2(rg_conf_buf,rg_conf_buf_dup); //encrypt and get the result
in rg_conf_buf_dup
decode2(rg_conf_buf_dup,rg_conf_buf_dup_2,rg_conf_len); //decrypt and get
the result in rg_conf_buf_dup_2, passing original buffer's length

if (!memcmp(rg_conf_buf,rg_conf_buf_dup_2,rg_conf_len))
    rg_error(LCONSOLE, "\n%s : Decrypting OK\n", __FUNCTION__);
else
    rg_error(LCONSOLE, "\n%s : Decrypting NOT OK\n", __FUNCTION__);
}

void encode2(char *inbuf,char *outbuf)
{
unsigned char key32[] = "As different as chalk and cheese";
unsigned char iv[] = "As dark as pitch";

AES_KEY aeskey;
 memset(outbuf, 0, sizeof(outbuf));

AES_set_encrypt_key(key32, 32*8, &aeskey);
 AES_cbc_encrypt(inbuf, outbuf, strlen(inbuf), &aeskey, iv, AES_ENCRYPT);

return;
}

void decode2(char *inbuf,char *outbuf,int len)
{
unsigned char key32[] = "As different as chalk and cheese";
unsigned char iv[] = "As dark as pitch";
 AES_KEY aeskey;
 memset(outbuf, 0, sizeof(outbuf));

AES_set_decrypt_key(key32, 32*8, &aeskey);
 AES_cbc_encrypt(inbuf, outbuf, len, &aeskey, iv, AES_DECRYPT);

return;
}
+++++++++++++++++++++++++++++++++++++++++++++++++

In the above code I'm not modifying the original buffer (*rg_conf_buf )
after encryption.*
*I see the message "Decrypting OK".*
*
*
*But when I print out the two buffers rg_conf_buf and rg_conf_buf_dup_2, they
are different. *
*
*
*Please help me understand where I'm going wrong.*
*
*
*Also, the size of rg_conf_buf is variable. Should I create the buffers to
hold encrypted data of the same size as rg_conf_buf ?*
*
*
*Please help.*
*
*
*Thanks,*
*Kunal*
*
*
*
*

Reply via email to