A byte-oriented keystream generator built around a permutation of the 256 byte values , rather than an LFSR. Designed by Ron Rivest in 1987.
Generating the keystream has 2 phases: the Key Scheduling Algorithm builds an initial permutation from the key, and the Pseudo-Random Generation Algorithm repeatedly mutates it to emit 1 output byte per step.
Key Space
For a fixed key length of bytes, RC4 accepts input keys. RC4 permits to bytes, so its full input key space has members. The key scheduling algorithm maps these inputs into at most internal permutations, so distinct input keys can produce the same state.
Key Scheduling Algorithm
Initialises to the identity permutation, then scrambles it using the secret key (1 to 256 bytes), reused cyclically if shorter than 256 bytes.
- iterates once over every index in order.
-
a running value seeded by the key, deciding which entry is swapped with.
After 256 swaps, every byte value has moved to a key-dependent position, and passes unchanged into the PRGA.
Pseudo-Random Generation Algorithm
Produces the keystream 1 byte at a time, advancing further at every step.
is the output byte, XORed with the corresponding plaintext byte exactly as in any other stream cipher.
Each output byte depends on the entire current permutation rather than a fixed linear recurrence, so RC4 has none of the structure LFSR-based generators rely on, at the cost of a much larger internal state ( possible permutations) than the key itself.
Security Analysis
RC4 was used in SSL/TLS, WEP, and WPA. It is now broken or deprecated in each protocol.
- Early-byte bias
The 2nd keystream byte is with probability close to . A uniform byte has probability . The early output is distinguishable from random. - Related-key weakness
WEP prefixes the long-term key with a public 24-bit IV. Certain IVs make the first KSA swaps expose 1 key byte through the first PRGA byte. The Fluhrer-Mantin-Shamir attack collects enough such packets to recover the long-term key. - Keystream bias
Later PRGA output also has statistical biases. Mantin’s ABSAB bias can reveal a plaintext sent under many unrelated keys. The attack uses byte frequencies across sessions. It does not recover a key.
Discarding the first 256 or more bytes removes some early-output biases. Keeping a public IV separate from the key prevents the WEP-style related-key attack. Neither change makes RC4 safe for new systems. Modern protocols use authenticated ciphers such as AES-GCM or ChaCha20-Poly1305.