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

  1. Verify the cipher is available: in_array('aes-256-gcm', openssl_get_cipher_methods(true)).
  2. Install/enable the full openssl extension and a complete OpenSSL build (e.g. swap alpine for debian image, or apk add openssl).
  3. Fall back to a CBC cipher that your OpenSSL supports and regenerate APP_KEY at the matching length.
  4. 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

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


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/6f513e377055710d.json. Report an issue: GitHub.