开发者

Converting a Visual Basic function to PHP

I have a Visual Basic function and I am not that familiar with VB. I need to convert it to PHP and have made a start. There are a couple of functions I'm not sure how to replicate and am looking for some help with this and to see if I have got the nesting right etc. In the following code, there is the vb function and then my attempt at te php version. it is not complete and in the php version I have commented out the vb parts I am not sure about. Can anyone help put me on the right lines?

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 = 开发者_StackOverflow(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

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


  • The arithmetic modulo can be used in php using % operator.

$modulus = $a % $b

  • I think you will get errors with your 3 embedded for (x…) loops by the way.

  • To access an element of the array (the $ith for instance) don't use $array['i'] but $array[$i].

  • The StrConv must be a ut8_encode and the left stuff can be done with substr($string, 0, 255).

References: utf8_encode substr


If you are trying to do RC4 encryption in PHP then you may wan't to take a look at the project at this link http://code.google.com/p/rc4crypt/


I really hope you have some unit tests to test that:

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

I finished the code you started, but it seems really wrong (for example, why nest 3 for loops, using the same variable ?). It doesn't even seem to match the initial VB code...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