mcrypt_decrypt() error change key size

2019-01-14 21:31发布

mcrypt_decrypt(): Key of size 15 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported

How Can I fix this issue? my key is set - can not change it. It has to be a local change, I think my local PHP version is too advanced for the project I loaded. How can I fix this?

8条回答
来,给爷笑一个
2楼-- · 2019-01-14 22:24

I had this issue with OSTicket 1.6 ST (yes old version I know). Hosting company just went to PHP 5.6 and it broke the Mail Fetch for cron.php. I'm posting this hoping it helps others fix this issue faster.

You have to edit the file "include/class.misc.php".

Add the function "pad_key" provided in the answer authored by @troskater to the "include/class.misc.php" file and then on line 51 in the function "decrypt" change

return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt,...

to instead use

return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, pad_key($salt),...

查看更多
冷血范
3楼-- · 2019-01-14 22:25

You can just use str_pad() for this. In its simplest form, this will suffice.

function padKey($key) 
{
    // Get the current key size
    $keySize = strlen($key);

    // Set an array containing the valid sizes
    $validSizes = [16,24,32];

    // Loop through sizes and return correct padded $key
    foreach($validSizes as $validSize) {
        if ($keySize <= $validSize) return str_pad($key, $validSize, "\0");
    }

    // Throw an exception if the key is greater than the max size
    throw new Exception("Key size is too large"); 

}

The other answers will do just fine. I'm just taking advantage of the built in PHP function str_pad here instead of appending "\0" in a loop.

查看更多
登录 后发表回答