mcrypt_decrypt() error change key size

后端 未结 8 1660
慢半拍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: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.

提交回复
热议问题