php forum
php mysql forum
php mysql smarty
 
Topic Options
#104965 - 11/23/03 03:38 PM [Perl] From one language to another...
CTM Offline
Moderator / Da Masta

Registered: 11/23/01
Posts: 2567
Another one. smile

I'm trying to incorporate some kind of license protection into one of my scripts (and teach myself something extra about Perl that's worthwhile smile ). I've had some related VB code on my hard drive for ages that creates a 25-digit alphanumeric license key based on the "program keys", a set of keys you give to the program.

Here is the VB code:

Code:
' 32 Numerics and alphas - we are missing I, O, S and Z not just because
' we only want 32 characters but also because I could be mistaken for
' the number 1, O for 0, S for 5 and Z for 2.
Private Const VALID_CHARS As String = "0123456789ABCDEFGHJKLMNPQRTUVWXY"

Private Const RANDOM_LOWER As Long = 0
Private Const RANDOM_UPPER As Long = 31

'*******************************************************************************
' GenerateKey (FUNCTION)
'
' PARAMETERS:
' (In/Out) - sAppChars - String - Application specific characters to be used
'                                 during the MD5 operation.
'
' RETURN VALUE:
' String - The key
'
' DESCRIPTION:
' Generates a random key by first selecting 9 random characters from our 32
' valid characters, adding our application specific characters, creating an
' MD5 digest, and using the digest to select the other 16 characters for
' our key.
'*******************************************************************************
Public Function GenerateKey(sAppChars As String) As String
    Dim lChar           As Long
    Dim lCount          As Long
    Dim sInitialChars   As String
    Dim oMD5            As CMD5
    Dim sMD5            As String
    Dim sKey            As String
    
    Randomize
    
    ' We first generate 9 random characters that are members of VALID_CHARS
    sInitialChars = ""
    For lCount = 1 To 9
        lChar = Int((RANDOM_UPPER - RANDOM_LOWER + 1) * Rnd + RANDOM_LOWER)
        sInitialChars = sInitialChars & Mid(VALID_CHARS, lChar + 1, 1)
    Next
    
    ' We now get an MD5 of our initial chars plus out application chars
    ' The application chars should be different for each application to
    ' ensure that a key for one of our applications is not valid on another
    ' of our applications. If hackers know we are using this method for
    ' generating our keys we should ensure that the application characters
    ' are very long to help prevent cracking.
    Set oMD5 = New CMD5
    sMD5 = oMD5.MD5(sInitialChars & sAppChars)
    Set oMD5 = Nothing
    
    ' We now take each byte-pair from the MD5, convert it back to a byte
    ' value from the hex code, do a MOD 32, and then select the appropriate
    ' character from our VALID_CHARS
    sKey = sInitialChars
    
    For lCount = 1 To 16
        lChar = CLng("&H" & Mid(sMD5, (lCount * 2) - 1, 2)) Mod 32
        sKey = sKey & Mid(VALID_CHARS, lChar + 1, 1)
    Next
    
    GenerateKey = sKey
End Function
I'd like to translate this into Perl, because it's extremely useful to me.

This is what I have so far:

Code:
use CGI qw(:standard);
use Digest::MD5 qw(md5 md5_hex md5_base64);

$passphrase = "WRDCT";
$numkeys = 1;

$i = 1;

while ($i <= $numkeys) {
	@digit = qw(0 1 2 3 4 5 6 7 8 9 A B C D E F G H J K L M N P Q R T U V W X Y);

	# Generate 9 random @digits
	srand;
	$randchars = join('', map{ $digit[rand(@digit)] }(1 .. 9) );
	print "$randcharsn";

	# Add the passphrase to the random characters
	$randchars .= $passphrase;
	print "$randcharsn";

	# MD5-encrypt all of it
	$hash = md5_hex($randchars);
	print "$hashn";

	# Take each byte-pair from the MD5, convert it back to a byte
     	# value from the hex code, do a % 32, and then select the appropriate
   	# character from @digits
	$finalkey = $randchars;

	my $lcount;
	for ($lcount; $lcount <= 16; $lcount++) {
		my $lchar = hex(substr($hash, ($lcount * 2) - 1, 2)) % 32;
		$finalkey .= $digits[$lchar + 1];
	}

	$i++;
}
Everything works great in the Perl script down to the for block, where it goes... not quite so well. It's clearly a problem with my for loop.

