Symmetric Encryption and Modes

Topics Covered

What Symmetric Encryption Protects

One key, two directions

Block ciphers and stream ciphers

Key sizes are not the problem

Why the mode matters

Confidentiality is not integrity

A worked example

What attackers usually have

Length and metadata leak

Where symmetric encryption appears

Encryption versus access control

Symmetric encryption is the workhorse of practical cryptography. Every TLS connection, every encrypted disk, every encrypted database field and every encrypted backup ends up using a symmetric cipher for the bulk of the data, because symmetric ciphers are fast, well studied and supported in hardware. Public key cryptography, covered in the next lesson, is mostly used to agree on or protect the symmetric keys. This lesson explains what symmetric encryption protects, why the choice of mode matters more than the choice of cipher, how nonces fail, and how to encrypt application data without building a fragile scheme of your own.

One key, two directions

A symmetric cipher uses the same secret key to encrypt and to decrypt. Anyone who holds the key can read the data and can create new ciphertexts that decrypt correctly, so the key is the whole of the security. The cipher itself is public, and its design assumes the attacker knows the algorithm, can see ciphertexts and can often choose some of the plaintexts. A good cipher keeps data confidential under those conditions as long as the key stays secret and is used correctly. Almost every real failure of symmetric encryption comes from the second condition, correct use, rather than from a weakness in the cipher.

Block ciphers and stream ciphers

AES is a block cipher: it transforms a fixed 16 byte block of plaintext into a 16 byte block of ciphertext under a key of 128, 192 or 256 bits. On its own it can encrypt exactly one block, so it is always used inside a mode of operation that says how to handle longer messages. ChaCha20 is a stream cipher: it generates a long pseudorandom keystream from the key and a nonce, and the plaintext is combined with the keystream using XOR. Both are current, trusted choices. AES is fast on processors with AES instructions, which includes nearly all servers, laptops and phones made in the last decade, and ChaCha20 is fast in software on devices without them.

Key sizes are not the problem

A 128 bit key cannot be found by trying every value with any computer that exists or is planned. Even a machine testing a trillion trillion keys a second would need longer than the age of the universe. AES with 256 bit keys adds margin against future advances, including large quantum computers, which would roughly halve effective key strength, and many standards now prefer it for long lived data. In practice, no production system loses data because AES-128 was brute forced. Systems lose data because the key was stored next to the data, because the same nonce was used twice, or because ciphertexts were accepted without checking that they had not been changed.

Why the mode matters

The simplest mode, Electronic Codebook or ECB, encrypts each 16 byte block independently with the same key. Identical plaintext blocks therefore produce identical ciphertext blocks, and patterns in the data survive encryption. The famous demonstration encrypts a bitmap of a penguin with ECB and the penguin remains clearly visible. The same leak appears in databases: encrypting a status column with ECB, or with any deterministic scheme, produces one ciphertext per distinct value, so an observer learns the distribution of values and can often guess them. Libraries that expose ECB by default, or that pick it when no mode is specified, have caused many such leaks.

A one million row status column with six statuses produced only six distinct ciphertexts under ECB. AES-GCM with random nonces produced one million.

Confidentiality is not integrity

Encryption hides data. It does not, by itself, stop an attacker from changing it. In counter mode and in stream ciphers, flipping a bit of the ciphertext flips exactly the same bit of the plaintext after decryption, so an attacker who knows or can guess the format can make precise edits without knowing the key. In CBC mode, changing one ciphertext block garbles that block and flips chosen bits in the next one. Decryption reports no error in either case, and the application receives altered data that it believes came from a trusted source. This is why modern guidance treats unauthenticated encryption as a defect, not as a weaker option.

A worked example

Consider a service that stores a short record such as an order with an amount field, encrypts it with AES in counter mode and later reads it back. An attacker who can modify the stored ciphertext, perhaps through a separate bug that grants write access to the storage bucket, wants to raise the amount. The plaintext character for the digit one is 0x31 and for nine is 0x39, which differ by 0x08. XORing the ciphertext byte at the right position with 0x08 changes the decrypted digit from one to nine. The service decrypts successfully and processes an order for 900 instead of 100. An authenticated mode would have rejected the modified ciphertext outright.

Without authentication, XORing one ciphertext byte with 0x08 turned amount 100 into amount 900 and decryption raised no error. AES-GCM rejected the same change.

What attackers usually have

When reasoning about encryption, assume the attacker can read every ciphertext, can see their lengths and timing, can submit modified ciphertexts to the system and observe how it reacts, and can often cause the system to encrypt data of their choosing, for example by creating an account with a chosen name. Designs that are secure only if the attacker cannot see ciphertexts, or cannot submit them, are fragile, because those capabilities appear through ordinary features and unrelated bugs. Authenticated encryption with unique nonces is designed to remain secure under all of these conditions.

Length and metadata leak

Encryption hides content but not size. A ciphertext is roughly as long as its plaintext, so encrypted messages reveal their approximate length, and sequences of encrypted messages reveal patterns of activity. Researchers have identified pages visited over encrypted connections, words spoken over encrypted voice calls and medical conditions from encrypted traffic to health sites, all from sizes and timing. Most applications accept this leak, but where length itself is sensitive, such as a field that is either a short code or a long diagnosis, padding to fixed sizes is the usual mitigation. Metadata such as who talks to whom, when and how often is not hidden by encrypting payloads at all.

Where symmetric encryption appears

It helps to list where a typical product already relies on symmetric encryption, because each place has its own owner and its own failure modes. TLS protects traffic between browsers and load balancers and between internal services. Managed databases, object storage and block volumes encrypt at rest under provider managed or customer managed keys. Session cookies and password reset tokens are often encrypted or authenticated by the web framework. Backups, exports to partners and files shared through storage links are encrypted by tools or scripts. Mobile apps encrypt local caches. A short inventory of these uses, with who configured each one, is the starting point for every later decision in this lesson.

Encryption versus access control

Encryption is a way of enforcing access control with keys rather than with permissions checked by a server. Its value comes from moving the trust boundary: data can travel through or rest in places that are not trusted, such as networks, providers, backups and replicas, because only holders of the key can read it. When the component that holds the key is the same component that an attacker compromises, encryption adds little, and the protection comes from permissions, isolation and monitoring instead. Asking where the key lives relative to each attacker is the fastest way to tell whether a proposed use of encryption will stop anything.

The cipher is rarely what breaks

AES and ChaCha20 are not the weak point. Symmetric encryption fails through modes that leak patterns, like ECB, through ciphertexts accepted without integrity checks, and through keys and nonces handled badly. Assume attackers see and modify ciphertexts, and remember that length and metadata still leak.