The openssl_cipher_key_length() function is an inbuilt function in PHP that is used to retrieve the key length required for a given cipher algorithm.
Syntax:
openssl_cipher_key_length(string $cipher_algo): int|false
Parameters: This function accepts only one parameter which is described below.
- $cipher_algo: This is a string representing the cipher algorithm.
Return Values: The function returns an integer representing the length in bytes of the key required for the given cipher algorithm, or “false” on failure.
Example 1: The following program demonstrates the openssl_cipher_key_length() function.
PHP
<?php $cipher = "bf-cbc" ; $key_len = openssl_cipher_key_length( $cipher ); echo "Key length for {$cipher}: {$key_len}\n" ; ?> |
Output:
Key length for bf-cbc: 16
Example 2: The following program demonstrates the openssl_cipher_key_length() function.
PHP
<?php $cipher = "AES-256-CBC" ; $key_length = 24; if ( $key_length === openssl_cipher_key_length( $cipher )) { echo "The key length is valid for $cipher." ; } else { echo "The key length is not valid for $cipher." ; } ?> |
Output:
The key length is not valid for AES-256-CBC.
Reference: https://www.php.net/manual/en/function.openssl-cipher-key-length.php