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
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
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

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

Joined: Apr 2006
Posts: 1
Junior Member
Junior Member
Offline
Joined: Apr 2006
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

Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
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.

Sponsored Links

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
Bill B
Bill B
Issaquah, WA
Posts: 87
Joined: December 2001
Forum Statistics
Forums63
Topics37,574
Posts293,926
Members13,850
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 20240506)