← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

mcrypt Deprecation — Migrate to OpenSSL

PHP PHP 5.0+ Intermediate
debt(d5/e5/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list rector and phpcs, both specialist/static-analysis tools that can flag mcrypt_encrypt|mcrypt_decrypt patterns. It won't be caught by a plain compiler error (d1) or a default linter out of the box, but these tools automate detection when configured.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix says to replace mcrypt with libsodium or AES-256-GCM, but common_mistakes reveal that a naive one-for-one swap (mcrypt to AES-CBC) is still insecure. A correct migration requires auditing all encryption call sites, changing key/nonce management, and potentially adding HMAC authentication — touching multiple files and requiring careful cryptographic decisions, not just a single-line swap.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The applies_to scope covers both web and cli contexts but is bounded to code that actually uses mcrypt. Once the migration is complete the burden is lifted entirely; it does not persist as an ongoing structural tax on the codebase. The impact is real during migration but does not shape every future change.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception field explicitly states that AES-CBC without authentication is vulnerable to padding oracle attacks, yet CBC is the natural migration target for developers moving from mcrypt. This contradicts the reasonable expectation that 'just use OpenSSL with the same mode' is safe. The common_mistakes confirm developers routinely make this mistake, making it a serious cryptographic trap that contradicts intuition from similar contexts.

About DEBT scoring →

TL;DR

mcrypt was deprecated in PHP 7.1 and removed in PHP 7.2 — migrate all encryption to OpenSSL (openssl_encrypt) or libsodium (sodium_crypto_secretbox).

Explanation

mcrypt was PHP's encryption extension for decades — but it was unmaintained since 2007, used outdated algorithms, and had a confusing API. PHP 7.1 deprecated it, PHP 7.2 removed it. Migration: AES-256-CBC via openssl_encrypt($data, 'aes-256-cbc', $key, 0, $iv). The correct modern option: libsodium (PHP 7.2+) with sodium_crypto_secretbox() — authenticated encryption preventing tampering. Key management: never hardcode keys, use environment variables or a secrets manager. Always use authenticated encryption (AES-GCM or libsodium) — unauthenticated AES-CBC can be attacked via padding oracle.

Common Misconception

AES-CBC is secure for all use cases — CBC without authentication (HMAC or AEAD) is vulnerable to padding oracle attacks. Use AES-GCM or libsodium instead.

Why It Matters

Any code using mcrypt fails on PHP 7.2+ and was likely using insecure encryption modes. Migration is mandatory and is an opportunity to fix the cryptography.

Common Mistakes

  • Migrating from mcrypt to AES-CBC without adding authentication (HMAC) — still vulnerable.
  • Not using sodium_crypto_secretbox() which handles key+nonce management correctly.
  • Hardcoding encryption keys in source code.

Code Examples

✗ Vulnerable
// mcrypt — removed in PHP 7.2:
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC);
✓ Fixed
// Modern libsodium (PHP 7.2+) — authenticated encryption:
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$key = sodium_crypto_secretbox_keygen();
$encrypted = sodium_crypto_secretbox($data, $nonce, $key);

// Decrypt:
$decrypted = sodium_crypto_secretbox_open($encrypted, $nonce, $key);

Added 23 Mar 2026
Views 30
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 7 Unknown AI 3 Google 3 Ahrefs 3 Meta AI 2 Perplexity 2 ChatGPT 1 Claude 1 Scrapy 1 Majestic 1
crawler 21 crawler_json 2 pre-tracking 1
DEV INTEL Tools & Severity
🔴 Critical ⚙ Fix effort: Medium
⚡ Quick Fix
Replace mcrypt with libsodium (sodium_crypto_secretbox). Use AES-256-GCM via openssl if libsodium unavailable. Never use CBC without HMAC authentication.
📦 Applies To
PHP 5.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
mcrypt_encrypt|mcrypt_decrypt
Auto-detectable: ✓ Yes rector phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✗ Manual fix Fix: High Context: Function Tests: Update
CWE-327 CWE-326 CWE-311


✓ schema.org compliant