Again I have a problem with mcrypt. I am wondering why am I getting diffrent results in php and c bei encryping with rinjdael-128 cfb mode. Here is my php function:
function mcencrypt($Clear, $Pass){
$td = mcrypt_module_open('rijndael-128', '', 'cfb', '');
$iv = 'AAAAAAAAAAAAAAAA';
mcrypt_generic_init($td, $Pass, $iv);
$encrypted = mcrypt_generic($td, $Clear);
mcrypt_generic_deinit($td);
return $encrypted;
}
and my c function:
unsigned char *Encrypt( unsigned char *key, unsigned char *message, int buff_len){
unsigned char *Res;
MCRYPT mfd;
char* IV = "AAAAAAAAAAAAAAAA";
int i, blocks, key_size=16, block_size;
mfd = mcrypt_module_open("rijndael-128", NULL, "cfb", NULL);
block_size = mcrypt_enc_get_block_size(mfd);
blocks = ceil((double)buff_len/block_size);
mcrypt_generic_init(mfd, key, key_size, IV);
Res = calloc(1, (blocks *block_size)+1);
strncpy(Res, message, buff_len);
mcrypt_generic(mfd,Res,(blocks *block_size));
mcrypt_generic_deinit(mfd);
mcrypt_module_close(mfd);
return (Res);
}
when I use them in a for loop the first 26 encryptions are korect, but at the 27 the last tree signs are diffrent. This is realy very odd.
in php I make the for loop like this:
$seed ="m78otPfBLT48msvd";
$key = "r2oE61IQo7VwFXnF";
$start_y= mcencrypt($seed,$key);
$new =$start_y;
$enc_prob = mcencrypt($start_y,$key);
for ($j=1; $j<30;$j++){
$new.=$enc_prob;
echo "j: ".$j.'<br>';
echo "the new: ".$enc_prob."lenght: ".strlen($enc_prob).'<br>';
echo "new in hex for j=".$j.": ".strToHex($enc_prob).'<br>';
$enc_prob=mcencrypt($enc_prob,$key);
}
and in c like this:
int j, buff_len;
char seed[] = "m78otPfBLT48msvd", key1[]="r2oE61IQo7VwFXnF";
buff_len = strlen(seed);
unsigned char *encrypted, *encrypted2;
encrypted = Encrypt(key1, seed, buff_len);
encrypted2 = Encrypt(key1, encrypted, buff_len);
for (j=1; j<cols; j++) {
printf("j: %d\n", j);
printf("encrypted2 %s\n", string_to_hex(encrypted2, buff_len));
memcpy(encrypted2, Encrypt(key1, encrypted2, buff_len),buff_len);
}
free(encrypted);
free(encrypted2);
what is going wrong. The message and the key are the same. I'm using the same library, the same iv. I don't get it. It is strange. Please help me out on this one. Many thanks in advance!!
I figured out what the problem was. The strings in c are null terminated and by using strncpy in my encryption function was making the problems, so I just changed that in memcpy and now everything goes well. Thanks again to everybody who tried to help and took time to take a look at my problem.