Search code examples
phpencryptionaesmcrypt

Decrypting Text Using AES in CBC Mode


I have this code

$td = \mcrypt_module_open(\MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
            \mcrypt_generic_init($td, '12345678901234561234567890123456', '12345678901234567890123456789012');
            echo mdecrypt_generic(
                    $td,
                    \mcrypt_generic($td, "Testing")
            );

But the result is ˆ]Ië{ŒÕÌe}Q™‡ÿòø¬ÀÿÙ®»/› Why isnt the text being properly decrypted? I also tried base_64 encoding and decoding in the right places so i think the problem might be elsewhere.


Solution

  • You should reinitialize before decryption, i.e. call crypt_generic_init again. Also, after encryption is finished, you should call mcrypt_generic_deinit.