ARC4 works well as a stream cipher for a random number generator for a number of reasons. First, it's a stream cipher that works with 8-bits at a time. This is nice because we can retrieve arbitrary lengths of data with no extra steps. Secondly, ARC4 is quite small and fast. This is nice because any program using the random number system won't suffer a large size increases or performance loss by using the generator. ARC4 has one added benefit as well, which will be discussed below.
First, let us look at the design of ARC4. The heart of the ARC4 algorithm is a strong random number generator in which the key is the seed value. For encryption, the random data generated by the algorithm is XORed with the plaintext to produce the encrypted text.
The state of ARC4 consists of an array of 256 8-bit values and two 8-bit
ARC4's key scheduling is laid out like this:
The cipher works on a block of data like this:
We can quickly convert ARC4 into just random number generator by simply
using 'XOR_Byte' instead of XORing with anything. The loop
above would then turn into this:
Now look at the key scheduler and the stream generator and
note the relationship of 'x', 'y' and 'State' in these two processes.
'x' is continually counting up and is used as an index info 'State' when
modifying 'y'. The state at index 'x' and index 'y' and then swapped.
In both scheduling and generation, 'y' is modified by adding 'State[ x
]'. The difference is, during scheduling, the key is also added to 'y'.
Now, the key scheduling becomes this:
Note that 'x' is set to 255 instead of 0. This is because in the key scheduling, 'x' is incremented at the end of the loop rather than at the beginning. To address this, we set 'x' to 255 so that after being incremented, 'x' becomes 0-- thus working with incrementing 'x' at the beginning of the loop rather than the end.
The random stream generation now becomes this:
See now that we call 'ClockARC4' with 0, because
we are not modifying the state with any external data. We could
actually place any static number in this parameter and not effect the
strength of the algorithm-- but we would effect output. Doing this
would nether add nor subtract from the strength of the generator.
random data to generator }
key that was pressed }
In the Cypher libraries, the process described above is performed in
in the unit 'RandUnit.Pas'