RC4 128 bit encryption in C#
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.
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.
UPDATE: The link is dead. here's the Archive.org's snapshot.
Key setup is the first and most difficult phase of this algorithm. During a N-bit key setup (N being your key length), the encryption key is used to generate an encrypting variable using two arrays, state and key, and N-number of mixing operations. These mixing operations consist of swapping bytes, modulo operations, and other formulae.
In the attached project you can see how I do it in the EncryptionKey set property of RC4Engine class.
this is an interesting article for this subject - http://www.codeproject.com/Articles/5068/RC4-Encryption-Algorithm-C-Version
I can tell you that there is no RC4 algorithm in the .Net Framework. There's an RC2 crypto... but no RC4.
The RC4 alghorithm uses a variable key length (not only 128-bit) and the same amount of encrypted and original data.
public class RC4
{
// Creates an RC4 instance using the given key of any length.
public RC4(byte[] key)
{
CreateSBlock();
KeyScheduling(key);
}
// Performs encryption or decryption of the data.
public void Cipher(byte[] buffer, int offset, int count)
{
for (int i = offset; i < count; i++)
{
buffer[i] = unchecked((byte)(buffer[i] ^ NextByte()));
}
}
private byte[] sblock = new byte[256]; // The array contained S-block.
private int x = 0, y = 0;
private void CreateSBlock() // S-block initialization.
{
for (int i = 0; i < 256; i++)
{
sblock[i] = (byte)i;
}
}
private void KeyScheduling(byte[] key) // KSA
{
for (int i = 0, j = 0, l = key.Length; i < 256; i++)
{
j = (j + sblock[i] + key[i % l]) % 256;
Swap(sblock, i, j);
}
}
private void Swap(byte[] array, int index1, int index2)
{
byte b = array[index1];
array[index1] = array[index2];
array[index2] = b;
}
private byte NextByte() // PRGA
{
x = (x + 1) % 256;
y = (y + sblock[x]) % 256;
Swap(sblock, x, y);
return sblock[(sblock[x] + sblock[y]) % 256];
}
}
Here is a C# library that provides the RC4 algorithm for use in .NET projects. Example of usage:
using System.Security.Cryptography;
// ...
byte[] password = Encoding.UTF8.GetBytes("password");
byte[] data = Encoding.UTF8.GetBytes("secret");
byte[] encrypted, restored;
using (var arc4 = ARC4.Create(password)
{
using(var transform = arc4.CreateEncryptor()) // Encryption.
{
encrypted = transform.TransformFinalBlock(data, 0, data.Length);
}
using(var transform = arc4.CreateDecryptor()) // Decryption.
{
restored = transform.TransformFinalBlock(data, 0, data.Length);
}
}
精彩评论