Reputation: 93
I have this code:
$token = $this->hextobin($dataEncrypt);
$key = $this->key_192;
$iv = $this->iv;
$algorithm = 'xxxx';
$mode = 'ecb'; //QUESTION!!!
$td = mcrypt_module_open($algorithm, '', $mode, '') ;
$iv = substr($iv, 0, mcrypt_enc_get_iv_size($td));
$expected_key_size = mcrypt_enc_get_key_size($td);
$key = substr($key, 0, $expected_key_size);
mcrypt_generic_init($td, $key, $iv);
$response = trim(mdecrypt_generic($td, $token), '');
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$response = explode(XXXX,$response);
return $response[0];
But reading the PHP documentation, specifically: http://php.net/manual/en/function.mcrypt-ecb.php says that thus is obsolete and recommend mcrypt_generic() and mdecrypt_generic() for replacement. My question is how to use these functions if you ask me the same way the open mode?
Upvotes: 0
Views: 181
Reputation: 28921
To use mcrypt_generic()
is explained here:
However, I would recommend the simpler API mcrypt
:
Example usage:
mcrypt_encrypt(MCRYPT_3DES, "secret key", "data to encrypt", MCRYPT_MODE_ECB);
I noticed you had question marks on ecb
. This is the "mode" for the encryption explained here:
http://php.net/manual/en/mcrypt.constants.php
According to the PHP Docs:
Update
If your using the CBC
mode, remember to set the iv
like so:
$size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
echo mcrypt_encrypt(MCRYPT_3DES, "secret key", "data", MCRYPT_MODE_CBC, $iv);
Upvotes: 2