liveBook · Manning

RC4

As explained in Hacking Cryptography

RC4 is a stream cipher that generates a keystream from a variable-length secret key using a permutation-based state algorithm.

RC4 is a byte-oriented stream cipher: it expands a secret key into a keystream, then XORs each keystream byte with plaintext to produce ciphertext. Its appeal was simple software and hardware implementation, but its use in WEP made Wi-Fi preshared keys recoverable from captured traffic under particular conditions. section 4.3 section 4.4

The cipher keeps a 256-byte S-box containing a permutation of 0 through 255, plus moving indices i and j. Initialization fills the S-box with its indices; later swaps preserve every byte while changing their positions. The state can be pictured as a box of numbered rocks: shaking changes the order, not the collection. section 4.3.1

Figure 4.10 RC4 internal state: a 256-byte S-box and two pointers, i and j
figure

Inspect the two pieces of mutable RC4 state: the 256-byte permutation and the indices that traverse it. section 4.3.1

RC4 runs in two phases:

  • Key-scheduling algorithm (KSA): initialize S[0..255], then for each index update j with the current state byte and a repeating key byte, and swap S[i] with S[j]. KSA runs once for a new key. section 4.3.1
  • Pseudo-random generation algorithm (PRGA): repeatedly advance i, update j from S[i], swap the selected entries, and select one S-box byte as output. Each output byte joins the keystream. section 4.3.1
  • Encryption: run KSA, generate as many keystream bytes as the plaintext requires, and XOR corresponding bytes. The same keystream relationship makes known plaintext useful to an attacker. section 4.3.1 section 4.1.2

WEP and the FMS attack

WEP formed each packet’s RC4 key by concatenating a public three-byte initialization vector (IV) with a shared Wi-Fi password. For weak IV patterns, RC4’s key scheduling produces statistical biases. An attacker who captures many such encrypted packets can XOR a known packet header with ciphertext to obtain a keystream byte, simulate the relevant KSA steps using already guessed key bytes, vote on the next password byte, and repeat. A Go demonstration recovers test keys with 50,000 messages, while 500 messages can produce wrong byte guesses. section 4.3.2 section 4.3.2

The failure is not merely reuse of one visible packet key: WEP intended each public IV to create a different RC4 key. Instead, weak-IV bias leaks information about the appended preshared key across enough observations. Discarding an initial portion of RC4 output is described as a mitigation for problematic early-byte indices, but the demonstrated WEP construction remains an insecure deployment. section 4.3.2 section 10.1.2

How Hacking Cryptography covers it

How deep it goes
Works through KSA, PRGA, XOR encryption, and a byte-by-byte statistical FMS recovery procedure.
Examples
Demonstrates Go RC4 encryption and FMS recovery of “helloworld123” from 50,000 weak-IV packets.
What it assumes
Assumes familiarity with XOR and byte arrays while explaining the S-box, KSA, and PRGA operationally.
What you can do after
Build it from scratch in Go and evaluate why weak-IV packet volume enables WEP key recovery.
How it connects
Relates RC4 to stream ciphers, XOR, keystreams, WEP, initialization vectors, preshared keys, and statistical bias.

Also explained in

Compare how 2 books explain RC4 →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.