Stream ciphers
A stream cipher turns a short key into a long run of random-looking bytes and XORs it with the message. Watch HELLO get encrypted, and see why a nonce must never repeat.
In 30 seconds
A stream cipher stretches a short secret key into a long keystream of random-looking bytes, then combines it with the message one byte at a time using XOR. Doing the same XOR again decrypts.
Each message also uses a nonce, a public number that must never repeat with the same key. Reuse it and two messages share a keystream, which lets an eavesdropper cancel it out.
ChaCha20 is the stream cipher in everyday use, alongside AES, a block cipher.
Stream ciphers, step by step
The keystream bytes are real ChaCha20 output for the test key in RFC 8439. A stream cipher alone hides a message but does not detect changes to it: in practice it is paired with Poly1305.
- Step 1: A short key, a long stream. The simplest strong cipher would pair every byte of the message with its own secret random byte. But then the key must be as long as the message. A stream cipher instead grows a long run of random-looking bytes, the keystream, from one short key.
- Step 2: Generating the keystream. ChaCha20 makes keystream in 64-byte blocks. Each block is a scramble of three inputs: the secret key, a nonce (a number used once, sent openly with the message) and a counter that goes up by one per block. Change any input and the block changes completely.
- Step 3: XOR with the plaintext. Each message byte is combined with one keystream byte using XOR: compare the two bit by bit, write 0 where they agree and 1 where they differ. A 1 in the keystream flips the message bit, a 0 leaves it alone. Since the keystream looks random, so does the result.
- Step 4: XOR again to decrypt. XOR undoes itself: flipping the same bits twice restores the original. The receiver, who holds the same key and reads the nonce sent with the message, regenerates the identical keystream and XORs it with the ciphertext. Encryption and decryption are the same operation.
- Step 5: Never reuse key and nonce. Encrypt two messages with the same key and nonce and both get the same keystream. Anyone who XORs the two ciphertexts makes that keystream cancel, leaving the XOR of the two messages, with no key needed. Guess a word in one and the other falls out. So every message needs a fresh nonce.
- Step 6: Inside ChaCha20. ChaCha20, designed by Daniel J. Bernstein in 2008, is the most widely used dedicated stream cipher today. It lays a fixed constant, the key, the counter and the nonce in a 4 × 4 grid of 32-bit words, mixes it for 20 rounds using only addition, bit rotation and XOR, then adds the starting grid back. Out comes one 64-byte block.
- Step 7: Stream versus block. A block cipher such as AES scrambles fixed 16-byte blocks and needs a mode of operation to handle real messages. The popular modes, CTR and GCM, simply encrypt a counter to make keystream, so they behave as stream ciphers too. Either kind also needs an integrity check: flipping a ciphertext bit flips the same plaintext bit.
Try it yourself
Learn this first
- Symmetric encryptionOne shared key locks and unlocks a message; the hard part is sharing the key. Beginner
- Binary numbersCounting with only 0 and 1, and why that suits a machine built from switches. Beginner
- Logic gates (AND, OR, NOT, XOR)NOT, AND, OR and XOR as truth tables, and how NAND gates alone can build them all. Beginner
Spotted a mistake on this page?