Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: Nov 2001
Posts: 1,704
CTM
Offline
Moderator / Da Masta
Moderator / Da Masta
Offline
Joined: Nov 2001
Posts: 1,704
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

Sponsored Links
Entire Thread
Subject Posted By Posted
[Perl] From one language to another... CTM 11/23/2003 11:38 PM
Re: [Perl] From one language to another... Burak 11/24/2003 12:42 AM
Re: [Perl] From one language to another... CTM 11/24/2003 2:31 AM
Re: [Perl] From one language to another... Nayeem 04/12/2006 1:50 PM
Re: [Perl] From one language to another... Burak 04/12/2006 3:01 PM

Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
Gizmo
Gizmo
Portland, OR, USA
Posts: 5,833
Joined: January 2000
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)