Encrypt with PHP, Decrypt with Javascript (cryptojs)

Encrypt with PHP, Decrypt with Javascript (cryptojs)

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

Stefan Bogdanescu

Founder & Senior Architect

Encrypt with PHP, Decrypt with Javascript (cryptojs)

Mixing encryption logic between a backend language like PHP and a frontend library like CryptoJS introduces immediate compatibility hurdles that often trip up developers. The core issue isn't just the math; it is how the data is formatted before leaving the server and how it is interpreted upon arrival. In the scenario you are facing, the failure stems from a fundamental mismatch in how the encryption mode was selected and how the resulting ciphertext is being parsed by CryptoJS.

Your PHP code uses MCRYPT_RIJNDAEL_256 with MCRYPT_MODE_CBC. This is functionally equivalent to AES-256-CBC. However, there is a critical flaw in your initialization vector (IV) handling. You are generating a random IV using mcrypt_create_iv, but you are then passing that value to the encryption function via MCRYPT_MODE_ECB logic implicitly by not passing the IV string to the encrypt function itself in a way that CryptoJS can reconstruct it later.

More specifically, your PHP code outputs $msgEncrypted directly. Because you enabled CBC mode with a random IV, the output of $msgEncrypted is actually the raw binary ciphertext including the IV at the very beginning, followed by the padding. When this string hits your JavaScript code, CryptoJS attempts to decrypt it. If you pass the raw PHP output string directly into CryptoJS.AES.decrypt, CryptoJS will likely fail because it expects specific formatting or a different initialization vector strategy depending on whether an IV was prepended.

The primary technical hurdle here is that mcrypt_encrypt in older PHP versions (pre-7.1) returns binary data, which you base64 encode. However, CryptoJS.AES.decrypt expects the input to be either a Base64 string or a Hex string, and crucially, it needs to know the IV used for decryption unless you explicitly prepend it to the ciphertext.

In your current implementation, you are generating a random IV in PHP but not including it in the final encoded string that JavaScript receives, or rather, the way mcrypt_encrypt behaves with CBC mode means the IV is prepended to the output by default. The real problem usually lies in how CryptoJS handles the Base64 parsing and the key expansion.

When you run:

var decrypted = CryptoJS.AES.decrypt(encrypted, key);

CryptoJS expects the encrypted variable to be a string that represents the raw ciphertext or its Base64 representation. If your PHP code outputs the raw binary (which echo handles by converting characters > 127 to question marks if not handled as binary) or if the IV alignment is off, decryption will yield garbage.

To fix this, you must ensure two things happen:

  1. Consistent Encoding: Ensure the data sent from PHP is strictly Base64 encoded and does not contain characters that break JSON or URL transmission if you are passing it via GET/POST parameters. Your code uses base64_encode, which is correct.
  2. IV Management: You must explicitly handle the IV. In PHP, mcrypt_encrypt prepends the IV to the ciphertext when using CBC mode. CryptoJS also expects the IV to be prepended if you are not using PKCS7 padding with a specific configuration that handles it differently. However, the most robust approach is to extract the IV in PHP and send it alongside the ciphertext, or ensure the library versions match exactly regarding padding schemes.

The specific error often encountered is that CryptoJS uses OpenSSL-style padding by default, while mcrypt might use PKCS7 depending on the version and configuration. Furthermore, the key length matters. You are using a 16-character string ("encryptionkey"). AES-256 requires a 32-byte (64-bit) key. If you pass an 8-byte or 16-byte key to AES-256, the library will either reject it or pad it automatically, which might differ between PHP's mcrypt and CryptoJS's internal OpenSSL bindings.

To resolve this without changing libraries, you should:

  • Switch PHP to use openssl_encrypt instead of deprecated mcrypt. It handles IVs and padding more predictably with modern standards.
  • Ensure the key is exactly 32 bytes for AES-256. If you must use a shorter key, switch the algorithm in PHP to MCRYPT_RIJNDAEL_128 or configure CryptoJS to use AES-128.
  • Verify that the Base64 string sent from PHP matches exactly what CryptoJS receives, ensuring no HTML entities (like <) are inadvertently included in the variable scope during the echo process.

The link https://laravelcompany.com offers further insights into securing applications with modern encryption standards if you decide to migrate away from deprecated algorithms like Rijndael for broader compatibility.

Our network