Getting all possible key sizes for PHP MCrypt Ciphers

214 views Asked by At

I'm creating public packages, There are some encryption there, I let developer to choose cipher type and mode and set the key for encryption part of the packeges. Now I need to check the key size before using it in MCrypt functions. So what I have to know is:

1) All possible key sizes for the cipher.

2) Byte size of the given key.

Or if you have a better way, please share it.

1

There are 1 answers

0
Artjom B. On BEST ANSWER
  • mcrypt_list_algorithms() gives you the list of ciphers
  • mcrypt_module_get_supported_key_sizes($cipher) gives you the supported key sizes (is empty if the keysizes are continous)
  • mcrypt_module_get_algo_key_size($cipher) gives you the maximum key size in case the previous function returned nothing

All key sizes are given in bytes.

Example:

$algorithms = mcrypt_list_algorithms();

foreach ($algorithms as $cipher) {
    echo "$cipher:\n";
    $keysizes = mcrypt_module_get_supported_key_sizes($cipher);
    if (count($keysizes) == 0) {
        $max = mcrypt_module_get_algo_key_size($cipher);
        echo "  max: $max\n";
    } else {
        foreach ($keysizes as $keysize) {
            echo "  $keysize\n";
        }
    }
    echo "\n";
}

Use strlen($input) to retrieve the bytes in a given string (should be decoded).