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 = (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;
}
%operator.$modulus = $a % $bI 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
StrConvmust be aut8_encodeand theleftstuff can be done withsubstr($string, 0, 255).References: utf8_encode substr