PHP openssl_cipher_key_length() Function
Last Updated :
28 Apr, 2025
Improve
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
$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
$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