Question

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.

Was it helpful?

Solution

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

OTHER TIPS

It's just a wild guess, but since you're using CBC mode, you probably need to reset the iv before you can decrypt it.

$td = \mcrypt_module_open(\MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
$key = '12345678901234561234567890123456';
$iv =  '12345678901234567890123456789012';

\mcrypt_generic_init($td, $key, $iv);
$encrypted = \mcrypt_generic($td, "Testing");

\mcrypt_generic_init($td, $key, $iv);
echo \mdecrypt_generic($td, $encrypted);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top