mcrypt_decrypt() error change key size

后端 未结 8 1638
慢半拍i
慢半拍i 2020-12-06 05:17

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

相关标签:
8条回答
  • 2020-12-06 05:28

    I had the same problem, but fixed it with this

    public function setKey($key) {
        $len = strlen($key);
        if($len < 24 && $len != 16){
            $key = str_pad($key, 24, "\0", STR_PAD_RIGHT); 
        } elseif ($len > 24 && $len < 32) {
            $key = str_pad($key, 32, "\0", STR_PAD_RIGHT);       
        }elseif ($len > 32){
            $key = substr($key, 0, 32);
        }
        $this->key = $key;
     }
    
    0 讨论(0)
  • 2020-12-06 05:32

    For Laravel 5

    Just run php artisan key:generate:

    Application key [EaaJgaD0uFDEg7tpvMOqKfAQ46Bqi8Va] set successfully.
    

    If you don't see your key updated, just paste it in your .env file.

    APP_KEY=EaaJgaD0uFDEg7tpvMOqKfAQ46Bqi8Va

    Refresh your page

    0 讨论(0)
  • 2020-12-06 05:34

    You don't need to pad the key with "\0".

    I had the same issue when migrating to a new PHP 7 server and I got the message :

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

    The key I had in the code was a string of 19 characters, I simply changed it to a string of 32 characters and everything was fine again.

    So as the error message suggests, use a valid size key.

    0 讨论(0)
  • 2020-12-06 05:34

    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),...

    0 讨论(0)
  • 2020-12-06 05:34

    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.

    0 讨论(0)
  • 2020-12-06 05:52

    Did you update to 5.6? It says

    Invalid key and iv sizes are no longer accepted. mcrypt_decrypt() 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.

    Reference

    Read the last line of that quote, and there you will find your solution :)

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

    That means you need to pad your key with \0 (that's what previous versions were doing for you)

    $key=$key."\0";
    
    0 讨论(0)
提交回复
热议问题