将VB6代码转换为PHP

时间:2018-05-04 05:31:51

标签: php vb6

我想将Visual Basic 6代码转换为PHP代码。我是PHP的新手请帮我将VB6代码转换为PHP。到目前为止,当我尝试代码时,我尝试将其转换为PHP代码,“

中存在错误
  

CryptRC4 = CryptRC4& Chr $((pvCryptXor(baS((CLng(baS(li))+ baS(lJ))Mod 256),Asc(Mid $(sText,lIdx,1)))));

部分,我也不知道如何进入子功能。请参阅以下代码。 vb代码用于encrypt个字符串。我想将其转换为php格式。

PHP代码

<?php 

    function CryptRC4($sText,$sKey){
        $baS = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
                          16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
                          32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
                          48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
                          64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
                          80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
                          96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
                          112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
                          128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
                          144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
                          160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
                          176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
                          192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
                          208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
                          224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
                          240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255);
        $baK = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
                          16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,
                          32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,
                          48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
                          64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
                          80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,
                          96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
                          112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
                          128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
                          144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
                          160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
                          176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
                          192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
                          208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
                          224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
                          240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255);
        $bytSwap = 0;
        $li = 0;
        $lJ = 0;
        $lIdx = 0;

        for( $lIdx = 0; $lIdx < 256; $lIdx++){
            $baS[$lIdx] = $lIdx;
            $baK[$lIdx] = ord(substr($sKey, 1 + ($lIdx % strlen($sKey)), 1));   
        }

        for($li = 0; $li < 256; $li++){
            $lJ = ($baS[$li] + $baK[$li]) % 256;
            $bytSwap = $baS[$li];
            $baS[$li] = $baS[$lJ];
            $baS[$lJ] = $bytSwap;
        }

        $li = 0;
        $lJ = 0;
        $data_str = "";
        for($lIdx = 0; $lIdx < strlen($sText); $lIdx++){
            $li = ($li + 1) % 256;
            $lJ = ($lJ + $baS[$li]) % 256;
            $bytSwap = $baS[$li];
            $baS[$li] = $baS[$lJ];
            $baS[$lJ] = $bytSwap;
            #echo chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1)))));
            $data_str .= chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1)))));
        }   
        echo $data_str;
    }


        function pvCryptXor($li, $lJ){
            if($li = $lJ){
                $pcx = $lJ;
            }
            else {
                $pcx = $li Xor $lJ;
            }

            return $pcx;
        }

        unction ToHexDump($sText) {
    $lIdx;
    for($lIdx = 1; $lIdx < strlen($sText); $lIdx++){
        $thd .= Right$("0" & Hex(Asc(Mid(sText, lIdx, 1))), 2)
        echo $thd;
    }
    return $thd;
}

FromHexDump("events");

function FromHexDump($sText) {
    $fhd = "";
    for($lIdx = 0; $lIdx < strlen($sText); $lIdx++){
         $fhd .= chr(CLng("&H" & Mid(sText, lIdx, 2)));
    }
    return $fhd;
}

    ?>

VB代码:

Public Function CryptRC4(sText As String, sKey As String) As String
On Error Resume Next
    Dim baS(0 To 255) As Byte
    Dim baK(0 To 255) As Byte
    Dim bytSwap     As Byte
    Dim li          As Long
    Dim lJ          As Long
    Dim lIdx        As Long

    For lIdx = 0 To 255
        baS(lIdx) = lIdx
        baK(lIdx) = Asc(Mid$(sKey, 1 + (lIdx Mod Len(sKey)), 1))
    Next
    For li = 0 To 255
        lJ = (lJ + baS(li) + baK(li)) Mod 256
        bytSwap = baS(li)
        baS(li) = baS(lJ)
        baS(lJ) = bytSwap
    Next
    li = 0
    lJ = 0
    For lIdx = 1 To Len(sText)
        li = (li + 1) Mod 256
        lJ = (lJ + baS(li)) Mod 256
        bytSwap = baS(li)
        baS(li) = baS(lJ)
        baS(lJ) = bytSwap
        CryptRC4 = CryptRC4 & Chr$((pvCryptXor(baS((CLng(baS(li)) + baS(lJ)) Mod 256), Asc(Mid$(sText, lIdx, 1)))))
    Next
