• php5.6 的mcrypt_encrypt 函数可以和5.5.9的行为一样


    php5.5.9

    -----------------------
    $output = "test  php !!"

    $key = "abcd123456789";

    $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $output, MCRYPT_MODE_CBC);

    $output = bin2hex($output);

    echo $output;

     
    ---------------------------------------------------
    php5.6 之后,mcrypt_encrypt 必须要 带 第五个参数 $iv,  但是加密出来的结果也可以和5.5的行为一样
    1. php5.6 中 key 必须要8,16,32个字符;(之前的只有15个字符,php5.5其实是自动填 )
    2. php5.6 中 iv 必须要 ; (之前的版本, php5.5应该是默认填 16个 (至于是8个, 还是16个,32个,这个是和前面的key的长度有关的!));
    所有,基于以上的原因,便有了下面php5.6对应的代码:
    --------------------------------------------------------
     

    $output = "test  php !!"

    $key = "abcd123456789"."";
    $iv = str_repeat("", 16);
    $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $output, MCRYPT_MODE_CBC, $iv);

    $output = bin2hex($output);

    echo $output;

    -------------------------------------------

    Found the answer in case anyone need

    $ivSize = 8; 
    $iv = str_repeat("", $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!

    --------------------------------------------------------------

    参考:https://stackoverflow.com/questions/30475946/mcrypt-encrypt-not-working-properly-on-php-5-6-9/30477958#30477958

    Asked 4 years, 5 months ago
    Viewed 6k times
    5

    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 '' 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.

     

    3 Answers

    4

    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("", $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!

  • 相关阅读:
    JB的IDE可视化MongoDB、MySQL数据库信息
    爬取QQ音乐(讲解爬虫思路)
    selenium实现淘宝的商品爬取
    爬取中国福彩网并做可视化分析
    Django的学习进阶(二)———— name
    xpath获取一个标签下的多个同级标签
    selenium中动作链的使用
    Error: Cannot find module 'electron-prebuilt'
    error Invalid tag name "–save-dev": Tags may not have any characters that encodeURIComponent encodes
    TypeError:mainWindow.loadUrl is not a function
  • 原文地址:https://www.cnblogs.com/oxspirt/p/11860017.html
Copyright © 2020-2023  润新知