Short answer: RC4 builds a 256-byte permutation from the key, then walks that permutation to emit a keystream byte. The key schedule adds the next key byte into an index and swaps. The generator increments i, adds S[i] into j, swaps, and emits S[(S[i] + S[j]) mod 256]. The key Key and the plaintext Plaintext produce bbf316e8d940af0ad3.
What the problem actually asks
A stream cipher can be a walk on a permutation instead of a block function. RC4 is that walk. The plaintext is xored with the emitted bytes, and decryption is the same walk. The permutation is the whole secret state after the key schedule. The keystream is biased. Those biases distinguish the pad from a random string, which is why the cipher is obsolete. The fragment shows the walk. It is not a procedure for recovering a key.
What the algorithm guarantees
The same key reproduces the same keystream. Decryption is the same function. The first keystream bytes are not uniformly distributed, and a keystream from this construction is not a substitute for a random pad. A new design does not use RC4.
The working steps
- Start with the identity permutation of 0 through 255.
- For each index, add the permutation entry and the next key byte into
j, and swap the two entries. - For each plaintext byte, increment
i, addS[i]intoj, swap, and xor the plaintext byte withS[(S[i] + S[j]) mod 256].
Approaches that look simpler and fail
| Approach | Why it fails as a current stream cipher |
|---|---|
| Assume the emitted bytes are an unbiased pad | The keystream distribution is biased. A biased pad is distinguishable from a random pad. |
| Drop the first bytes and keep the same construction for new work | Discarding a prefix was an attempted patch. The cipher remains retired. |
| Reuse one keystream for two messages | The xor of the ciphertexts cancels the pad, as it does for any stream cipher. |
| Use the permutation walk as a hash | The walk is a keystream. It is not a collision-resistant digest. |
ChaCha20 is a current keystream with a nonce and a counter. RC4 is the historical permutation walk.
A working example
This Python fragment walks the RC4 permutation for the key Key and the plaintext Plaintext. It prints bbf316e8d940af0ad3.
def rc4(key: bytes, data: bytes) -> bytes:
s = list(range(256))
j = 0
for i in range(256):
j = (j + s[i] + key[i % len(key)]) % 256
s[i], s[j] = s[j], s[i]
i = j = 0
out = bytearray()
for byte in data:
i = (i + 1) % 256
j = (j + s[i]) % 256
s[i], s[j] = s[j], s[i]
out.append(byte ^ s[(s[i] + s[j]) % 256])
return bytes(out)
cipher = rc4(b"Key", b"Plaintext")
assert cipher.hex() == "bbf316e8d940af0ad3"
print(cipher.hex())
Where it is used
RC4 was the stream cipher of WEP and of old TLS cipher suites. Both were withdrawn because the keystream is biased. ChaCha20 is the stream cipher those protocols moved to.
When you can drop this algorithm
Drop RC4. The 256-byte permutation is the historical stream cipher. A current keystream is ChaCha20, with a nonce that is not repeated under the same key.
Why this stays published
We publish this so a search for RC4 returns the permutation walk and the reason the keystream is retired. RC4 swaps its way through a 256-byte permutation to emit a keystream, and that keystream is biased enough that the cipher is obsolete.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
