KenHorse Posted December 2, 2012 Share Posted December 2, 2012 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 Link to comment https://forums.phpfreaks.com/topic/271494-converting-a-block-of-vb6-to-php/ Share on other sites More sharing options...
DavidAM Posted December 2, 2012 Share Posted December 2, 2012 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 https://forums.phpfreaks.com/topic/271494-converting-a-block-of-vb6-to-php/#findComment-1397012 Share on other sites More sharing options...
KenHorse Posted December 2, 2012 Author Share Posted December 2, 2012 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 Link to comment https://forums.phpfreaks.com/topic/271494-converting-a-block-of-vb6-to-php/#findComment-1397015 Share on other sites More sharing options...
DavidAM Posted December 2, 2012 Share Posted December 2, 2012 Sorry, you're right. It should be $strBuff = $strBuff . Chr($B & 0xFF); Link to comment https://forums.phpfreaks.com/topic/271494-converting-a-block-of-vb6-to-php/#findComment-1397019 Share on other sites More sharing options...
KenHorse Posted December 2, 2012 Author Share Posted December 2, 2012 Sorry, you're right. It should be $strBuff = $strBuff . Chr($B & 0xFF); No problem. I really appeciate your help Link to comment https://forums.phpfreaks.com/topic/271494-converting-a-block-of-vb6-to-php/#findComment-1397023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.