Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mcrypt_decrypt() error change key size

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?

like image 911
Asaf Maoz Avatar asked Dec 02 '14 16:12

Asaf Maoz


2 Answers

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";
like image 151
Hanky Panky Avatar answered Sep 30 '22 16:09

Hanky Panky


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;
}
like image 26
troskater Avatar answered Sep 30 '22 16:09

troskater