PHP how encode/decode text with key?

user2881809 picture user2881809 · May 27, 2014 · Viewed 31.3k times · Source

Code:

$result = mcrypt_ecb (MCRYPT_3DES, 'test', $string, MCRYPT_ENCRYPT);

It code encode $string. But how decode $result?

Tell me please how decode $result ?

Answer

Rakesh Sharma picture Rakesh Sharma · May 27, 2014

Decrypt:

//Encryption
$result = mcrypt_ecb (MCRYPT_3DES, 'test', $string, MCRYPT_ENCRYPT);
//Decryption
$decrypt_result = mcrypt_ecb (MCRYPT_3DES, 'test', $result, MCRYPT_DECRYPT);

You need to change mode in arguments and pass encrypted values.

NOTE: mcrypt_generic() has also been DEPRECATED as of PHP 7.1.0.

Read manual: http://www.php.net/manual/en/function.mcrypt-ecb.php.

Better to use mcrypt_generic().

$cc = 'my secret text';
$key = 'my secret key';
$iv = '12345678';

$cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');

mcrypt_generic_init($cipher, $key, $iv);
$encrypted = mcrypt_generic($cipher,$cc);
mcrypt_generic_deinit($cipher);

mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,$encrypted);
mcrypt_generic_deinit($cipher);

echo "encrypted : ".$encrypted;
echo "<br>";
echo "decrypted : ".$decrypted;