RC4

A byte-oriented keystream generator built around a permutation SS of the 256 byte values S[0],…,S[255]S[0], \ldots, S[255], rather than an LFSR. Designed by Ron Rivest in 1987.

S:{0,…,255}→{0,…,255}S: \{0, \ldots, 255\} \to \{0, \ldots, 255\}

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 ll bytes, RC4 accepts 256l=28l256^l = 2^{8l} input keys. RC4 permits 11 to 256256 bytes, so its full input key space has ∑l=1256256l\sum_{l=1}^{256} 256^l members. The key scheduling algorithm maps these inputs into at most 256!256! internal permutations, so distinct input keys can produce the same state.

Key Scheduling Algorithm

Initialises SS to the identity permutation, then scrambles it using the secret key KK (1 to 256 bytes), reused cyclically if shorter than 256 bytes.

S[i]=i,i=0,…,255j=0for i=0 to 255:j=(j+S[i]+K[i mod len(K)]) mod 256swap S[i] and S[j]\begin{aligned} &S[i] = i, \quad i = 0, \ldots, 255 \\ &j = 0 \\ &\text{for } i = 0 \text{ to } 255: \\ &\quad j = (j + S[i] + K[i \bmod \text{len}(K)]) \bmod 256 \\ &\quad \text{swap } S[i] \text{ and } S[j] \end{aligned}
Here:
ii
iterates once over every index in order.
jj

a running value seeded by the key, deciding which entry ii is swapped with.

After 256 swaps, every byte value has moved to a key-dependent position, and SS passes unchanged into the PRGA.

Pseudo-Random Generation Algorithm

Produces the keystream 1 byte at a time, advancing SS further at every step.

i=0,j=0repeat:i=(i+1) mod 256j=(j+S[i]) mod 256swap S[i] and S[j]k=S[(S[i]+S[j]) mod 256]\begin{aligned} &i = 0, \quad j = 0 \\ &\text{repeat:} \\ &\quad i = (i + 1) \bmod 256 \\ &\quad j = (j + S[i]) \bmod 256 \\ &\quad \text{swap } S[i] \text{ and } S[j] \\ &\quad k = S[(S[i] + S[j]) \bmod 256] \end{aligned}

kk 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 (256!256! 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 00 with probability close to 2/2562/256. A uniform byte has probability 1/2561/256. 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.

Written by November 3, 2026 3 min read
Was this helpful?