[Perl] From one language to another... - 11/23/2003 11:38 PM
Another one.
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 ). 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:
I'd like to translate this into Perl, because it's extremely useful to me.
This is what I have so far:
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?
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 ). 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
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++;
}
Could someone with some VB/Perl knowledge throw me a hint please?