UBB.Dev
Posted By: CTM [Perl] From one language to another... - 11/23/2003 11:38 PM
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
Posted By: Burak Re: [Perl] From one language to another... - 11/24/2003 12:42 AM
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
Posted By: CTM Re: [Perl] From one language to another... - 11/24/2003 2:31 AM
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
Posted By: Nayeem Re: [Perl] From one language to another... - 04/12/2006 1:50 PM
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
Posted By: Burak Re: [Perl] From one language to another... - 04/12/2006 3:01 PM
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.
© UBB.Developers