public void RC4
(ref Byte[] bytes,
Byte[] key
)
{
Byte[] s =
new Byte[256];
Byte[] k =
new Byte[256];
Byte temp;
int i, j, t;
int byteLen = bytes.
GetLength(0);
int keyLen = key.
GetLength(0);
// Generate "8x8 S-Box" and initialize key index
for (i =
0; i <
256; i++
)
{
s
[i
] =
(Byte) i;
k
[i
] = key
[i % keyLen
];
}
j =
0;
for (i =
0; i<
256; i++
)
{
j =
(j + s
[i
] + k
[i
]) %
256;
// swap
temp = s
[i
];
s
[i
] = s
[j
];
s
[j
] = temp;
}
i = j =
0;
for (int x =
0; x < byteLen; x++
)
{
// The following is used to generate a random byte
i =
(i +
1) %
256;
j =
(j + s
[i
]) %
256;
temp = s
[i
];
s
[i
] = s
[j
];
s
[j
] = temp;
t =
((int)s
[i
] + s
[j
]) %
256;
// which is xor'd to the source
bytes
[x
] ^= s
[t
];
}
}