Stream & Block Ciphers, One Gate at a Time

Every symmetric cipher scrambles data with a key. The difference between a stream cipher and a block cipher is really a difference in how much data gets combined with the key at once — and the XOR gate is where that combination actually happens.

CIT 2640 — Network Security · Module 2: Cryptography Concepts · Offline lecture demo

Two Familiar Gates First

Before XOR, a quick refresher on the two basic logic gates it's usually taught alongside. Both take two bits and produce one — the difference is in which combinations of inputs produce a 1.

AND Gate both must be 1

Output is 1 only when A and B are both 1.

ABA AND B
000
010
100
111

OR Gate either can be 1

Output is 1 when A or B (or both) is 1.

ABA OR B
000
011
101
111

The XOR Gate

XOR ("exclusive or") outputs 1 when its two inputs differ, and 0 when they match. That single rule is the engine behind most symmetric ciphers: it's cheap to compute, and — critically — applying the same key bits a second time undoes the first pass.

Input A 0
Input B 0
ABA XOR B
000
011
101
110
0
A XOR B

Two Ways to Encrypt a Message

Both approaches use a secret key to turn plaintext into ciphertext. They differ in whether they process the message continuously or in fixed-size chunks. Try both panels below.

Stream Cipher bit by bit

A keystream generator produces an endless run of pseudorandom bits from the key. Each bit of plaintext is combined with the matching keystream bit — usually with XOR — the instant it arrives.

Plaintext
Keystream
XOR output
Click any keystream bit to flip it — the output updates instantly, one position at a time.
  • Examples: RC4, ChaCha20, Salsa20
  • Good for: real-time data — voice, video, network links

Block Cipher fixed chunks

The message is split into fixed-size blocks. Each full block is transformed through several rounds of substitution and permutation, keyed by the secret. Short final blocks get padded.

  • Examples: AES, DES, Blowfish
  • Good for: files, storage, anything not streaming live
Where they meet: a block cipher run in CTR mode uses the block cipher only to generate a keystream from a counter — then XORs that keystream with the plaintext, exactly like a stream cipher. The XOR step above is the same operation either way.

Sample Test: XOR as a Stream Cipher

Repeat the key to match the length of the message, XOR each character byte-for-byte, and the result is your ciphertext. Run the exact same XOR a second time with the same key, and you get the plaintext back — that's what "symmetric" means in practice.

Try changing this and re-running.
Sample key — short on purpose, so the repetition is visible.
Click "Encrypt with XOR" to see the byte-by-byte breakdown.

Ciphertext (hex bytes — mostly unprintable, so hex is how you'd actually transmit this)

—