Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Port VB6 string functions to PHP

Hi all, I need some help porting a some VB 6 code into PHP.  Below is the VB 6 code followed by my attempt so far, although my attempt is creating a serial it's not one that's compatible with the VB 6 check routine.  Any help is greatly appreciated...

Note: I know I should use % rather than my method however that didn't work for some reason, probably me using it wrong.

Original VB6 code by Jhnet Computers
VB6 Code --------------------------------------------

Private Function getPlusMinus(chrr) As Boolean ' <<< This function retunrs either true or false
chrr = UCase(chrr)                             '     depending on if a charachter is more than
                                               '     halfway through the alphabet or not...
If Asc(chrr) - 65 < 12 Then
    getPlusMinus = True
Else
    getPlusMinus = False
End If
End Function

Public Function genNumber(appName)
On Error Resume Next
Dim appVal As Long
Dim genVal As Long
Dim tmpVar As String
Dim i As Integer
Dim seedMod As Integer

For i = 1 To Len(appName) - 0
    appVal = appVal + Val(Asc(Mid$(appName, i, 1))) ' <<< Counts the value of each ascii chr
Next                                                '     in the app name
seedMod = Int((Day(Date) & Month(Date) & Year(Date) & Hour(Time) & Minute(Time) & Second(Time)) ^ 0.2)

For i = 0 To Int(seedMod + Minute(Time) & Second(Time)) ' <<< Vb's random num generator is not
    Rnd                                                 '     very random so i will make it more
Next                                                    '     random

tmpVar = ""
For i = 1 To 20                                   ' <<< Randomly create the 1st 4 parts of the code
    If Rnd < 0.5 Then                             ' <<< 1 in two chance of a letter or a number
        tmpVar = tmpVar & Chr(Int(Rnd * 25) + 65)
    Else
        tmpVar = tmpVar & Int(Rnd * 9)
    End If
   
    If Int(i / 5) = i / 5 And i <> 25 Then    ' <<< Add a ' - ' every 5 charachters
        tmpVar = tmpVar & " - "
    End If
Next

For i = 1 To Len(tmpVar) - 0                              ' <<< Creates a number based on the
    If i < Len(appName) Then                              '     first sections. Adds or takes
        If getPlusMinus(Mid(appName, i, 1)) = False Then  '     depending on various things
            genVal = genVal + Val(Asc(Mid$(tmpVar, i, 1))) '    Makes it mathematicaly harder
        Else                                              '     to re-order the code.
            genVal = genVal - Val(Asc(Mid$(tmpVar, i, 1)))
        End If
    Else
        If Int(i / 2) = i / 2 Then
            genVal = genVal - Val(Asc(Mid$(tmpVar, i, 1)))
        Else
            genVal = genVal + Val(Asc(Mid$(tmpVar, i, 1)))
        End If
    End If
Next
If genVal < 0 Then genVal = 0 - genVal      ' <<< If the number is less than 0 then make it
                                            '     positive

tmpVar = tmpVar & Mid((genVal * appVal) & "JSDEU", 1, 5) ' <<< Last part of the code is the
                                                         '     'value' of the first part of
                                                         '     the code times the 'value'
                                                         '     of the program name, limited
                                                         '     to 5 charachters. "JSDEU" is
                                                         '     to make sure the result is
                                                         '     atleast 5 chars.

genNumber = UCase(tmpVar)    ' <<< Returns the new key
End Function


Public Function authKey(key, appName) As Boolean
authKey = False
On Error GoTo err

Dim splt() As String
Dim appVal As Long
Dim genVal As Long
Dim tempVar As String
Dim i As Integer
key = UCase(key)

For i = 1 To Len(appName) - 0
    appVal = appVal + Val(Asc(Mid$(appName, i, 1)))
Next

splt = Split(key, " - ")
splt(4) = ""

tempVar = Join(splt, " - ")

For i = 1 To Len(tempVar) - 0
    If i < Len(appName) Then
        If getPlusMinus(Mid(appName, i, 1)) = False Then
            genVal = genVal + Val(Asc(Mid$(tempVar, i, 1)))
        Else
            genVal = genVal - Val(Asc(Mid$(tempVar, i, 1)))
        End If
    Else
        If Int(i / 2) = i / 2 Then
            genVal = genVal - Val(Asc(Mid$(tempVar, i, 1)))
        Else
            genVal = genVal + Val(Asc(Mid$(tempVar, i, 1)))
        End If
    End If
Next
If genVal < 0 Then genVal = 0 - genVal

splt = Split(key, " - ")

If genVal = Val(splt(4)) / appVal Then
    authKey = True
Else
    authKey = False
End If


Debug.Print Mid((appVal * genVal) & "JSDEU", 1, 5)
Debug.Print splt(4)

If Mid((appVal * genVal) & "JSDEU", 1, 5) = splt(4) Then
    authKey = True
Else
    authKey = False
End If

err:

End Function

Private Sub cmdCheck_Click()
    If authKey(txtSerial2.Text, txtName2.Text) = True Then
        lblResult.Caption = "Result: Valid"
    Else
        lblResult.Caption = "Result: Invalid"
    End If
End Sub

'DEMO SUB PROCEDURE
Private Sub cmdGenerate_Click()
    Dim sName, sNumber As String
    sName = txtName.Text
    sNumber = genNumber(sName)
    txtSerial.Text = sNumber
End Sub

My PHP Code ----------------------------------------------------------
<?

function getPlusMinus($chrr) {
  $chrr = strtoupper($chrr);

  $tmp = ord($chrr);
  $tmp = $tmp - 65;

  if ($tmp < 12) {
    return 1;
  } else {
    return 0;
  }
}

