开发者

converting RC4 from c# to vb

I was put in a situation where I had to convert RC4 code from c# into vb I tried getting already made codes onli开发者_开发知识库ne but they did not seem to work as the currently c# one that I have. I am very new to VB so this is very hard for me. Maybe a professional will see the mistake I am making. Both codes give a different encrypted result.

c# code:

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;
        }

my VB translation of it:

   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


Try using this C# to VB converter to see if you get the desired result http://www.developerfusion.com/tools/convert/csharp-to-vb/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