将Visual Basic函数转换为PHP

时间:2011-06-20 16:05:25

标签: php encryption vbscript

我有一个Visual Basic函数,我不熟悉VB。我需要将它转换为PHP并开始了。有几个函数我不知道如何复制,我正在寻找一些帮助,看看我是否有嵌套权等。在下面的代码中,有vb函数,然后我尝试te php版本。它不完整,在php版本中我已经注释掉了我不确定的vb部分。任何人都可以帮我把它放在正确的位置吗?

Public Function gfnCrypt(ByVal Expression As String, ByVal Password As String) As String
  'RC4 Encryption
  Dim rb(0 To 255) As Integer, X As Long, Y As Long, Z As Long, Key() As Byte, ByteArray() As Byte, temp As Byte
  On Error Resume Next
  If Len(Password) = 0 Then
    Exit Function
  End If
  If Len(Expression) = 0 Then
    Exit Function
  End If
  If Len(Password) > 256 Then
    Key() = StrConv(Left$(Password, 256), vbFromUnicode)
  Else
    Key() = StrConv(Password, vbFromUnicode)
  End If
  For X = 0 To 255
    rb(X) = X
  Next X
  X = 0
  Y = 0
  Z = 0
  For X = 0 To 255
    Y = (Y + rb(X) + Key(X Mod Len(Password))) Mod 256
    temp = rb(X)
    rb(X) = rb(Y)
    rb(Y) = temp
  Next X
  X = 0
  Y = 0
  Z = 0
  ByteArray() = StrConv(Expression, vbFromUnicode)
  For X = 0 To Len(Expression)
    Y = (Y + 1) Mod 256
    Z = (Z + rb(Y)) Mod 256
    temp = rb(Y)
    rb(Y) = rb(Z)
    rb(Z) = temp
    ByteArray(X) = ByteArray(X) Xor (rb((rb(Y) + rb(Z)) Mod 256))
  Next X
  gfnCrypt = StrConv(ByteArray, vbUnicode)
End Function

在PHP中:

function gfnCrypt($mywebpassword, $mywebkey) {
  //'RC4 Encryption
  //Dim rb(0 To 255) As Integer, X As Long, Y As Long, Z As Long, Key() As Byte, ByteArray() As Byte, temp As Byte
  if(strlen($mywebpassword) == 0){
    return false;
  }
  if(strlen($mywebkey) == 0){
    return false;
  }
  if(strlen($mywebpassword) > 256){
    //Key() = StrConv(Left$(Password, 256), vbFromUnicode)
  }else{
    //Key() = StrConv(Password, vbFromUnicode)
  }
  $rb=array();
  for($x=0;$x=255;$x++){
    $rb['x'] = $x;
    for($x=0;$x=255;$x++){
     $y = ($y + $rb['x'];// + Key(X Mod Len(Password))) Mod 256
     $temp = $rb['x'];
     $rb['x'] = $rb[$y];
     $rb[$y] = $temp;
     //ByteArray() = StrConv(Expression, vbFromUnicode)
      for($x=0;$x=strlen($mywebpassword), $x++){
       $y = ($y + 1);// Mod 256
       $z = ($z + $rb[$y]);// Mod 256
       $temp = $rb[$y];
       $rb[$y] = $rb[$z];
       $rb[$z] = $temp;
       //ByteArray(X) = ByteArray(X) Xor (rb((rb(Y) + rb(Z)) Mod 256))
      }
    }
  }
  //gfnCrypt = StrConv(ByteArray, vbUnicode)
  return $gfnCrypt;
}

3 个答案:

答案 0 :(得分:2)

  • 算术模数可以在php中使用%运算符。

$modulus = $a % $b

  • 顺便提一下,我认为您的3个嵌入式for (x…)循环会出错。

  • 要访问数组的元素(例如$i),请不要使用$array['i'],而应使用$array[$i]

  • StrConv必须是ut8_encode,而left内容可以substr($string, 0, 255)完成。

参考文献:utf8_encode substr

答案 1 :(得分:1)

如果您尝试在PHP中进行RC4加密,那么您可能不想在这个链接上查看该项目http://code.google.com/p/rc4crypt/

答案 2 :(得分:1)

我真的希望你有一些单元测试来测试:

function gfnCrypt($mywebpassword, $mywebkey) {
  //'RC4 Encryption
  //Dim rb(0 To 255) As Integer, X As Long, Y As Long, Z As Long, Key() As Byte, ByteArray() As Byte, temp As Byte
  rb = array();
  Key = array();
  ByteArray = array();
  if(strlen($mywebpassword) == 0){
    return false;
  }
  if(strlen($mywebkey) == 0){
    return false;
  }
  if(strlen($mywebpassword) > 256){
    //Key() = StrConv(Left$(Password, 256), vbFromUnicode)
    Key[] = ut8_encodesubstr(Password, 0, 256));
  }else{
    //Key() = StrConv(Password, vbFromUnicode)
    Key[] = ut8_encode(Password);
  }
  $rb=array();
  for($x=0;$x=255;$x++){
    $rb['x'] = $x;
    for($x=0;$x=255;$x++){
     $y = ($y + $rb['x'] + Key(X % strlen(Password))) % 256;
     $temp = $rb['x'];
     $rb['x'] = $rb[$y];
     $rb[$y] = $temp;
     //ByteArray() = StrConv(Expression, vbFromUnicode)
     ByteArray[] = ut8_encode(Expression);
      for($x=0;$x=strlen($mywebpassword), $x++){
       $y = ($y + 1);// Mod 256
       $z = ($z + $rb[$y]);// Mod 256
       $temp = $rb[$y];
       $rb[$y] = $rb[$z];
       $rb[$z] = $temp;
       //ByteArray(X) = ByteArray(X) Xor (rb((rb(Y) + rb(Z)) Mod 256))
       ByteArray[X] = (ByteArray[X] ^= (rb[(rb[Y] + rb[Z]] % 256)]);
      }
    }
  }
  //gfnCrypt = StrConv(ByteArray, vbUnicode)
  gfnCrypt = ut8_encode(ByteArray);
  return $gfnCrypt;
}

我完成了你开始的代码,但它似乎真的错了(例如,为什么使用相同的变量嵌套3 for循环?)。它甚至似乎与最初的VB代码不匹配......