Skip to main content

Insecure encryption algorithm - Anonymous cipher suites


Implementation of secure encryption algorithms and disallowing anonymous cipher suites


  • Usage of PHP 7.1+ for server-side scripting and web development
  • Usage of OpenSSL for secure communication and encryption


Non compliant code

$cipher = 'AES-256-CBC';
$key = 'your-secret-key';
$iv = substr(hash('sha256', $key), 0, 16);
$encrypted = openssl_encrypt($data, $cipher, $key, 0, $iv);

The code above is a simple PHP script that uses the OpenSSL library to encrypt data. The encryption algorithm used here is 'AES-256-CBC'.

The $cipher variable specifies the encryption algorithm, 'AES-256-CBC'.

The $key variable is the secret encryption key. This key should be kept confidential and not be exposed to potential attackers.

The $iv variable is the initialization vector. It's derived from a SHA-256 hash of the secret key and is used to ensure that the same plaintext will not result in the same ciphertext when encrypted multiple times.

The $encrypted variable stores the result of the encryption process.

The vulnerability lies in the encryption algorithm 'AES-256-CBC'. Although it's a strong encryption algorithm, it doesn't provide authentication, which means that it's vulnerable to attacks such as the padding oracle attack.

In addition, the use of a static key and IV (Initialization Vector) also poses a security risk. If the key or IV is compromised, an attacker can decrypt all the data encrypted with that key and IV.

To mitigate this vulnerability, you should use an authenticated encryption mode such as AES-GCM, and the key and IV should be generated dynamically for each encryption process.


  • Identify all instances where the insecure encryption algorithm is used.
  • Replace the insecure encryption algorithm with a secure one. In this case, the AES-256-CBC cipher is already a secure choice.
  • Ensure that the key used for encryption is securely generated and stored. It should not be hardcoded as in the example.
  • Ensure that the initialization vector (IV) is randomly generated for each session. Do not derive it from the key as in the example.
  • Ensure that the application does not allow connections with anonymous cipher suites. This can be done by configuring the server to only accept connections with named cipher suites.
  • Test the application to ensure that the changes have not introduced any new issues.

Compliant code

// Use openssl_random_pseudo_bytes to generate a random key
$key = bin2hex(openssl_random_pseudo_bytes(16));

// Use openssl_random_pseudo_bytes to generate a random IV
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('AES-256-CBC'));

$cipher = 'AES-256-CBC';
$encrypted = openssl_encrypt($data, $cipher, $key, 0, $iv);

The updated code now uses the openssl_random_pseudo_bytes function to generate a random key and initialization vector (IV) for each session. This is a more secure method than the previous approach, which used a hardcoded key and derived the IV from the key.

The openssl_random_pseudo_bytes function generates a pseudo-random string of bytes. This is suitable for creating a random key and IV, as it is highly unlikely to produce the same output twice. The bin2hex function is used to convert the binary output of openssl_random_pseudo_bytes into a hexadecimal string, which is easier to work with.

The cipher used remains 'AES-256-CBC', which is a secure choice. However, the application should be configured to only accept connections with named cipher suites, to prevent connections with anonymous cipher suites. This can be done in the server configuration, and is not shown in the code snippet.

After making these changes, it is important to test the application thoroughly to ensure that no new issues have been introduced.