mcrypt_encrypt not working properly on PHP 5.6.9

一世执手 提交于 2019-12-05 08:24:04

Don't emulate old PHP versions weak behaviour for initializing IV.

Use mcrypt_create_iv().

They removed the auto zero-byte iv for a reason.

Saritha Nair

Found the answer in case anyone need

$ivSize = 8; 
$iv = str_repeat("\0", $ivSize);

$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC, $iv));

Pass a 5th parameter manually which the earlier version was doing on its own!

I would advise you against reinventing the wheel as your function has numerous cryptography engineering flaws.

If you're going to use mcrypt (our recommendations for secure data encryption in PHP are to use libsodium if you can; otherwise defuse/php-encryption; otherwise openssl), make sure you pass the correct constant to mcrypt_create_iv().

Bad:

$iv = mcrypt_create_iv(16, MCRYPT_RAND); // BAD EXAMPLE

Good:

$iv = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM); // YES!
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!