-
May 1st, 2007, 12:01 PM
#1
Simple License Encryption Class
Ok, I wrote this from a php encryption function that I had. It takes a PassKey and will encrypt it with a random value each time. It is a very simple encryption but for my uses it seemed fine. I thought I would share it. I will include both the vb php versions.
VB Version
vb.net Code:
Public Class License
#Region " VARIABLES "
Private strcode As String() = {"", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
Private _PassKey As String
#End Region
#Region " ENUM "
Public Enum ValidationResult
Trial
Valid
Invalid
End Enum
#End Region
#Region " PROPERTIES "
Public Property PassKey() As String
Get
Return _PassKey
End Get
Set(ByVal value As String)
_PassKey = value.Replace(" ",String.Empty).ToUpper
End Set
End Property
#End Region
#Region " PUBLIC FUNCTIONS "
Public Function GenerateRandomPassKey() As String
Dim rand As New Random
Dim passKey As String = String.Empty
For i As Integer = 0 To 22
Dim r As Integer = rand.Next(48, 90)
Do Until r < 58 OrElse r > 64
r = rand.Next(48, 90)
Loop
passKey &= Chr(r)
Next
Return passKey
End Function
Public Function GenerateKey() As String
If _PassKey = String.Empty Then
Return "Please Enter Passkey or generate a random passkey. The longer the passkey, the greater the security."
End If
Dim nstr As String() = strsplt(_PassKey, 2)
Dim output As String = String.Empty
Dim tmp As String = String.Empty
For i As Integer = 0 To nstr.GetUpperBound(0)
tmp &= convert_keyto_value(nstr(i))
Next
For i As Integer = 0 To tmp.Length - 1
output &= tmp.Substring(i, 1)
If i > 0 AndAlso i <> tmp.Length - 1 AndAlso (i + 1) Mod 5 = 0 Then
output &= "-"
End If
Next
Return output
End Function
Public Function ValidateKey(ByVal thetext As String) As Boolean
thetext = thetext.Replace("-"c, String.Empty)
Dim nstr As String() = strsplt(thetext, 3)
Dim output As String = String.Empty
For i As Integer = 0 To nstr.GetUpperBound(0)
output &= deRandomize(nstr(i))
Next
If output = _PassKey Then
Return True
Else
Return False
End If
End Function
#End Region
#Region " PRIVATE FUNCTIONS "
Private Function strsplt(ByVal thetext As String, Optional ByVal num As Integer = 1) As String()
Dim arr As New List(Of String)
Dim i As Integer
Dim y As String
For i = 0 To thetext.Length - 1 Step num
If i + num > thetext.Length - 1 Then
y = thetext.Substring(i, (thetext.Length - i))
Else
y = thetext.Substring(i, num)
End If
arr.Add(y)
Next
Return arr.ToArray
End Function
Private Function key_locator(ByVal code As String) As Integer
For i As Integer = 0 To strcode.GetUpperBound(0)
If strcode(i) = code Then
Return i
End If
Next
Return Nothing
End Function
Private Function add_random_key(ByVal thetext As String) As Integer()
Dim newcode As New List(Of Integer)
Dim rnd As New Random()
Dim r As Integer = rnd.Next(1, strcode.Length - 2)
Dim x As Integer
Dim tmp As Integer
For i As Integer = 0 To thetext.Length - 1
x = key_locator(thetext.Substring(i, 1))
tmp = x + r
If tmp > strcode.Length - 1 Then
tmp = tmp - (strcode.Length - 1)
End If
newcode.Add(tmp)
Next
newcode.Add(r)
Return newcode.ToArray
End Function
Private Function convert_keyto_value(ByVal thetext As String) As String
Dim a As Integer() = add_random_key(thetext)
Dim strReturn As String = String.Empty
Do Until Array.IndexOf(a, strcode.Length) = -1
a = add_random_key(thetext)
Loop
For i As Integer = 0 To a.GetUpperBound(0)
strReturn &= strcode(a(i))
Next
Return strReturn
End Function
Private Function deRandomize(ByVal thetext As String) As String
Dim arr As String() = strsplt(thetext, thetext.Length - 1)
Dim s, t, newcode As Integer
Dim output As String = String.Empty
For x As Integer = 0 To thetext.Length - 2
s = key_locator(arr(0).Substring(x, 1))
t = key_locator(arr(1))
newcode = s - t
If newcode < 0 Then
newcode += (strcode.Length - 1)
End If
If newcode = 0 And s <> 0 Then
newcode = (strcode.Length - 1)
End If
output &= strcode(newcode)
Next
Return output
End Function
#End Region
End Class
Usage
You can either select your own passkey to use for this or you can generate a random passkey. The random Passkey generates a random 23 character string.
Creating Licenses
vb.net Code:
Dim Lic As New License
Dim pKey As String = lic.GenerateRandomPassKey 'You need to save this key because it is used to validate any licenses you create
lic.PassKey = pKey
Dim LicenseKey As String = lic.GenerateKey
Validating Licenses
vb.net Code:
Dim Lic As New License
Lic.PassKey = YOUR_PASSKEY
Dim isValid As Boolean = lic.ValidateKey("THEY KEY TO VALIDATE")
The PHP File
PHP Code:
<?php
$strcode=array('','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
function strsplt($thetext,$num=1){
$arr=array();
while ($i<strlen($thetext)){
$y=substr($thetext,$i,$num);
if (isset($y)){
$arr[]=$y;
}
$i=$i+$num;
}
return $arr;
}
function key_locator($code,$strcode){
while (list($key, $val) = each($strcode)) {
if ($val==$code){
$x=$key;
}
}
return $x;
}
function add_random_key($thetext){
global $strcode;
$newcode=array();
$rnd=rand(1,intval(count($strcode))-2);
for ($i=0;$i<strlen($thetext);$i++){
$x=key_locator(substr($thetext,$i,1),$strcode);
$temp=$x+$rnd;
if($temp>intval(count($strcode)-1)){
$temp=$temp-intval(count($strcode)-1);
}
$newcode[]=$temp;
$temp="";
}
$newcode[]=$rnd;
return $newcode;
}
function convert_keyto_value($thetext){
global $strcode;
$a=add_random_key($thetext);
while (in_array(count($strcode)-1,$a)==true){
$a=add_random_key($thetext);
}
for ($i=0;$i<strlen($thetext)+1;$i++){
$output.=$strcode[$a[$i]];
}
return $output;
}
function derandomized($thetext){
global $strcode;;
$arr=strsplt($thetext,intval(strlen($thetext)-1));
for ($x=0;$x<strlen($thetext)-1;$x++){
$s=key_locator(substr($arr[0],$x,1),$strcode);
$t=key_locator($arr[1],$strcode);
$newcode=$s-$t;
if ($newcode<0){
$newcode=$newcode+intval(count($strcode)-1);
}
if ($newcode==0&&$s<>0){
$newcode=count($strcode)-1;
}
$output.=$strcode[$newcode];
}
return $output;
}
function encrypt($thetext){
global $strcode;;
$nstr=strsplt($thetext,2);
for ($i=0;$i<count($nstr);$i++){
$output.=convert_keyto_value($nstr[$i]);
}
return $output;
}
function decrypt($thetext){
global $strcode;;
$nstr=strsplt($thetext,3);
for ($i=0;$i<count($nstr);$i++){
$output.=derandomized($nstr[$i]);
}
return $output;
}
?>
Last edited by bmahler; May 1st, 2007 at 12:07 PM.
-
May 30th, 2007, 10:18 AM
#2
Hyperactive Member
Re: Simple License Encryption Class
Hi bmahler
Very nice class and can be very useful. There is 1 limiation I can find, if you use any of these keys _-/~ etc.... you cannot validate the Lic Key.
CompanyName~LicKeyExpiry~NoLics etc....
Thanks for some great code
-
May 31st, 2007, 09:17 AM
#3
Re: Simple License Encryption Class
yes, it is limited to alphanumeric characters atm. But this is just something I made at work because I was bored. I recommend you use the random generated passkey and then just save it for the product if you plan on actually using it. I have some other stuff that I was working on that can create a GUID and store information under it like expiration date and such, but I had to get some real work done , this was just me messing around.
-
Jun 21st, 2007, 12:42 PM
#4
New Member
Re: Simple License Encryption Class
Very nice class, thank you...
One little 'error' or feature as Microsoft would call it...
If you try to verify a key of a different length from the original it will crap out in the strsplt function.
Easily controlled by checking the length of course
Johnny
-
Jul 7th, 2007, 12:59 PM
#5
Re: Simple License Encryption Class
For something like that I would recommend that you have a webservice that checks the key against a database. Sort of an activation. Have the key in the database and increment installs every time it is installed. Then just prevent it from activating if the maxed allowed have been reached.
-
Feb 18th, 2008, 06:08 PM
#6
PowerPoster
Re: Simple License Encryption Class
i was going to shell out $300 for a commercial licensing program. gonna give your code a shot.
-
Oct 16th, 2010, 07:29 PM
#7
New Member
Re: Simple License Encryption Class
Originally Posted by bmahler
For something like that I would recommend that you have a webservice that checks the key against a database. Sort of an activation. Have the key in the database and increment installs every time it is installed. Then just prevent it from activating if the maxed allowed have been reached.
How to do that I use VS2010 and I'm making an application that I want it to ask for license to work
and I already have my own hosting space
Thanks,
-
Mar 4th, 2018, 01:15 PM
#8
New Member
Re: Simple License Encryption Class
Hi! Thanks for this awesome code!
Where can i change the serial keys lenght? I dont want to create random-lenght key...
Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|