static void rc4_setup(ref rc4_state s, byte[] key, int length) { int i, j, k, a; int[] m = new int[256]; s.x = 0; s.y = 0; m = s.m; for (i = 0; i < 256; i++) { m[i] = i; } j = k = 0; for (i = 0; i < 256; i++) { a = m[i]; j = (byte)(j + a + key[k]); m[i] = m[j]; m[j] = a; if (++k >= length) { k = 0; } } }
static void rc4_crypt(ref rc4_state s, ref byte[] data, int length) { int i, x, y, a, b; int[] m = new int[256]; x = s.x; y = s.y; m = s.m; for (i = 0; i < length; i++) { x = (byte)(x + 1); a = m[x]; y = (byte)(y + a); m[x] = b = m[y]; m[y] = a; data[i] ^= (byte)m[(byte)(a + b)]; } s.x = x; s.y = y; }