将RC4从c#转换为vb

时间:2011-07-04 13:05:00

标签: c# vb.net encryption

我遇到了必须将RC4代码从c#转换为vb的情况 我尝试将已经编写的代码在线,但它们似乎不像我现在的c#那样工作。 我对VB很新,所以对我来说这很难。也许专业人士会看到我犯的错误。两个代码都给出了不同的加密结果。

c#c​​ode:

public static string RC4(string pStrMessage, string pStrKey)
        {
            char[] lBytAsciiAry;
            int[] lBytKeyAry;
            int lLngIndex;  //
            int lBytJump;
            char lBytTemp;
            int lBytY;
            int lLngT;
            int lLngX;
            int lLngKeyLength;  //
            string encrypt; //The encrypted message

            lLngKeyLength = pStrKey.Length;
            if (lLngKeyLength < 1)
                return string.Empty;

            if (pStrMessage.Length < 1)
                return string.Empty;

            lBytAsciiAry = new char[256];
            lBytKeyAry = new int[256];

            for (lLngIndex = 0; lLngIndex < 256; lLngIndex++)
            {
                lBytKeyAry[lLngIndex] = pStrKey[(lLngIndex % lLngKeyLength)];
            }

            for (lLngIndex = 0; lLngIndex < 256; lLngIndex++)
            {
                lBytAsciiAry[lLngIndex] = Convert.ToChar(lLngIndex);
            }

            lBytJump = 0;
            for (lLngIndex = 0; lLngIndex < 256; lLngIndex++)
            {
                lBytJump = (lBytJump + lBytAsciiAry[lLngIndex] + lBytKeyAry[lLngIndex]) % 256;
                lBytTemp = lBytAsciiAry[lLngIndex];
                lBytAsciiAry[lLngIndex] = lBytAsciiAry[lBytJump];
                lBytAsciiAry[lBytJump] = Convert.ToChar(lBytTemp);
            }

            lLngIndex = 0;
            lBytJump = 0;
            encrypt = "";
            for (lLngX = 0; lLngX < pStrMessage.Length; lLngX++)
            {
                lLngIndex = (lLngIndex + 1) % 256; // wrap index
                lBytJump = (lBytJump + lBytAsciiAry[lLngIndex]) % 256; // wrap J+S()
                lLngT = (lBytAsciiAry[lLngIndex] + lBytAsciiAry[lBytJump]) % 256;
                //swap
                lBytTemp = lBytAsciiAry[lLngIndex];
                lBytAsciiAry[lLngIndex] = lBytAsciiAry[lBytJump];
                lBytAsciiAry[lBytJump] = lBytTemp;

                lBytY = lBytAsciiAry[lLngT];
                //encrypt = encrypt + Chr(Asc(Mid(pStrMessage, lLngX, 1)) Xor lBytY)
                encrypt = encrypt + Convert.ToChar(pStrMessage[lLngX] ^ lBytY);
            }

            return encrypt;
        }

我的VB翻译:

   Public Function RC4(ByVal pStrMessage As String, ByVal pStrKey As String) As String
        Dim lBytAsciiAry(0 To 256) As Char
        Dim lBytKeyAry(0 To 256) As Integer
        Dim lLngIndex As Integer
        Dim lBytJump As Integer
        Dim lBytTemp As Char
        Dim lBytY As Integer
        Dim lLngT As Integer
        Dim lLngX As Integer
        Dim lLngKeyLength As Integer
        Dim encrypt As String

        lLngKeyLength = pStrKey.Length

        If lLngKeyLength < 1 Then
            Return String.Empty
        End If

        If pStrMessage.Length < 1 Then
            Return String.Empty
        End If

        For lLngIndex = 0 To 255
            lBytKeyAry(lLngIndex) = Val(pStrKey((lLngIndex Mod lLngKeyLength)))
        Next

        For lLngIndex = 0 To 255
            lBytAsciiAry(lLngIndex) = Convert.ToChar(lLngIndex)
        Next

        lBytJump = 0
        For lLngIndex = 0 To 255
            lBytJump = (lBytJump + Asc(lBytAsciiAry(lLngIndex)) + lBytKeyAry(lLngIndex)) Mod 256
            lBytTemp = lBytAsciiAry(lLngIndex)
            lBytAsciiAry(lLngIndex) = lBytAsciiAry(lBytJump)
            lBytAsciiAry(lBytJump) = Convert.ToChar(lBytTemp)
        Next

        lLngIndex = 0
        lBytJump = 0
        encrypt = ""

        For lLngX = 0 To pStrMessage.Length - 1
            lLngIndex = (lLngIndex + 1) Mod 256
            lBytJump = (lBytJump + Asc(lBytAsciiAry(lLngIndex))) Mod 256
            lLngT = Asc(lBytAsciiAry(lLngIndex) + lBytAsciiAry(lBytJump)) Mod 256

            lBytTemp = lBytAsciiAry(lLngIndex)
            lBytAsciiAry(lLngIndex) = lBytAsciiAry(lBytJump)
            lBytAsciiAry(lBytJump) = lBytTemp

            lBytY = Asc(lBytAsciiAry(lLngT))
            encrypt = encrypt + Convert.ToChar(Asc(pStrMessage(lLngX)) Xor lBytY)
        Next

        Return encrypt
    End Function

1 个答案:

答案 0 :(得分:1)

尝试使用此C#到VB转换器来查看是否获得了所需的结果http://www.developerfusion.com/tools/convert/csharp-to-vb/