RC4 is a stream cipher that produces a keystream byte at each step. The algorithm consists of a self-modifying lookup table.
Suppose there are N key bytes, key[0],key[1],...,key[n-1]. Then the RC4 algorithm can be described as follows.
Initialize: for i = 0 to 255 S[i] = i; K[i] = key[i (mod N)]; next i j = 0; for j = 0 to 255 j = (j + S[i] + K[i]) mod 256; swap(S[i], S[j]); next j i = j = 0; For each keystream byte: i = (i + 1) mod 256; j = (j + S[i]) mod 256; swap(S[i], S[j]); t = (S[i] + S[j]) mod 256; keystreamByte = S[t];
Note: The first 256 keystream bytes must be discarded.