Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are these strange characters appearing in mcrypt?

I encrypt and decrypt successfully, but when I decrypt the value, there appears strange characters at the end of the string, "���". The initial $_POST['value'] do not have any blank space or any strange character.

How can I solve this?

I encrypt with this:

$key = 'my key';
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);        
$id = mcrypt_generic($td, $_POST['value']);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

I decrypt with this:

$key = 'my key';
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$id = mdecrypt_generic($td, $_COOKIE['value']);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

1 Answers

It is just padding the result based on the block size used. If you use rtrim(), you will get rid of them.

like image 114
Ben Avatar answered Jan 30 '26 20:01

Ben