RC4 — KSA & PRGA

Finish your 3-bit example, then scale up

Mission tie-in: Your notes completed KSA but stopped mid-PRGA. This lesson finishes the encryption and gives you a 4-bit template for exam practice.

Two phases

Phase 1 — KSA

Initialize S = [0,1,…,n−1]. Repeat key into T. Swap S[i] with S[j] where:

j = (j + S[i] + T[i]) mod n

Real RC4: n=256. Your toy: n=8 (3-bit).

Phase 2 — PRGA

Generate keystream byte k, XOR with plaintext:

i = (i + 1) mod n
j = (j + S[i]) mod n
swap S[i], S[j]
t = (S[i] + S[j]) mod n
k = S[t]
C = P ⊕ k
Key K KSA Permuted S-box [0,4,7,3,1,2,6,5] PRGA Plaintext P Ciphertext C keystream k from PRGA

Your 3-bit example — completed

K = [3,5,2], P = [6,1,4]. Steps computed live (Stallings RC4: KSA then PRGA).

4-bit / 5-char worked solution

Scale to mod 16. S = [0,1,…,15]. K = [3,5,2,7]. P = [9,4,12,7,1]. Steps below are computed live — change inputs to practice.

Retrieval practice

After KSA finishes, what operation produces the ciphertext?

Stuck? Ask: "Check my 4-bit RC4 KSA" or "Show PRGA step 2 with my S array."