Figuring out the exact key created by PHP's mcrypt

后端 未结 2 1650
情深已故
情深已故 2021-01-11 12:53

A PHP application I\'m maintaining uses Rijndael_256 with EBC_MODE encryption with mcrypt. Fun has it that the key isn\'t 256 bits long, but only 160. According to the mcryp

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-11 13:41

    '\0' means NULL, the hex value of it is 00 ! So i tested 3 codes, and they returned all the same :)

    1. Let mcrypt_encrypt do the '\0' padding
    2. Added with PHP NULL values
    3. Added with PHP converted 0 hex values

    Code:

    function encryptThis($text,$key){
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
        return ($crypttext);
    }
    
    echo bin2hex(encryptThis("Meet me at 11 o'clock behind the monument.", "abcdefghijklmnopqrstuvwxyz"))."
    "; echo bin2hex(encryptThis("Meet me at 11 o'clock behind the monument.", "abcdefghijklmnopqrstuvwxyz" . NULL . NULL . NULL . NULL . NULL . NULL))."
    "; echo bin2hex(encryptThis("Meet me at 11 o'clock behind the monument.", "abcdefghijklmnopqrstuvwxyz" . hex2bin(0) . hex2bin(0) . hex2bin(0) . hex2bin(0) . hex2bin(0) . hex2bin(0)))."
    "; ?>

提交回复
热议问题