I try to understand Spritz or RC4-like from this web . And I want to implement Spritz with C#. Spritz add k and w to algorithm different from RC4. I'm not sure how to implement it. I try to edit code from RC4 like this .
class RC4Class
{
public static string RC4(string input , string key)
{
StringBuilder result = new StringBuilder();
int x, y, j = 0 , k=0 , z =0 ;
int[] box = new int[256];
for(int i = 0; i < 256; i++)
{
box[i] = i;
}
for(int i=0; i<256 ; i++)
{
j = (key[i % key.Length] + box[i] + j) % 256;
x = box[i];
box[i] = box[j];
box[j] = x;
}
for(int i = 0; i < input.Length; i++)
{
//RC4 Code
/* y = i % 256;
j = (box[y] + j) % 256;
//swap
x = box[y];
box[y] = box[j];
box[j] = x;
//XOR
z = box[(box[y] + box[j]) % 256];
result.Append((char)(input[i] ^ z)); */
//Spritz
y = i % 256;
j = k + box[(box[y] + box[j]) % 256] ;
x = box[y];
box[y] = box[j];
box[j] = x;
z = box[ j + box[(y + box[z+k]) % 256]];
result.Append((char)(input[i] ^ z));
}
return result.ToString();
}
}
when I run this code it show error like this .
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in RC4CSharp.exe
please help me how to implement Splitz with any language.