Could someone with some VB/Perl knowledge throw me a hint please? smile

Top
#104966 - 11/23/03 04:42 PM Re: [Perl] From one language to another...
Burak Offline
Addict

Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
So, will you distribute your code? If yes, thats unnecessary. If you're also looking for some compiled distribution look at perl2exe from indigostar and PerlApp from ActiveState. You can also try PAR ...

Also, if I were you, I wouldnt try to convert from such a silly language wink
_________________________
one bytecode to rule them all

Top
#104967 - 11/23/03 06:31 PM Re: [Perl] From one language to another...
CTM Offline
Moderator / Da Masta

Registered: 11/23/01
Posts: 2567
I'm not planning on distributing the code, just using it as a check in the background. The user supplies a license key and the script processes it using the algorithm. This script generates the keys. smile

And this is exactly the reason why I'm converting from VB to Perl, cause VB is such a silly language... Some of its developers do have brains though tipsy

Top
#104968 - 04/12/06 06:50 AM Re: [Perl] From one language to another...
Nayeem Offline
Junior Member

Registered: 04/12/06
Posts: 1
can you convert this vb functions to perl lang

Public Function EncodeArabic(ByVal arabicString As String) As String
Dim _targetEncoding As System.Text.Encoding
Dim _encodedCharsArray() As Byte
Dim _resultString As String = String.Empty
'1256 is the code page for windows arabic

' 1201 is the code page of Unicode big endian
_targetEncoding = System.Text.Encoding.GetEncoding(1201)
_encodedCharsArray = _targetEncoding.GetBytes(arabicString)
'_resultString = ByteToHexadecimal(_encodedCharsArray)
_resultString = ByteToHexadecimal(_encodedCharsArray)
Return _resultString
End Function
Public Function ByteToHexadecimal(ByVal imageByteArray() As Byte) As String

Dim hexString As String = String.Empty

Dim i As Integer
For i = 0 To imageByteArray.Length - 1 Step i + 1

hexString += imageByteArray(i).ToString("x2") 'it was X2

Next

Return hexString

End Function

Top
#104969 - 04/12/06 08:01 AM Re: [Perl] From one language to another...
Burak Offline
Addict

Registered: 05/29/00
Posts: 1820
Loc: Istanbul, Turkey
This is .NET framework code. If you want to use it with pure perl, that's impossible. You have to use ActiveState' s PerlNET plugin.
_________________________
one bytecode to rule them all

Top


Who's Online
0 registered (), 21 Guests and 10 Spiders online.
Key: Admin, Global Mod, Mod
Shout Box

Latest Posts
Wisdom needed
by Gizmo
Yesterday at 10:54 AM
How to hide sub forums from summary page
by blaaskaak
12/03/08 09:54 AM
Spell Check [beta]
by Bill B
12/01/08 09:16 PM
PhotoPost BB Code Popup
by AllenAyres
12/01/08 09:41 AM
Problems reading a lot of old posts here
by AllenAyres
12/01/08 09:35 AM
Forum 'Trader Ratings'.
by AllenAyres
12/01/08 09:33 AM
Customization needed
by Gizmo
11/12/08 12:28 PM
New Mods
User Authentication Class
by
01/19/07 02:59 PM
Multiple Identity Detector
by
12/30/06 06:39 PM
PhotoPost BB Code Popup
by
11/06/06 05:43 PM
Spell Check [beta]
by
10/17/06 09:24 PM
Newest Members
Truth, David DelMonte, nick1, Begbie, cenk
13364 Registered Users
Top Posters
AllenAyres 25452
JoshPet 11330
Rick 8372
LK 7396
Lord Dexter 6503
Greg Hard 5533
Charles Capps 5438

 

 

 
fusionbb message board php hacks