转换迈克·谢弗的RC4Encryption到C# [英] Converting Mike Shaffer's RC4Encryption to C#

查看:142
本文介绍了转换迈克·谢弗的RC4Encryption到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的文章, http://www.4guysfromrolla.com/articles /091802-1.3.aspx ,它展示了如何迈克·谢弗的VB RC4加密转换为C#,但是我得到比原来的文章不同的结果,的 http://www.4guysfromrolla.com/webtech/010100-1.shtml

I'm following an article, http://www.4guysfromrolla.com/articles/091802-1.3.aspx, which shows how to convert Mike Shaffer's VB RC4 encryption to C#, however I'm getting different results than the original article, http://www.4guysfromrolla.com/webtech/010100-1.shtml.

使用从原来的文章这个测试环节, http://www.4guysfromrolla.com/demos /rc4test.htm ,用ABC和是testing123纯文本的密码,我得到B9 F8 AA 5D 31 B1 8A 42 1E D4。但是,使用C#版本的时候,我得到的东西稍有不同:B9 F8 AA 5D 31 B1 160 42 1E D4。我得到的,而不是8A160。

Using this test link from the original article, http://www.4guysfromrolla.com/demos/rc4test.htm, with a password of "abc" and plain text of "testing123", I get "B9 F8 AA 5D 31 B1 8A 42 1E D4". However, when using the C# version, I get something slightly different: "b9 f8 aa 5d 31 b1 160 42 1e d4". I'm getting "160" instead of "8A".

下面是我的方法,它的ASCII码(C#的方法,最终结果)转换为十六进制:

Here's my method which converts the ASCII (final result of C# method) to hex:

public static string ConvertAsciiToHex(string input)
{
    return string.Join(string.Empty, input.Select(c => Convert.ToInt32(c).ToString("X")).ToArray());
}

和这里的C#code我从文章(修改,以静态类):

And here's the C# code I have from the article (modified to static class):

protected static int[] sbox = new int[256];
protected static int[] key = new int[256];
private static string password = "abc";

private static void RC4Initialize(string strPwd)
{
    int intLength = strPwd.Length;

    for (int a = 0; a <= 255; a++)
    {
        char ctmp = (strPwd.Substring((a % intLength), 1).ToCharArray()[0]);

        key[a] = Microsoft.VisualBasic.Strings.Asc(ctmp);
        sbox[a] = a;
    }


    int x = 0;

    for (int b = 0; b <= 255; b++)
    {
        x = (x + sbox[b] + key[b]) % 256;
        int tempSwap = sbox[b];
        sbox[b] = sbox[x];
        sbox[x] = tempSwap;
    }
}

private static string EnDeCrypt(string text)
{
    int i = 0;
    int j = 0;
    string cipher = "";

    RC4Initialize(password);


    for (int a = 1; a <= text.Length; a++)
    {
        int itmp = 0;

        i = (i + 1) % 256;
        j = (j + sbox[i]) % 256;
        itmp = sbox[i];
        sbox[i] = sbox[j];
        sbox[j] = itmp;

        int k = sbox[(sbox[i] + sbox[j]) % 256];

        char ctmp = text.Substring(a - 1, 1).ToCharArray()
        [0];

        itmp = Microsoft.VisualBasic.Strings.Asc(ctmp);

        int cipherby = itmp ^ k;

        cipher += Microsoft.VisualBasic.Strings.Chr(cipherby);
    }

    return cipher;
}

我打电话的方法,像这样:

I'm calling the method like so:

public static string Encrypt(string text)
{ 
    return ConvertAsciiToHex(EnDeCrypt(text));
} 

RC4Encrypt.Encrypt("testing123");

我是什么做错了吗?

What am I doing wrong?

推荐答案

这是给的 人权委员会 和的 CHRW

It's a difference between calls to Chr and ChrW.

人权委员会将只需要0和255之间的值,并返回一个字符,它的值超出该范围的(至少在我的机器上,如下图所示)。你所看见的是138。

Chr will only take values between 0 and 255 and will return a char whose value is out of that range (at least on my machine as shown below). You were seeing a 138.


128 != 8364 (?)
130 != 8218 (,)
131 != 402 (ƒ)
132 != 8222 (,)
133 != 8230 (.)
134 != 8224 (+)
135 != 8225 (╪)
136 != 710 (^)
137 != 8240 (%)
138 != 352 (S)
139 != 8249 ()
156 != 339 (o)
158 != 382 (z)
159 != 376 (Y)

对于一个更好的解释,一个VB.Net开发可能需要...; - )

For a better explanation, a VB.Net dev may be required... ;-)

鉴于这种情况,但是,几乎没有需要使用 Microsoft.VisualBasic程序电话(呼叫VB时,几乎没有一个翻译... ;-)),因为使用字符将工作得很好,你在做什么。

Given that, however, there's little need for using the Microsoft.VisualBasic calls (hardly a translation when calling VB... ;-) ) because using char will work just fine for what you're doing.

itmp = ctmp;  //there's an implicit conversion for char to int

int cipherby = itmp ^ k;

Console.WriteLine("cipherby = {0}", cipherby);
cipher += (char)cipherby; //just cast cipherby to a char

这篇关于转换迈克·谢弗的RC4Encryption到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