mcrypt_encrypt not working properly on PHP 5.6.9

拥有回忆 提交于 2019-12-22 06:58:31

问题


I have the following code which worked fine on PHP 5.5.9.

function index()
{
    echo $this->encryptText_3des('TEST','JHHKJH9879');
}

function encryptText_3des($plainText, $key) {
    $key = hash("md5", $key, TRUE); 
    for ($x=0;$x<8;$x++) {
        $key = $key.substr($key, $x, 1);
    }
    $padded = $this->pkcs5_pad($plainText,
    mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC));
    return $encrypted;
}

function pkcs5_pad ($text, $blocksize)  
{
    $pad = $blocksize - (strlen($text) % $blocksize);
    return $text . str_repeat(chr($pad), $pad);
}

The encryption was happening fine.But in 5.6.9, the in the PHP doc of mcrypt_encrypt, they mention that

Invalid key and iv sizes are no longer accepted. mcrypt_encrypt() will now throw a warning and return FALSE if the inputs are invalid. Previously keys and IVs were padded with '\0' bytes to the next valid size.

How will I modify my current code with the fifth parameter without altering the encryption algorithm?

I tried

$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

and given $iv as fifth parameter.

But it didn't work out. The encryption was different from the earlier one.


回答1:


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.




回答2:


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!




回答3:


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

  • Don't use 3DES, use AES. The correct mcrypt constant for AES is MCRYPT_RIJNDAEL_128, regardless of your desired key size. Mcrypt is pretty terrible.
  • Don't use md5() as a key derivation function. If you find yourself needing a KDF (e.g. because you're using a password instead of storing an encryption key), use hash_pbkdf2() with SHA-256.
  • You are encrypting but not authenticating your ciphertext. Secret-key encryption without message authentication is NOT secure in any language! Encrypt-Then-MAC please.

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!


来源:https://stackoverflow.com/questions/30475946/mcrypt-encrypt-not-working-properly-on-php-5-6-9

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