Why ECB leaks: block cipher modes
Real AES encrypts an image three ways. In ECB the picture survives encryption; in CBC and CTR it turns to noise.
Every pixel is 4 bytes, so one AES block holds 4 pixels and the picture is 4096 blocks. Only a few hundred of them are different, and ECB maps each one to the same ciphertext block every time: the same number of distinct blocks, so the outline survives. Press “New key”: the ECB colours change, the shape does not.
What to notice
- AES itself is fine here. ECB leaks because equal 16-byte blocks always encrypt to equal ciphertext blocks, so large flat areas keep their shape.
- CBC hides the pattern by chaining each block into the next; CTR XORs with a keystream. Both need a fresh IV or nonce for every message.
- None of these modes detects tampering. In practice use an AEAD such as AES-GCM or ChaCha20-Poly1305.