Solved

BIT Operators VB6 and PHP

Posted on 2011-09-27
6
475 Views
Last Modified: 2013-12-13
Following on from a related question at http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Windows/Q_27303952.html

I have now a simple (not required to be 'tricky' or foolproof) key creator that I've put together in VB6 and have been using on my software for a number of years now. I have been trying to simulate that algorithm in PHP to generate the same 'key' so users can generate the access key online, instead of having to ring me.

The trick I'm finding is the codes are producing different keys. This may be related to how PHP and VB6 handle Bit Operators.

I've attached the code on both PHP and VB6.

Given an input string of say "1234-123-123-123JOE BLOW" the output in VB6 is 03FCGGFB, whilst the PHP output is 0017GD02

I've played a bit with the PHP code to try different things.

Any ideas would be great.
Public Function gencheck(sstring As String) As String
   
    Dim I As Integer
    Dim chk As Long
    Dim n1, n2, n3, n4, n5, n6, n7, n8 As Byte
    
    chk = 0
    For I = 1 To Len(sstring)
        chk = chk * 2
        
        ' Set bit 0 to whatever bit 16 is..
        If (chk And 65536) = 0 Then
            ' Bit 16 is clear, so clear bit 0
            chk = chk And 65534
        Else
            ' Bit 16 is set, so set bit 0
            chk = chk Or 1
        End If
        ' Clear bit 16..
        chk = chk And 65535
        
        ' XOR sum
        chk = chk Xor Asc(Mid$(sstring, I, 1))
    Next I
    
   
    ' We have the additive checksum, so now get the 4 nybbles..
    n8 = chk And 15
    chk = Int(chk / 4)
    n7 = chk And 15
    chk = Int(chk / 4)
    n6 = chk And 15
    chk = Int(chk / 4)
    n5 = chk And 15
    chk = Int(chk / 4)
    n4 = chk And 15
    chk = Int(chk / 4)
    n3 = chk And 15
    chk = Int(chk / 4)
    n2 = chk And 15
    chk = Int(chk / 4)
    n1 = chk And 15
    
    ' Add 48 to each one (ascii '0')
    n1 = n1 + 48
    
    If n1 > 57 Then
        n1 = 65 + (n1 - 57)
    End If
    
    n2 = n2 + 48
    
    If n2 > 57 Then
        n2 = 65 + (n2 - 57)
    End If
    
    n3 = n3 + 48
    
    If n3 > 57 Then
        n3 = 65 + (n3 - 57)
    End If
    
    n4 = n4 + 48
    If n4 > 57 Then
        n4 = 65 + (n4 - 57)
    End If
    
    n5 = n5 + 48
    If n5 > 57 Then
        n5 = 65 + (n5 - 57)
    End If
    
    n6 = n6 + 48
    If n6 > 57 Then
        n6 = 65 + (n6 - 57)
    End If
    
    n7 = n7 + 48
    If n7 > 57 Then
        n7 = 65 + (n7 - 57)
    End If
    
    n8 = n8 + 48
    If n8 > 57 Then
        n8 = 65 + (n8 - 57)
    End If
       
    gencheck = Chr$(n1) + Chr$(n2) + Chr$(n3) + Chr$(n4) + Chr$(n5) + Chr$(n6) + Chr$(n7) + Chr$(n8)
End Function

Open in new window

