本文介绍了在C#中RC4 128位加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行128位RC4加密,我使用.NET和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];
    }
}

但我不知道这是否是128位与否,它看起来256,但我真的不知道其中的差别。

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

推荐答案

根据的http:// EN。 wikipedia.org/wiki/Rc4 的RC4算法可以具有keylength其可以是范围为1≤keylength≤256
这里是你可以决定密钥大小的一个例子:的http:// tofuculture。 COM /博客/后/ RC4加密功能于C.aspx
下载源代码,并查看RC4.cs。

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.

这篇关于在C#中RC4 128位加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 07:31