views:

56

answers:

2

I need to perform a 128-bit RC4 encryption, I'm using .NET and C#. Is there a built-in function to do this.

If not, I found this function that can do it:

public void RC4(ref Byte[] bytes, Byte[] key)
{
    Byte[] s = new Byte[256];
    Byte[] k = new Byte[256];
    Byte temp;
    int i, j;

    for (i = 0; i < 256; i++)
    {
        s[i] = (Byte)i;
        k[i] = key[i % key.GetLength(0)];
    }

    j = 0;
    for (i = 0; i < 256; i++)
    {
        j = (j + s[i] + k[i]) % 256;
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }

    i = j = 0;
    for (int x = 0; x < bytes.GetLength(0); x++)
    {
        i = (i + 1) % 256;
        j = (j + s[i]) % 256;
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
        int t = (s[i] + s[j]) % 256;
        bytes[x] ^= s[t];
    }
}

But I don't know if it's 128-bit or not, it looks 256, but I really don't know the difference.

A: 

I can tell you that there is no RC4 algorithm in the .Net Framework. There's an RC2 crypto... but no RC4.

codekaizen
http://en.wikipedia.org/wiki/RC4
Kamyar
+1  A: 

According to http://en.wikipedia.org/wiki/Rc4 RC4 algorithm can have keylength which can be in the range 1 ≤ keylength ≤ 256
Here's an example which you can determine the key size: http://tofuculture.com/Blog/post/RC4-Encryption-in-C.aspx
Download the source and view RC4.cs.

Kamyar
This helps, thanks. I assume I can just replace 256 with 128.
Steven
@Steven: No problem. glad I could help.
Kamyar