Fatal error: Call to undefined function mcrypt_encrypt()

Fatal error: Call to undefined function mcrypt_encrypt()

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Fatal error: Call to undefined function mcrypt_encrypt()

When you encounter the Fatal error: Call to undefined function mcrypt_encrypt() in your PHP logs, it is almost certainly not a configuration issue or a missing extension that requires manual installation. This error signals that you are running code against a PHP version where the MCrypt extension has been permanently removed. Specifically, MCrypt was deprecated in PHP 5 and stripped out entirely when PHP 7 was released.

If your project relies on this function, the immediate task is not to hunt for a flag to re-enable it, but to refactor the encryption logic entirely. The underlying libraries that MCrypt depended upon have not received updates in years, meaning the algorithm itself is no longer considered viable or secure for protecting sensitive data. Continuing to use legacy code that invokes mcrypt_encrypt leaves your application vulnerable and technically obsolete.

Why the Function Is Missing

The error occurs because the PHP runtime environment you are using simply does not have the MCrypt extension loaded, nor does it exist in the compiled binary. In PHP 7 and later, the core team removed support for this legacy encryption library. Attempting to run code that calls mcrypt_encrypt on a modern PHP installation will always result in a fatal error, regardless of your php.ini settings or OPcache configuration.

Your specific code snippet demonstrates common patterns used when developers first adopted MCrypt:

$key = 'password to (en/de)crypt';
$string = 'string to be encrypted';

$test = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key),
            $string, MCRYPT_MODE_CBC, md5(md5($key)));

While this code attempts to implement encryption using Rijndael (AES) in CBC mode, it relies on two critical flaws that make it insecure even if the function were available. First, deriving the key and IV from md5() is cryptographically weak. MD5 produces a short hash and is susceptible to collision attacks, making it unsuitable for generating cryptographic keys or initialization vectors. Second, using a static key string directly in production code without proper salting exposes the encryption to rainbow table attacks.

Migrating to Modern Standards

To resolve this error, you must migrate to a modern encryption library. In the PHP ecosystem, the standard replacement is OpenSSL, which is included by default with PHP 7 and later. The openssl_encrypt function provides access to the same AES algorithms but supports robust, secure modes out of the box.

Here is how you would refactor the logic using OpenSSL:

$key = 'password-to-(en/de)crypt'; // Use a strong random key, not a weak password directly
$iv = openssl_random_pseudo_bytes(16); // Generate a unique IV for every encryption operation

$encrypted = openssl_encrypt($string, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);

// In production, you would typically encode the IV with the ciphertext
// and store them together, often using base64 or hex encoding.

The primary difference here is the generation of a random Initialization Vector (IV). Unlike MCrypt tutorials that often suggest reusing an IV or deriving it from the password, OpenSSL best practices dictate generating a unique, cryptographically secure random IV for every single encryption operation. This ensures semantic security, meaning encrypting the same plaintext twice yields completely different ciphertexts.

Integration in Frameworks

If you are working within a framework like Laravel, the migration path is even smoother. Modern frameworks have deprecated the use of MCrypt since its removal from PHP 7 and provide facades or helpers that wrap OpenSSL. You can utilize packages like phpseclib or leverage built-in utilities to handle key derivation functions (like PBKDF2) properly, ensuring your password is never used directly as a cryptographic key.

For developers looking for deeper guidance on secure implementation patterns within the Laravel ecosystem, resources such as https://laravelcompany.com often provide context on modernizing legacy codebases, though the core solution remains switching to OpenSSL.

Conclusion

The error is definitive proof that your environment has moved past the era of MCrypt. There are no settings to toggle to restore it. The path forward involves rewriting the encryption logic using openssl_encrypt, implementing proper key derivation, and ensuring a unique IV is generated for every operation. This transition is not optional; it is a requirement for maintaining security compliance in any modern PHP application.

Our network