0

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.

user572575
  • 1,009
  • 3
  • 25
  • 45
  • 1
    `box[y] = box[j];` `j` probably is causing this here. – kevintjuh93 Sep 17 '15 at 12:02
  • General method: debug or print intermediate values using another implementation, then try and mimic that in your C# code. – Maarten Bodewes Sep 17 '15 at 18:53
  • 1
    I think the likely issue is this line: z = box[ j + box[(y + box[z+k]) % 256]]; I cannot recall 100% off the top of my head, but I think the mod comes before the add, meaning you can get numbers higher than the size of box[]. To correct this, try: z = box[ (j + box[(y + box[z+k])) % 256]]; And actually you may need more mods in there. Main thing is not to let any index get larger than the array size. – WDS Sep 18 '15 at 02:37
  • To be more clear if less specific, inside each set of brackets for the array, you should consider add then mod. So z+k then mod. Look up the value of that element, add y, and mod again. Look up the value of that element, add j and mod a third time. Then look up the value of that element and assign to z. That is if this is what the algorithm calls for. But on first pale, it definitely seems like this will help. – WDS Sep 18 '15 at 02:44
  • On first "pale"? What does that mean, after the first beer? – Maarten Bodewes Sep 18 '15 at 12:43
  • @MaartenBodewes Weirdly, I have lived my whole life thinking "on first pale" was a widely used and understood expression. And now it appears that is not the case at all. It referred to my first impression. Perhaps now would be the time for that pale (ale). – WDS Sep 18 '15 at 19:00
  • 1
    Ah, well language. You can imagine my surprise that "showstopper" can mean either that there is something that *stops the show* or *an exhibit in the show that makes you stop*. One is bad, the other is good. Go figure :) – Maarten Bodewes Sep 18 '15 at 19:03

0 Answers0