Article

Replacing mcrypt_encrypt()

Remove mcrypt_encrypt() safely: lifecycle, modern replacements, and migration examples for legacy PHP apps.

mcrypt_encrypt() encrypted data with algorithms/modes from libmcrypt. Padding behavior and mode defaults differ from modern OpenSSL/libsodium APIs, so ciphertext compatibility must be verified—not assumed.

Lifecycle (php.net)

  • Deprecated: ext/mcrypt deprecated as of PHP 7.1.0
  • Removed: PHP 7.2.0

Modern replacement

Prefer libsodium (sodium_crypto_secretbox / AEAD) for new data. For historical ciphertext, decrypt with a controlled transitional tool, then re-encrypt. OpenSSL equivalents require explicit attention to key length, IV, and PKCS#7 padding.

Migration example

# LEGACY — mcrypt_encrypt()
$cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plain, MCRYPT_MODE_CBC, $iv);

# MODERN
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$cipher = sodium_crypto_secretbox($plain, $nonce, $key);
# store $nonce alongside ciphertext

Security note: treat key handling and IV/nonce uniqueness as part of the migration design review—not an afterthought.

Scan before cutover

Search the tree for mcrypt_encrypt() and paste samples into the PHP Version Compatibility Checker and PHP Deprecated Checker.

Related tools

Related reading