Jump to content

Converting A Block Of Vb6 To Php


KenHorse

Recommended Posts

I am new to PHP but have been programming in VB6 for many years. I have the following code I'm trying to convert to PHP and I've figured most of it out but am stuck on one line that I can't find an equivalent way to do in PHP (it is a VERY simple encryption scheme I know but I cannot change it). My plan is for a webform to supply the Username and callsign but for testing purposes, I'm assigning those variables.

 

Here's the VB6 code:


Username = "Peter Piper"
CallSign = "aa4aa"

CallSign = UCase(CallSign) 'force uppercase callsign

If Len(Username) Then
For A = 1 To Len(CallSign)
 B = Asc(Mid(CallSign, A, 1))
 B = B + Asc(Mid(Username, (A Mod Len(Username)) + 1, 1))
 strBuff = strBuff & Chr$(B And &HFF)
Next
Else
 strBuff = CallSign
End If

 

And here's what I've come up with (so far) in PHP:

 

$username = "Peter Piper;
$callsign = "aa4aa" ;

$uppercase = strtoupper($callsign) ;
$callsign = $uppercase ;
$datalen = strlen($username) ;

//Encrypt string

If($datalen > 0) {

 For($A=1; $A<=$datalen; $A++)
 {
 $B = substr($callsign, $A, 1) ; // get one character at a time
 $B = ord($B) ; // convert to ASCII	


 }

}

 

Thanks in advance

Edited by KenHorse
Link to comment
Share on other sites

Ahh, VB6! Takes me back to my younger days ...

 

I think this would do it.

$Username = "Peter Piper";
$CallSign = "aa4aa";

$CallSign = strtoupper($CallSign); #force uppercase callsign

If strlen($Username) {    ## or use: if (! empty($Username))
   $strBuff = '';    ## Avoid an undefined variable Notice later
   for ($A = 0; $A < strlen($CallSign); $A++) { ## NOTE Start at Zero NOT One
       $B = ord(substr($CallSign, $A, 1));
       $B = $B + ord(substr($Username, ($A % strlen($Username)) + 1, 1));    ## % is Mod
       $strBuff = $strBuff . Chr($B & &HFF);    ## NOTE concatenation is . (dot) // & is bitwise And
   }
} Else {
   $strBuff = $CallSign;
} 

 

That could be optimized a bit, but I wanted to keep it as close to the original code as possible so you can see the differences. I added comments about some significant points

Link to comment
Share on other sites

Thanks very much! But I am getting the following error:

 

Parse error: syntax error, unexpected '&' in encrypt.php on line 22

 

which is this line:

 

$strBuff = $strBuff . Chr($B & &HFF);

 

 

EDIT: Found the problem. Apparently &HFF isn't legal syntax in PHP, so I changed the line to:

 

$strBuff = $strBuff . Chr($B & 255);

 

and it's good

Edited by KenHorse
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.