Solved

VB6 to PHP Code Checksum

Posted on 2011-09-12
6
620 Views
Last Modified: 2013-12-13
Hi,

I presently have a number of applications developed using VB6, that have a licence check attached to them based on a particular string, which is passed to a checksum function which returns an unlock access code.

Now I'm about to provide users with the ability to activate the applications online using the various inputs and what I must achieve is the same checksum access pin.

The VB6 code for the function is attached.

The website page is a PHP page, which i'm fairly new to, and i'm having difficulties in converting this particular critical function with byte variables and Xor operators from VB6 to PHP. The output string generally results in something like 3E696814 etc.. for the access pin.

Public Function getchecksum(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$(buf, I, 1))
    Next I
    
    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
    
    
    getchecksum = Chr$(n1) + Chr$(n2) + Chr$(n3) + Chr$(n4) + Chr$(n5) + Chr$(n6) + Chr$(n7) + Chr$(n8)
End Function

Open in new window

0
Comment
Question by:pnclick
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36525057
in PHP you use the ^ to represent Xor
Ord can be used for Asc
$chk = $chk ^ Ord(substr($buf,$I,$l))
0
 
LVL 14

Accepted Solution

by:
Brook Braswell earned 500 total points
ID: 36525187
You may have to massage this a bit but I think it will get you close to what you need.
<?php
function getchecksum($sstring){
    
    $chk = 0;
    for ($I=1; strlen($sstring); $I++){
        $chk = $chk * 2;
        // Set bit 0 to whatever bit 16 is..
        If (($chk And 65536) === 0 ){
            // 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);
        }
        // Clear bit 16..
        $chk = ($chk And 65535);
        
        // XOR sum
        $chk = ($chk ^ Ord(substr($buf, $I, 1)));
    }
    
    $n8 = ($chk And 15);
    $chk = intval($chk / 4);
    $n7 = ($chk And 15);
    $chk = intval($chk / 4)
    $n6 = ($chk And 15);
    $chk = intval($chk / 4)
    $n5 = ($chk And 15);
    $chk = intval($chk / 4)
    $n4 = ($chk And 15);
    $chk = intval($chk / 4)
    $n3 = ($chk And 15);
    $chk = intval($chk / 4)
    $n2 = ($chk And 15);
    $chk = intval($chk / 4)
    $n1 = ($chk And 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
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36525212
BTW Just noticed you var ( buf ) what is that from ?
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

 

Author Closing Comment

by:pnclick
ID: 36526015
Thanks heap Brook,

The Buf variable was actually a carry over from some testing, its is in fact the sstring variable.
0
 

Author Comment

by:pnclick
ID: 36720773
Just cleaning up the above answer that needed some 'massaging' so other users find it heapful.

The final code for the checksum using PHP which is now working had the following amendments.

Key area of cleanup was;

1. Line 8:   If (($chk And 65536) === 0 ) changed to If (($chk And 65536) == 0 )

2. Line 13:  $chk = ($chk Or 1); changed to $chk = ($chk | 1);

3. Line 19: $chk = ($chk ^ Ord(substr($buf, $I, 1))); chaned to $chk = ($chk ^ Ord(substr($buf, $I-1, 1)));
 
4. Various Lines: All the And operators changed to &

Thanks Brooke1966 for pointing me in the right direction.
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 | 1);
	        }
	        // Clear bit 16..
	        $chk = ($chk & 65535);
	        
	        // XOR sum
	        $chk = ($chk ^ Ord(substr($sstring, $I-1, 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
 

Author Comment

by:pnclick
ID: 36720782
Oh, and another quirky one was the original for ($I=1; strlen($sstring); $I++){ put the code into a constant loop for some reason, however when I declared the strlen() prior such as $len = strlen($sstring); and then used the for loop as for ($I=1; $I<=$len; $I++){ it worked.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

25 Experts available now in Live!

Get 1:1 Help Now