// Floating point random number function
// April 2007, René W. Feuerlein
function fprand($intMin,$intMax,$intDecimals) {
  if($intDecimals) {
    $intPowerTen=pow(10,$intDecimals);
    return rand($intMin*$intPowerTen,$intMax*$intPowerTen)/$intPowerTen;
  }
  else
    return rand($intMin,$intMax);
}

function genNumber($appName) {
  $appVal = 0;
  $genVal = 0;
  $tmpVar = '';
  $i = 0;
  $seedMod = 0;

  for ($i = 0; $i < strlen($appName); $i++) {
    $appVal = $appVal + ord(substr($appName, $i, 1));
  }

  $tmpVar = '';
  $j = 0;
  for ($i = 1; $i <= 20; $i++) {
    $j++;
    if (fprand(0,1,1) < 0.5) {
      $tmpVar = $tmpVar . chr(intval(fprand(0,1,1) * 25) + 65);
    } else {
      $tmpVar = $tmpVar . intval(fprand(0,9,0));
    }

    if ($j == 5) {
      $tmpVar = $tmpVar . ' - ';
      $j = 0;
    }
  }

  $j = 0;
  for ($i = 0; $i < strlen($tmpVar); $i++) {
    $j++;
    if ($i < (strlen($appName) - 1)) {
      if (getPlusMinus(substr($tmpVar, $i, 1)) == 0) {
        $genVal = $genVal + ord(substr($tmpVar, $i, 1));
      } else {
        $genVal = $genVal - ord(substr($tmpVar, $i, 1));
      }
    } else {
      if ($j == 2) {
        $genVal = $genVal - ord(substr($tmpVar, $i, 1));
      } else {
        $genVal = $genVal + ord(substr($tmpVar, $i, 1));
      }
    }
    if ($j == 2) { $j = 0; }
  }

  if ($genVal < 0) { $genVal = 0 - $genVal; }

  $tmpVar = $tmpVar . substr(($genVal * $appVal) . "JSDEU",0,5);

  return strtoupper($tmpVar);
}

echo genNumber("Some String");
?>

Many thanks in advance.

Matt
0
MattJellings
Asked:
MattJellings
1 Solution
 
rhyspCommented:
Just a small typo bug. I found it by following the value throughout the generation and suppling a set random number so I could find where the two programs differed.

This brought to light the problem with the second line shown below:
  if ($i < (strlen($appName) - 1)) {
      if (getPlusMinus(substr($tmpVar, $i, 1)) == 0) {

It should be:
  if ($i < (strlen($appName) - 1)) {
      if (getPlusMinus(substr($appName, $i, 1)) == 0) {

Full code below:

<?php
header("Content-Type: text/plain");
function getPlusMinus($chrr) {
  $chrr = strtoupper($chrr);

  $tmp = ord($chrr);
  $tmp = $tmp - 65;

  if ($tmp < 12) {
    return 1;
  } else {
    return 0;
  }
}

// Floating point random number function
// April 2007, René W. Feuerlein
function fprand($intMin,$intMax,$intDecimals) {
  if($intDecimals) {
    $intPowerTen=pow(10,$intDecimals);
    return rand($intMin*$intPowerTen,$intMax*$intPowerTen)/$intPowerTen;
  } else {
    return rand($intMin,$intMax);
  }
}

function genNumber($appName) {
  $appVal = 0;
  $genVal = 0;
  $tmpVar = '';
  $i = 0;
  $seedMod = 0;

  for ($i = 0; $i < strlen($appName); $i++) {
    $appVal = $appVal + ord(substr($appName, $i, 1));
  }

  $tmpVar = '';
  $j = 0;
  for ($i = 1; $i <= 20; $i++) {
    $j++;
    if (fprand(0,1,1) < 0.5) {
      $tmpVar = $tmpVar . chr(intval(fprand(0,1,1) * 25) + 65);
    } else {
      $tmpVar = $tmpVar . intval(fprand(0,9,0));
    }

    if ($j == 5) {
      $tmpVar = $tmpVar . ' - ';
      $j = 0;
    }
  }
//echo "tmpVar = [".$tmpVar."]\n";
//$tmpVar = "FKZWC - 666A0 - 25U44 - 2ZKAM - ";
  $j = 0;
  for ($i = 0; $i < strlen($tmpVar); $i++) {
    $j++;
    if ($i < (strlen($appName) - 1)) {
      if (getPlusMinus(substr($appName, $i, 1)) == 0) {
        $genVal = $genVal + ord(substr($tmpVar, $i, 1));
      } else {
        $genVal = $genVal - ord(substr($tmpVar, $i, 1));
      }
    } else {
      if ($j == 2) {
        $genVal = $genVal - ord(substr($tmpVar, $i, 1));
      } else {
        $genVal = $genVal + ord(substr($tmpVar, $i, 1));
      }
    }
    if ($j == 2) { $j = 0; }
  }
//echo "genVal = [".$genVal."]\n";
  if ($genVal < 0) { $genVal = 0 - $genVal; }

  $tmpVar = $tmpVar . substr(($genVal * $appVal) . "JSDEU",0,5);

  return strtoupper($tmpVar);
}

echo genNumber("Some String");
?>

It worked for me, but let me know how you go.
0
 
MattJellingsAuthor Commented:
brilliant thank you, I had a feeling it was down to something simple as like I say it was generating a serial however just not a matching one.  Changing that and testing it works fine now, it creates a serial that the VB code recognises and validates.

Thanks for the prompt reply :)

Matt
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now