Stream cipher

On this page 7

A stream cipher is a symmetric encryption algorithm that encrypts plaintext by combining it, element by element, with a pseudorandom keystream generated from a secret key and a nonce. In cryptography and cybersecurity, stream ciphers protect network traffic, disk sectors and real-time media because they encrypt data of any length without padding and add very little latency.

The combining step is almost always the bitwise XOR operation, so decryption is identical to encryption: the receiver regenerates the same keystream and XORs it with the ciphertext. The security of the whole construction therefore rests on one property, namely that the keystream is unpredictable and never reused under the same key.

How a stream cipher generates and applies a keystream

A stream cipher works by expanding a short secret into a long sequence of bits that looks random to anyone without the key. The generator takes a key of 128 or 256 bits and a nonce or initialization vector, mixes them into an internal state, and then repeatedly updates that state while emitting output bytes.

Each output byte is XORed with one plaintext byte. Because XOR is its own inverse, the same operation recovers the plaintext. The model behind this is the one-time pad, which offers perfect secrecy when the pad is truly random, as long as the message and used only once. A stream cipher replaces the impractical random pad with a cryptographically secure pseudorandom generator, trading perfect secrecy for computational security.

The table below summarizes the components that appear in practically every modern design.

ComponentRoleTypical size
Secret keyLong-term secret shared by both parties128 or 256 bits
Nonce or IVMakes each keystream unique per message64 to 192 bits
Internal stateEvolving memory of the generator256 to 1,024 bits
CounterSelects the keystream block position32 or 64 bits
Combining functionMerges keystream with plaintextXOR per bit or byte
Core components of a stream cipher

Synchronous, self-synchronizing and counter-based designs

Stream ciphers fall into families that differ in how the keystream depends on previous data. In a synchronous stream cipher, the keystream depends only on the key and nonce, so sender and receiver must stay aligned; a lost byte desynchronizes everything that follows, while a flipped bit corrupts only one plaintext bit.

A self-synchronizing stream cipher derives the keystream from a fixed window of previous ciphertext, so the receiver recovers automatically after a limited number of bits. Modern practice favors counter-based designs, in which a block function processes the key, nonce and a counter, allowing random access to any position and straightforward parallel computation.

FamilyKeystream depends onError behaviorExample
SynchronousKey and nonce onlyBit flips stay local; lost bytes break alignmentRC4, Trivium
Self-synchronizingKey and recent ciphertextRecovers after a fixed delayCFB mode of a block cipher
Counter-basedKey, nonce and counterBit flips stay local; seekableChaCha20, AES-CTR
LFSR-basedShift register statesBit flips stay localA5/1, E0, Grain
Main families of stream cipher construction

Hardware-oriented designs such as Trivium and Grain, selected in the eSTREAM portfolio, use shift registers with nonlinear feedback to reach a very small gate count. Software-oriented designs such as Salsa20 and ChaCha20 use additions, rotations and XORs on 32-bit words, which run fast on ordinary processors without special instructions.

Stream ciphers compared with block ciphers

The difference between a stream cipher and a block cipher is the unit of processing rather than the underlying strength. A block cipher such as AES transforms fixed 128-bit blocks, while a stream cipher produces a keystream of arbitrary length. In practice the boundary is blurred, because modes of operation such as CTR, OFB and GCM turn a block cipher into a stream cipher.

PropertyStream cipherBlock cipher
Processing unitBit, byte or keystream blockFixed block, usually 128 bits
PaddingNot requiredRequired in ECB and CBC modes
Error propagationUsually limited to affected bitsDepends on the mode
Hardware costCan be extremely smallModerate; fast with AES instructions
Main misuse riskKeystream reuseWrong mode or predictable IV
Stream ciphers and block ciphers side by side

On processors with AES hardware instructions, AES-CTR and ChaCha20 reach comparable speeds. On devices without such instructions, ChaCha20 is typically several times faster and also resists cache-timing attacks more naturally, because it uses no lookup tables.

Choosing and deploying a stream cipher

The safe choice today is a modern cipher used inside an authenticated encryption construction. A bare stream cipher provides confidentiality only; an attacker who flips a ciphertext bit flips the same plaintext bit, a property called malleability. Pairing the cipher with a message authentication code, as in ChaCha20-Poly1305 or AES-GCM, detects such tampering.

The following decision table reflects common engineering choices in current protocols.

SituationRecommended choiceReason
Mobile or embedded softwareChaCha20-Poly1305Fast without AES hardware, constant time
Servers with AES instructionsAES-GCMHardware acceleration, broad support
Random nonces at high volumeXChaCha20-Poly1305192-bit nonce makes collisions negligible
Constrained hardwareGrain-128AEAD or TriviumVery low gate count and power
Legacy compatibilityMigrate away from RC4Known keystream biases
Selecting a stream cipher for a given situation

Nonce handling deserves as much attention as the algorithm itself. A 96-bit random nonce is acceptable only up to roughly 2 to the power of 32 messages per key, after which the collision probability becomes uncomfortable; counters or extended nonces avoid that limit. Keys should come from a proper key exchange or key derivation function, never from a password used directly.

Failure modes and historical attacks

Most real-world breaks of stream ciphers come from misuse rather than mathematics. The classic error is two-time pad reuse: if two messages share a keystream, XORing the ciphertexts cancels the keystream and leaves the XOR of the plaintexts, which statistical analysis often unravels completely.

  • WEP in early Wi-Fi combined a short 24-bit IV with RC4, so IVs repeated quickly and related-key weaknesses exposed the key within minutes.
  • RC4 biases in its first output bytes enabled plaintext recovery in TLS, which led to RC4 being prohibited in TLS in 2015.
  • A5/1 in GSM used an undersized internal state that permits practical time-memory trade-off attacks.
  • Missing authentication allows bit-flipping attacks that change amounts, addresses or commands without detection.

A further misconception holds that a longer key alone guarantees safety. A 256-bit key does not compensate for a repeated nonce, a biased generator or the absence of integrity protection, and it does not hide message length, which a stream cipher preserves exactly.

The idea in brief

A stream cipher scrambles a message by mixing it with a long string of secret, random-looking noise. Both sides know a short key, and from that key each side can produce exactly the same noise. The sender mixes the noise into the message; the receiver mixes the same noise in again, and the original message reappears.

Think of it as two people with identical decoder rings that spin out matching patterns. The method is fast and works on data of any size. The one rule that must never be broken is to avoid using the same noise twice, because reusing it lets an eavesdropper peel the messages apart.

A practical scenario

A team building a messaging app for budget Android phones finds that many target devices lack AES hardware instructions. Benchmarks on a low-end handset show AES-GCM in software encrypting at a fraction of the speed of ChaCha20-Poly1305, while also draining more battery during long voice calls.

The team adopts ChaCha20-Poly1305 with 256-bit session keys derived from an elliptic curve key exchange, and uses a 64-bit message counter as part of each nonce so that no nonce ever repeats under one key. Keys are rotated after every 1,000,000 messages or 24 hours, whichever comes first.

After release, encryption accounts for a negligible share of processor time during calls, and a penetration test confirms that tampered packets are rejected by the Poly1305 tag. The outcome is strong, authenticated encryption on inexpensive hardware without noticeable delay for users.