function getchecksum($sstring){
	    $chk = 0;
	    $len = strlen($sstring);	
	    for ($I=1; $I<=$len; $I++){
	        $chk = $chk * 2;
	        // Set bit 0 to whatever bit 16 is..
	        If (($chk & 65536) == 0 ){
	            // Bit 16 is clear, so clear bit 0
	            $chk = ($chk & 65534);
	        } else {
	            // Bit 16 is set, so set bit 0
	            $chk = ($chk Or 1);
	        }
	        // Clear bit 16..
	        $chk = ($chk & 65535);
	        
	        // XOR sum
	        $chk = ($chk ^ Ord(substr($sstring, $I, 1)));
	    }
	    
	    $n8 = ($chk & 15);
	    $chk = intval($chk / 4);
	    $n7 = ($chk & 15);
	    $chk = intval($chk / 4);
	    $n6 = ($chk & 15);
	    $chk = intval($chk / 4);
	    $n5 = ($chk & 15);
	    $chk = intval($chk / 4);
	    $n4 = ($chk & 15);
	    $chk = intval($chk / 4);
	    $n3 = ($chk & 15);
	    $chk = intval($chk / 4);
	    $n2 = ($chk & 15);
	    $chk = intval($chk / 4);
	    $n1 = ($chk & 15);
	    
	    // Add 48 to each one (ascii '0')
	    $n1 = $n1 + 48;
	    
	    If ( $n1 > 57 ){
	        $n1 = 65 + ($n1 - 57);
	    }
	    
	    $n2 = $n2 + 48;
	    
	    If ( $n2 > 57 ){
	        $n2 = 65 + ($n2 - 57);
	    }
	    
	    $n3 = $n3 + 48;
	    
	    If ( $n3 > 57 ){
	       $n3 = 65 + ($n3 - 57);
	    }
	    
	    $n4 = $n4 + 48;
	    If ($n4 > 57){
	        $n4 = 65 + ($n4 - 57);
	    }
	    
	    $n5 = $n5 + 48;
	    If ($n5 > 57){
	        $n5 = 65 + ($n5 - 57);
	    }
	    
	    $n6 = $n6 + 48;
	    If ($n6 > 57){
	        $n6 = 65 + ($n6 - 57);
	    }
	    
	    $n7 = $n7 + 48;
	    If ($n7 > 57){
	        $n7 = 65 + ($n7 - 57);
	    }
	    
	    $n8 = $n8 + 48;
	    If ($n8 > 57 ){
	        $n8 = 65 + ($n8 - 57);
	    }

	    return chr($n1).chr($n2).chr($n3).chr($n4).chr($n5).chr($n6).chr($n7).chr($n8);
	}

Open in new window

0
Comment
Question by:pnclick
  • 4
  • 2
6 Comments
 

Author Comment

by:pnclick
ID: 36713766
That string input should read: 1234-1234-1234-1234JOE BLOW
0
 
LVL 11

Expert Comment

by:kbirecki
ID: 36714060
Bingo!  I got it.  One moment while I put some clean docs together.  The PHP script needs two  adjustments.
0
 
LVL 11

Expert Comment

by:kbirecki
ID: 36714156
OK, I created a spreadsheet with the analysis I did (cleaned up from my initial work.)  It seemed like an easy way to organize the results to easily see how I identified the two issues with the PHP script, and provide an easy way for you to reproduce my analysis.

See the attached spreadsheet for the details, the first tab is especially enlightening, but, in summary....

In your question above:

1) line 18 uses substr() which uses a 0-based string reference, but $i is counting 1-based, so it was picking up the wrong character in your string.

2) line 12 uses "Or" operator and it should be "|" in PHP.  (Not sure why "Or" worked at all.  It's probably applicable in some situation.)
ChecksumFunctionsAnalysis.xls
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Accepted Solution

by:
kbirecki earned 500 total points
ID: 36714175
I left an intermediate PHP test harness in the spreadsheet.  Worksheet "PHP Test harness" should be the following to see all the results I put together.

<?PHP
require_once("getchecksum.php");

echo "Original Result = " . getchecksum_orig("1234-1234-1234-1234JOE BLOW")."<p>";

echo "*************************************************************************************<br>";
echo "*************************************************************************************<br>";
echo "*************************************************************************************<br>";
echo "*************************************************************************************<p>";

echo "Rev 1 Result = " . getchecksum_rev1("1234-1234-1234-1234JOE BLOW")."<p>";

echo "*************************************************************************************<br>";
echo "*************************************************************************************<br>";
echo "*************************************************************************************<br>";
echo "*************************************************************************************<p>";

echo "Rev 2 Result = " . getchecksum_rev2("1234-1234-1234-1234JOE BLOW")."<p>";


?>

Open in new window

0
 

Author Closing Comment

by:pnclick
ID: 36714389
Thats is absolutely brilliant.

Thank you kbirecki, especially for the spreadsheet analysis.

As you may have gathered, I'm running abit blind on PHP, having spent so long developing using VB6, however am enjoying the learning curve in moving over to PHP.
0
 
LVL 11

Expert Comment

by:kbirecki
ID: 36715813
Happy to help.  I'm doing the same.  Next up: C#!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now