/**
* Sets the AES encryption mode.
*
* WARNING: The strength is deprecated as it has a different effect in MCrypt and OpenSSL. MCrypt was abandoned in
* 2003 before the Rijndael-128 algorithm was officially the Advanced Encryption Standard (AES). MCrypt also offered
* Rijndael-192 and Rijndael-256 algorithms with different block sizes. These are NOT used in AES. OpenSSL, however,
* implements AES correctly. It always uses a 128-bit (16 byte) block. The 192 and 256 bit strengths refer to the
* key size, not the block size. Therefore using different strengths in MCrypt and OpenSSL will result in different
* and incompatible ciphertexts.
*
* TL;DR: Always use $strength = 128!
*
* @param string $mode Choose between CBC (recommended) or ECB
* @param int $strength Bit strength of the key (128, 192 or 256 bits). DEPRECATED. READ NOTES ABOVE.
*
* @return mixed
*/
public function setEncryptionMode($mode = 'cbc', $strength = 128)
{
static $availableAlgorithms = null;
static $defaultAlgo = 'aes-128-cbc';
if (!\is_array($availableAlgorithms)) {
$availableAlgorithms = openssl_get_cipher_methods();
foreach (array('aes-256-cbc', 'aes-256-ecb', 'aes-192-cbc', 'aes-192-ecb', 'aes-128-cbc', 'aes-128-ecb') as $algo) {
if (\in_array($algo, $availableAlgorithms)) {
$defaultAlgo = $algo;
break;
}
}
}
$strength = (int) $strength;
$mode = strtolower($mode);
if (!\in_array($strength, array(128, 192, 256))) {
$strength = 256;
}
if (!\in_array($mode, array('cbc', 'ebc'))) {
$mode = 'cbc';
}
$algo = 'aes-' . $strength . '-' . $mode;
if (!\in_array($algo, $availableAlgorithms)) {
$algo = $defaultAlgo;
}
$this->method = $algo;
}