Click to See Complete Forum and Search --> : Does mcrypt always take this much time to complete?


aquary
09-29-2008, 01:17 AM
This is my first time trying to implement Mcrypt in my project so I'm not quite sure if I did it right. Before I put Mcrypt in the script, it took like 30ms to complete the script, which has only 3 DB queries. But now with Mcrypt come in to play, it takes 20-30 seconds before I can see the result.

The Mcrypt part is in a function in a class like this

class encryption{
function encrypt($data, $key){
$td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);
mcrypt_generic_init($td, $key, $iv);
$encrypted = mcrypt_generic($td, $data);
mcrypt_generic_deinit($td);
return $encrypted;
}
}


Note that since I'm new with this thing, I copy some code I found somewhere.

Now, the parts where the class was called looks like this

$add["field_1"]=encryption::encrypt($field1_value, $key);
$add["field_2"]=encryption::encrypt($field2_value, $key);
$add["field_3"]=encryption::encrypt($field3_value, $key);

I throw data into the class, it return encrypted data back in to an array and wait for insertion to database. The number of lines here depends on how many data from a form, so it can be only 3 lines or more than 10 lines.

So..... why does it taking to long to execute the script? How can I fix it?

One more question, can I addslashes to the encrypted data? I've addslashes to data before pass them in to the class, but some encrypted data itself has double-quotes in it. If I addslashes to them again, would it damage the encryption?

Thanks in advance :)

EDIT: After posting this thread, the execution time went up and down, the fastest was 1.7s. and the slowest was 33.14s. :(

tfk11
09-29-2008, 02:29 AM
I have no experience with mcrypt. However... your first three lines of code within the function don't require the parameters passed to the function and may be able to just run once during the objects construction rather that once for every call to the function.

Depending on which call is taking the most time it may or may not help.