mcrypt_decrypt() error change key size

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

    I went ahead and created a function based on Hanky 웃 Panky's answer.

    This can be used with any key length to make sure it's the correct size.

    function pad_key($key){
        // key is too large
        if(strlen($key) > 32) return false;
    
        // set sizes
        $sizes = array(16,24,32);
    
        // loop through sizes and pad key
        foreach($sizes as $s){
            while(strlen($key) < $s) $key = $key."\0";
            if(strlen($key) == $s) break; // finish if the key matches a size
        }
    
        // return
        return $key;
    }
    
    0 讨论(0)
  • 2020-12-06 05:55

    If your encryption code looks like this:

    <?php
      function encryptCookie($value){
        if(!$value){return false;}
        $key = 'aNdRgUkXp2s5v8y/B?E(H+MbQeShVmYq';
        $text = $value;
        $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 trim(base64_encode($crypttext)); //encode for cookie
       }
     function decryptCookie($value){
        if(!$value){return false;}
        $key = 'aNdRgUkXp2s5v8y/B?E(H+MbQeShVmYq';
        $crypttext = base64_decode($value); //decode cookie
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
        return trim($decrypttext);
       }
    ?>
    

    You will want to change the $key to a 128 or 256 bit encrypted code. I simply copied a code that I generated from here: Generate Code

    I created a 256 bit code for mine which consists of 32 characters and thus fixes the issue of the invalid key size of 15 or whatever number is causing the error. So whatever is set for $key you need to change that to a valid code and then it should work fine.

    0 讨论(0)
提交回复
热议问题