The RC4 pseudorandom generation algorithm (PRGA) turns the shuffled S-box into a keystream. After key scheduling initializes the permutation, PRGA repeatedly updates the cipher state and emits one byte at a time. section 4.3.1
Each PRGA iteration performs four state and output operations:
- Increment i modulo 256. section 4.3.1
- Update j by adding the current S[i], modulo 256. section 4.3.1
- Swap S[i] and S[j], preserving the S-box as a permutation. section 4.3.1
- Output S[(S[i] + S[j]) mod 256] as the next keystream byte. section 4.3.1
Go implementation
A length-limited implementation repeats the PRGA loop, stores each selected state byte in keyStream, and returns the resulting sequence. RC4 then XORs that sequence with plaintext to produce ciphertext. In WEP, the first keystream byte can be recovered from a known SNAP header and ciphertext byte, enabling statistical key recovery under weak IV conditions. section 4.3.1 section 4.3.2
Figure 4.10 RC4 internal state: a 256-byte S-box and two pointers, i and j
Shows the mutable 256-byte S-box and the i and j pointers that drive each PRGA iteration. section 4.3.1
