|
-
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.
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
|