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

Symmetric Encryption

cryptography PHP 7.2+ Advanced

Also Known As

AES AES-256-GCM ChaCha20 AEAD

TL;DR

Encryption where the same key encrypts and decrypts data — fast and suitable for bulk data, but key distribution is the primary challenge.

Explanation

Symmetric algorithms (AES, ChaCha20) use one key for both operations. AES-256-GCM is the current gold standard: 256-bit key, Galois/Counter Mode provides both encryption and authentication (AEAD). A unique random IV (nonce) must be used for every encryption with the same key. ChaCha20-Poly1305 is preferred on systems without AES hardware acceleration. The key distribution problem — securely sharing the key — is solved using asymmetric encryption or key exchange protocols.

Common Misconception

AES-256 in any mode is secure — AES-256-ECB (Electronic Codebook) encrypts identical blocks identically, leaking data patterns; always use GCM or another authenticated mode.

Why It Matters

Using the wrong cipher mode (ECB) or reusing an IV turns strong AES into weak or broken encryption — implementation choices matter as much as key length.

Common Mistakes

  • Using ECB mode — identical plaintext blocks produce identical ciphertext, revealing patterns.
  • Reusing the same IV with the same key — completely breaks GCM security.
  • Not using authenticated encryption (GCM) — unauthenticated ciphertext can be silently tampered with.
  • Storing the IV separately from the ciphertext in an unreliable way — prepend IV to ciphertext for simple management.

Code Examples

✗ Vulnerable
// AES-256-ECB — insecure mode, no authentication:
$encrypted = openssl_encrypt($data, 'AES-256-ECB', $key);
// Identical 16-byte blocks encrypt identically — patterns visible
// No IV — deterministic, dictionary-attackable
// No authentication — tampering undetectable
✓ Fixed
// AES-256-GCM — authenticated, random IV:
function encrypt(string $plaintext, string $key): string {
    $iv = random_bytes(12); // GCM standard IV size
    $tag = '';
    $ciphertext = openssl_encrypt(
        $plaintext, 'AES-256-GCM', $key,
        OPENSSL_RAW_DATA, $iv, $tag
    );
    return base64_encode($iv . $tag . $ciphertext); // IV + auth tag + ciphertext
}

function decrypt(string $encoded, string $key): string {
    $data = base64_decode($encoded);
    $iv = substr($data, 0, 12);
    $tag = substr($data, 12, 16);
    $ciphertext = substr($data, 28);
    return openssl_decrypt($ciphertext, 'AES-256-GCM', $key, OPENSSL_RAW_DATA, $iv, $tag);
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 30
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
2 pings W 1 ping T 3 pings 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 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 7 Unknown AI 2 Google 2 Ahrefs 2
crawler 20 crawler_json 1
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Medium
⚡ Quick Fix
Use PHP's Sodium extension (sodium_crypto_secretbox) for authenticated symmetric encryption — it's simpler and safer than OpenSSL's AES-GCM because it handles nonce generation and authentication automatically
📦 Applies To
PHP 7.2+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
openssl_encrypt without verifying the authentication tag; reusing nonces; encryption without authentication (no GCM tag)
Auto-detectable: ✓ Yes semgrep psalm
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update
CWE-327 CWE-326

✓ schema.org compliant