Symmetric encryption, modes and randomness
Unit 3 of 5 in Cryptography basics.
You will be able to explain why the mode of encryption and the source of randomness matter and why you should use a maintained library.
Lesson
A cipher is not the whole story
AES takes a fixed block of 128 bits and a key and produces a 128-bit block. Real messages are longer than that, so a second decision is needed: how to apply the cipher to many blocks. That decision is called the mode of operation.
NIST SP 800-38A defines five confidentiality modes for a symmetric block cipher: ECB, CBC, CFB, OFB and CTR. They are not interchangeable. The choice changes what an attacker can learn.
Why ECB is the one to avoid
In ECB, the cipher is applied directly and independently to each block of the plaintext. Because blocks are treated independently, the same plaintext block always gives the same ciphertext block, so patterns in the data show through. The lab for this unit shows this with an image whose shape is still visible after encryption. OWASP says plainly that ECB should not be used outside of very specific circumstances.
Use authenticated encryption
Encryption alone hides data but does not prove it was not changed. OWASP advises that where available, authenticated modes should always be used, because they guarantee integrity and authenticity as well as confidentiality. The most commonly used authenticated modes are GCM and CCM. Modes can also require a fresh initialization vector or nonce, and OWASP says these should be handled automatically by a maintained library, which is the point: do not assemble these pieces yourself.
Randomness: keys must be unguessable
A strong cipher with a guessable key is no protection. OWASP warns that ordinary random number generators must not be used for anything security critical, because it is often possible for attackers to guess or predict their output. Cryptographically secure generators are designed to produce much higher quality randomness and are safe for security-sensitive uses. Always take keys, tokens and nonces from the secure source your platform provides.
Key points
- A cipher needs a mode of operation to handle messages longer than one block.
- ECB encrypts blocks independently and leaks patterns; avoid it.
- Prefer authenticated modes such as GCM or CCM, via a maintained library.
- Use a cryptographically secure random source for keys and nonces.
Sources
- NIST FIPS 197: Advanced Encryption Standard (AES)
- NIST SP 800-38A: Recommendation for Block Cipher Modes of Operation
- OWASP Cryptographic Storage Cheat Sheet
Practice in the labs
See a bad mode and a predictable generator fail, so you never reach for either.
- See how encryption fails: ECB leaks, IV reuse, and why you need authenticated encryption: You watch ECB leak an image's shape and see why authenticated encryption exists.
- Predict a Mersenne Twister (MT19937) PRNG from its output — then switch to a CSPRNG and watch the attack die: You predict a common random generator from its output, then switch to a secure one and watch the attack stop.
Knowledge check
- Why does ECB mode leak patterns? Answer: Each block is encrypted independently, so equal plaintext blocks give equal ciphertext blocks. SP 800-38A describes ECB as applying the cipher directly and independently to each block, so repeated blocks are visible in the output.
- Which should you prefer for encrypting stored data? Answer: An authenticated mode such as GCM through a maintained library. OWASP advises authenticated modes (GCM, CCM) where available because they also guarantee integrity and authenticity, and says to rely on a maintained library.
- Where should a key or token come from? Answer: A cryptographically secure random generator. OWASP warns ordinary random number generators can be predicted by attackers and must not be used for security-critical values.
Back to the syllabus or see all classes.