I am implementing a RC4 algorithm in C# for my WPF app.
I followed this pdf stating the algorithm.
The thing is, in the KSA, we ought to do : j = (j + S[i] + (int)key[i % keyLengthInBits]) % 256;
.
That being said, I do not understand how this would work because the key ought to be from 5 characters long to 32 characters long (40bits up to 256bits).
So let's take an example with a 5 characters key ( I'll use the same as in the pdf I linked above) : pwd12. You go fetch the character of the key at the position i % 40
(5 characters long is 40 bits). It's fine for the first 5 times because from i = 0
to i=4
, we have a value in the key (pwd12). Though here is the problem (from how I see it) : when we are at i=5
, we do not have any characters left in the key. Therefore we will get an ÒutOfBounds Exception
.
How is it possible to works if we try to fetch a character in the key where there is none? Obviously there is something I do not see in the algorithm because it does work otherwise it would not be used...