how to search value with some encrypted data
how to search value with some encrypted data
Hi everyone,
in my datatable i used the aes_encrypt function with the set and getformatters functions.
here's an exemple :
Field::inst( 'pays_crypt' )
->getFormatter( function ( $val, $data, $opts ) use ( $key ) {
return php_aes_decrypt( $val, $key );
} )
->setFormatter( function ( $val, $data, $opts ) use ( $key ) {
return php_aes_encrypt( $val, $key );
} ),
This the function,i've put in my serverside.php.
function php_aes_encrypt($text, $key) {
$key = mysql_aes_key($key);
// $text = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $text);
// $text = utf8_encode($text);
$text = preg_replace('/[\x00-\x1F]/', '', $text);
$pad_value = 16 - (strlen($text) % 16);
$text = str_pad($text, (16 * (floor(strlen($text) / 16) + 1)), chr($pad_value));
return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_DEV_URANDOM));
}
function php_aes_decrypt($text, $key) {
$key = mysql_aes_key($key);
$text1 = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_DEV_URANDOM));
$text1 = rtrim($text1, "\0..\16");
// $text1 = utf8_encode($text1);
// $text1 = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $text1);
$text1 = preg_replace('/[\x00-\x1F]/', '', $text1);
return $text1;
}
function mysql_aes_key($key) {
$new_key = str_repeat(chr(0), 16);
for($i=0,$len=strlen($key);$i<$len;$i++)
{
$new_key[$i%16] = $new_key[$i%16] ^ $key[$i];
}
return $new_key;
}
it works perfectly for displaying, modifying et creating my data.
However, the global searching function doesn't seems to work.
Do you know, how can i make it works ?
The only idea i got is to use some unencrypted data in some invisible columns. to apply the searching
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This question has an accepted answers - jump to answer
Answers
If you are using server-side processing (which I assume you are) the above won't allow searching to work on the unencrypted data since the formatting is being done in PHP while the search is being done in SQL.
The only option is to move your encryption code into SQL so the data to search can be decrypted before it is searched. If this is with the Editor PHP libraries (which it appears to be), then unfortunately there is no way to do that at the moment as they don't provide a method to use SQL functions with them.
Allan