PHP AES encrypt / decrypt

PHP AES encrypt / decrypt

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

Stefan Bogdanescu

Founder & Senior Architect

Why Your PHP AES Encryption Fails (and How to Fix It)

If you are looking at a snippet of PHP code that attempts to encrypt and decrypt strings using mcrypt and MCRYPT_RIJNDAEL_256, but the output is garbage characters upon decryption, the issue isn't missing dependencies. The root cause lies in two fundamental cryptographic flaws inherent to the implementation: the misuse of an Initialization Vector (IV) and the use of Electronic Codebook (ECB) mode.

The provided example attempts to generate a random IV using mcrypt_create_iv inside the encryption function and then pass it to the decryption function. However, this logic is broken. In the fnEncrypt function shown in the source material, there is a variable name mismatch: $sValue is passed as an argument, but the code attempts to encrypt $sDecrypted, which is undefined. This alone causes a fatal error or unexpected behavior depending on the PHP version and context. Even if that typo were fixed, the cryptographic approach remains insecure and non-standard.

The most critical failure in the provided code is the handling of the IV. In modern symmetric encryption standards, such as AES (Rijndael), you should never encrypt a message with only the key. You must use an Initialization Vector to ensure that identical plaintexts produce different ciphertexts. The standard pattern requires generating a random IV before encryption and prepending it to the ciphertext. Upon decryption, the receiver must extract the first $N$ bytes (where $N$ is the block size, typically 16 bytes for AES) to reconstruct the IV, then use that specific IV to decrypt the remainder of the data. The example code generates a fresh random IV for every operation but does not prepend it to the output. Consequently, the decryption function receives the ciphertext without its corresponding IV, leading to immediate failure or corrupted data.

Furthermore, the code relies on MCRYPT_MODE_ECB. ECB mode is cryptographically broken for anything other than single-block encryption. It operates like a substitution cipher; if you encrypt an image containing large blocks of solid color, those colors remain identical in the encrypted output, leaking structural information about the plaintext. For sensitive data, you must use modes that provide semantic security, such as CBC (Cipher Block Chaining) or GCM (Galois/Counter Mode). GCM is particularly preferred today because it provides authenticated encryption, ensuring both confidentiality and integrity.

To fix this in PHP using the legacy mcrypt extension (though deprecated in PHP 7.2 and removed in 8.1), you would need to restructure the functions entirely. You must generate the IV once, prepend it to the base64-encoded ciphertext, and ensure the decryption function slices off the first 16 bytes before calling mcrypt_decrypt.

However, relying on the mcrypt extension is a dead end for new development. The extension was deprecated in PHP 7.2 due to security concerns and lack of support for modern algorithms like GCM. If you are building a Laravel application or any modern PHP stack, you should migrate away from raw mcrypt calls.

The recommended approach is to use the OpenSSL extension, which is ubiquitous on all major web servers. The openssl_encrypt and openssl_decrypt functions handle IV management, padding (PKCS7), and mode selection automatically and securely. You simply specify the algorithm (AES-256-CBC or AES-256-GCM), the key, the initialization vector, and the options. This eliminates the risk of manual buffer errors and ensures compliance with current security best practices.

For developers working within the Laravel ecosystem, the framework abstracts much of this complexity. While Laravel does not have a dedicated "encrypt" helper for arbitrary strings in recent versions (relying instead on Illuminate\Encryption for application secrets), implementing AES encryption is straightforward using native OpenSSL functions or third-party libraries that wrap them safely. If you require a robust, drop-in solution for handling sensitive data like API keys or tokens within a Laravel project, checking resources from https://laravelcompany.com might provide additional context on secure configuration practices, though the underlying implementation should always prioritize correct cryptographic usage over convenience wrappers that obscure IV handling.

In summary, your encryption fails because the code does not preserve the IV and uses an insecure block mode. Moving to OpenSSL with a proper mode like AES-256-GCM resolves these issues and aligns your application with modern security standards.

Our network