End Function

Private Function pvCryptXor(ByVal li As Long, ByVal lJ As Long) As Long
On Error Resume Next
    If li = lJ Then
        pvCryptXor = lJ
    Else
        pvCryptXor = li Xor lJ
    End If
End Function

Public Function ToHexDump(sText As String) As String
On Error Resume Next
    Dim lIdx            As Long

    For lIdx = 1 To Len(sText)
        ToHexDump = ToHexDump & Right$("0" & Hex(Asc(Mid(sText, lIdx, 1))), 2)
    Next
End Function

Public Function FromHexDump(sText As String) As String
On Error Resume Next
    Dim lIdx            As Long

    For lIdx = 1 To Len(sText) Step 2
        FromHexDump = FromHexDump & Chr$(CLng("&H" & Mid(sText, lIdx, 2)))
    Next

End Function

3 个答案:

答案 0 :(得分:3)

我修改了您更新的代码,看起来您只有一些小错误,看起来和我的更改:

我猜你可以使用PHP函数hex2binbin2hex中的构建代替你自己的十六进制转换。

function CryptRC4($sText,$sKey){
    $baS = range(0, 255); // you can use range instead of your manual arrays
    $baK = range(0, 255);
    $bytSwap = 0;
    $li = 0;
    $lJ = 0;
    $lIdx = 0;

    for( $lIdx = 0; $lIdx < 256; $lIdx++){
        $baS[$lIdx] = $lIdx;
        $baK[$lIdx] = ord(substr($sKey, 1 + ($lIdx % strlen($sKey)), 1));
    }

    for($li = 0; $li < 256; $li++){
        $lJ = ($baS[$li] + $baK[$li]) % 256;
        $bytSwap = $baS[$li];
        $baS[$li] = $baS[$lJ];
        $baS[$lJ] = $bytSwap;
    }

    $li = 0;
    $lJ = 0;
    $data_str = "";
    for($lIdx = 0; $lIdx < strlen($sText); $lIdx++){
        $li = ($li + 1) % 256;
        $lJ = ($lJ + $baS[$li]) % 256;
        $bytSwap = $baS[$li];
        $baS[$li] = $baS[$lJ];
        $baS[$lJ] = $bytSwap;
        #echo chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1)))));
        $data_str .= chr((pvCryptXor($baS[(round(($baS[$li]) + $baS[$lJ])) % 256], ord(substr($sText, $lIdx, 1)))));
    }
    return $data_str; // changed from echo to return
}

function pvCryptXor($li, $lJ){
    if($li == $lJ){ // you had an error here, use == to compare instead of a single =
        $pcx = $lJ;
    }
    else {
        $pcx = $li ^ $lJ; // XOR function in PHP is the ^ operator
    }

    return $pcx;
}


$str_hex = bin2hex("events");
$str_enc = CryptRC4($str_hex,"password");
$str_dec = hex2bin(CryptRC4($str_enc,"password"));

echo $str_hex . PHP_EOL . $str_enc . PHP_EOL . $str_dec;

输出:

  

6576656e7473

     

&#39;〜我

     

事件

所以在我看来,因为它实际上正确编码和解码!?

答案 1 :(得分:2)

CryptRC4函数的原始VB6实现似乎来自我的answer to "VB6 encrypt text using password" question

因此,让我尝试使用VB6片段中所有公共函数的简短php实现来回答您的问题:

function CryptRC4($text, $key) {
    return openssl_encrypt($text, "RC4-40", $key, 1 | 2);
}

function ToHexDump($text) {
    return strtoupper(bin2hex($text));
}

function FromHexDump($text) {
    return hex2bin($text);
}

你可以用这样的东西来练习这些单行:

$text = "a message here";
$password = "password";
$encr = ToHexDump(CryptRC4($text, $password));
$decr = CryptRC4(FromHexDump($encr), $password);
echo $text . PHP_EOL . $encr . PHP_EOL . $decr;

答案 2 :(得分:0)

因为它用于密码,你可以省去很多麻烦。 PHP内置了函数(版本5.5和更新版本),用于处理密码散列和验证用户提交的密码的散列密码。阅读PHP相关的PHP手册页http://php.net/manual/en/book.password.php