{"slug": "why-ecb-mode-leaks-penguins-and-why-aead-aes-gcm-matters-for-api-payloads", "title": "Why ECB Mode Leaks Penguins and Why AEAD (AES-GCM) Matters for API Payloads", "summary": "A developer explains why AES in ECB mode leaks plaintext structure—identical 16-byte blocks produce identical ciphertext, preserving patterns in JSON and images—and why modern APIs should use AEAD ciphers like AES-256-GCM or ChaCha20-Poly1305, which add integrity verification and prevent padding-oracle attacks. The writeup includes a TypeScript Web Crypto example that binds a tenantId to an encrypted payload via additionalData, so a ciphertext moved between tenants fails authentication on decrypt.", "body_md": "If you have ever seen the famous \"ECB penguin\" picture in a cryptography textbook, you already know why block cipher modes matter.\n\nWhen you encrypt data with AES in Electronic Codebook (ECB) mode, identical 16-byte plaintext blocks produce identical 16-byte ciphertext blocks. That means patterns in structured data, JSON keys, or bitmap pixels survive encryption completely intact.\n\nIn 1949, Claude Shannon showed that strong encryption requires diffusion: flipping a single plaintext bit should scramble the entire ciphertext output. ECB violates diffusion across blocks because every 128-bit block is encrypted independently.\n\nEven worse, ECB and older modes like CBC provide confidentiality without built-in integrity. If an attacker flips bits in transit, your server tries to decrypt tampered bytes and can leak information through padding error messages (a padding oracle attack).\n\n`AES-256-GCM`) Solves Both Problems\nModern APIs and TLS 1.3 rely on **Authenticated Encryption with Associated Data (AEAD)** such as `AES-256-GCM` or `ChaCha20-Poly1305`:\n\n`additionalData`) fails verification immediately before decryption returns plaintext.\nHere is how you bind unencrypted routing metadata (like a `tenantId`) to an encrypted payload using `additionalData` in TypeScript Web Crypto:\n\n```\nexport async function encryptWithAad(\n  key: CryptoKey,\n  plaintext: string,\n  tenantId: string,\n): Promise<{ iv: Uint8Array; ciphertext: ArrayBuffer }> {\n  const iv = crypto.getRandomValues(new Uint8Array(12));\n  const encoder = new TextEncoder();\n\n  const ciphertext = await crypto.subtle.encrypt(\n    {\n      name: \"AES-GCM\",\n      iv,\n      additionalData: encoder.encode(tenantId),\n    },\n    key,\n    encoder.encode(plaintext),\n  );\n\n  return { iv, ciphertext };\n}\n```\n\nBecause `tenantId` is passed as `additionalData`, an attacker cannot move a valid encrypted blob from `tenantA` into `tenantB`'s row without triggering an authentication failure during `crypto.subtle.decrypt`.\n\n💡 **Developer Infrastructure Note**: Just as AEAD binds metadata to ciphertext, outbound email pipelines need cryptographically bound headers too. [SadaSend](https://www.sadasend.com/?utm_source=devto&utm_medium=article&utm_campaign=dan_kestrel&utm_content=ecb-vs-aead-aes-gcm) pairs Dual DKIM (RFC 6376) and DMARC alignment with active Port 25 bounce checks and per-key agent spend guards.", "url": "https://wpnews.pro/news/why-ecb-mode-leaks-penguins-and-why-aead-aes-gcm-matters-for-api-payloads", "canonical_source": "https://dev.to/dev_kestrel/why-ecb-mode-leaks-penguins-and-why-aead-aes-gcm-matters-for-api-payloads-1fkk", "published_at": "2026-12-06 06:31:07+00:00", "updated_at": "2026-12-06 06:44:48.422301+00:00", "lang": "en", "topics": ["artificial-intelligence"], "entities": ["Claude Shannon", "SadaSend"], "also_reported_by": [], "takeaway": "Use AES-256-GCM with the tenant ID passed as additionalData so a ciphertext copied into another tenant's row fails authentication instead of decrypting.", "recommendation": {"advice": "Encrypt API payloads with AES-256-GCM and bind routing metadata like tenantId as additionalData so tampered or relocated ciphertext fails verification before decryption.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/why-ecb-mode-leaks-penguins-and-why-aead-aes-gcm-matters-for-api-payloads", "markdown": "https://wpnews.pro/news/why-ecb-mode-leaks-penguins-and-why-aead-aes-gcm-matters-for-api-payloads.md", "text": "https://wpnews.pro/news/why-ecb-mode-leaks-penguins-and-why-aead-aes-gcm-matters-for-api-payloads.txt", "jsonld": "https://wpnews.pro/news/why-ecb-mode-leaks-penguins-and-why-aead-aes-gcm-matters-for-api-payloads.jsonld"}}