**Input:** a key (seed) of length up to 256 bytes
**Output:** a random byte (to be XORed with plaintext)

**Initialization:** Let *K*_{0}, *K*_{1},
*K*_{2}, ... *K*_{255} be the bytes of the
key, repeating the key as necessary

*j* = 0

for *i* = 0 to 255

*S*_{i} = *i*

for *i* = 0 to 255

j = (j + *S*_{i} + *K*_{i}) mod 256

swap *S*_{i} and *S*_{j}

*i *= 0

*j* = 0

**Algorithm:**

*i* = ( *i* + 1 ) mod 256

*j* = ( *j* + *S*_{i} ) mod 256

swap *S*_{i} and *S*_{j}

t = (*S*_{i} + *S*_{j} ) mod 256

output *S*_{t}

### Reference:

Applied Cryptography, by Bruce Schneier,
Wiley, 1996