laravel/framework · error · EncryptException
Could not encrypt the data.
Error message
Could not encrypt the data.
What it means
Encrypter.encrypt() throws EncryptException('Could not encrypt the data.') when openssl_encrypt() returns false. OpenSSL returns false on internal failure — typically an unsupported algorithm in the linked OpenSSL build, an invalid IV length, or a key/algorithm mismatch that bypassed the constructor's supported() check. This is distinct from the JSON-encode failure at line 127.
Source
Thrown at src/Illuminate/Encryption/Encrypter.php:114
* Encrypt the given value.
*
* @param mixed $value
* @param bool $serialize
* @return string
*
* @throws \Illuminate\Contracts\Encryption\EncryptException
*/
public function encrypt(#[\SensitiveParameter] $value, $serialize = true)
{
$iv = random_bytes(openssl_cipher_iv_length(strtolower($this->cipher)));
$value = \openssl_encrypt(
$serialize ? serialize($value) : $value,
strtolower($this->cipher), $this->key, 0, $iv, $tag
);
if ($value === false) {
throw new EncryptException('Could not encrypt the data.');
}
$iv = base64_encode($iv);
$tag = base64_encode($tag ?? '');
$mac = self::$supportedCiphers[strtolower($this->cipher)]['aead']
? '' // For AEAD-algorithms, the tag / MAC is returned by openssl_encrypt...
: $this->hash($iv, $value, $this->key);
$json = json_encode(['iv' => $iv, 'value' => $value, 'mac' => $mac, 'tag' => $tag], JSON_UNESCAPED_SLASHES);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new EncryptException('Could not encrypt the data.');
}
return base64_encode($json);
}
View on GitHub (pinned to bd6b5437e6)
Solutions
- Verify the cipher is available: in_array('aes-256-gcm', openssl_get_cipher_methods(true)).
- Install/enable the full openssl extension and a complete OpenSSL build (e.g. swap alpine for debian image, or apk add openssl).
- Fall back to a CBC cipher that your OpenSSL supports and regenerate APP_KEY at the matching length.
- Confirm PHP's openssl extension is loaded: php -m | grep openssl.
Example fix
// before — cipher not available in OpenSSL
// config/app.php 'cipher' => 'aes-256-gcm'
// diagnostic
var_dump(in_array(strtolower('aes-256-gcm'), openssl_get_cipher_methods(true)));
// false => OpenSSL lacks GCM
// after — pick a supported cipher and matching key length
// config/app.php 'cipher' => 'aes-256-cbc'
// regenerate key: php artisan key:generate Defensive patterns
Strategy: validation
Validate before calling
$cipher = strtolower(config('app.cipher'));
if (! in_array($cipher, openssl_get_cipher_methods(true), true)) {
throw new \RuntimeException("OpenSSL lacks cipher {$cipher}; install/enable full openssl");
} Type guard
function cipherAvailable(string $cipher): bool
{
return in_array(strtolower($cipher), openssl_get_cipher_methods(true), true);
} Try / catch
try {
$encrypted = encrypt($value);
} catch (\Illuminate\Contracts\Encryption\EncryptException $e) {
// likely OpenSSL lacks the cipher; check openssl_get_cipher_methods()
throw $e;
} Prevention
- Verify openssl_get_cipher_methods() includes your cipher before deploy.
- Use full OpenSSL builds in Docker images (avoid stripped alpine openssl).
- Confirm `php -m` lists openssl on every environment.
When it happens
Trigger: Calling encrypt($value) where the cipher string (e.g. 'aes-256-gcm') is not actually compiled into the host's OpenSSL — common on minimal/old PHP images or when the cipher was set to a variant OpenSSL doesn't expose. Also possible after tampering with $this->cipher or passing an IV shorter than openssl_cipher_iv_length() expects.
Common situations: PHP built against a stripped OpenSSL without GCM support; Alpine/slim Docker images missing openssl CA/algos; switching cipher to aes-256-gcm on a host whose OpenSSL lacks AEAD; custom Encrypter subclasses overriding the cipher; openssl extension disabled.
Related errors
- Unsupported cipher or incorrect key length. Supported cipher
- Could not decrypt the data.
- Unable to use tag because the cipher algorithm does not supp
- Callback must be a callable, callback array, or a 'Class@met
- Auth guard [{$name}] is not defined.
AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06).
Data as JSON: /data/errors/6f513e377055710d.json.
Report an issue: GitHub.