Results 1 to 8 of 8

Thread: Simple License Encryption Class

Threaded View

  1. #1

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    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:
    1. Public Class License
    2.  
    3. #Region " VARIABLES "
    4.     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"}
    5.     Private _PassKey As String
    6. #End Region
    7.  
    8. #Region " ENUM "
    9.     Public Enum ValidationResult
    10.         Trial
    11.         Valid
    12.         Invalid
    13.     End Enum
    14. #End Region
    15.  
    16. #Region " PROPERTIES "
    17.     Public Property PassKey() As String
    18.         Get
    19.             Return _PassKey
    20.         End Get
    21.         Set(ByVal value As String)
    22.             _PassKey = value.Replace(" ",String.Empty).ToUpper
    23.         End Set
    24.     End Property
    25. #End Region
    26.  
    27. #Region " PUBLIC FUNCTIONS "
    28.     Public Function GenerateRandomPassKey() As String
    29.         Dim rand As New Random
    30.         Dim passKey As String = String.Empty
    31.         For i As Integer = 0 To 22
    32.             Dim r As Integer = rand.Next(48, 90)
    33.             Do Until r < 58 OrElse r > 64
    34.                 r = rand.Next(48, 90)
    35.             Loop
    36.             passKey &= Chr(r)
    37.         Next
    38.         Return passKey
    39.     End Function
    40.  
    41.     Public Function GenerateKey() As String
    42.         If _PassKey = String.Empty Then
    43.             Return "Please Enter Passkey or generate a random passkey.  The longer the passkey, the greater the security."
    44.         End If
    45.         Dim nstr As String() = strsplt(_PassKey, 2)
    46.         Dim output As String = String.Empty
    47.         Dim tmp As String = String.Empty
    48.         For i As Integer = 0 To nstr.GetUpperBound(0)
    49.             tmp &= convert_keyto_value(nstr(i))
    50.         Next
    51.  
    52.         For i As Integer = 0 To tmp.Length - 1
    53.             output &= tmp.Substring(i, 1)
    54.             If i > 0 AndAlso i <> tmp.Length - 1 AndAlso (i + 1) Mod 5 = 0 Then
    55.                 output &= "-"
    56.             End If
    57.         Next
    58.         Return output
    59.     End Function
    60.  
    61.     Public Function ValidateKey(ByVal thetext As String) As Boolean
    62.         thetext = thetext.Replace("-"c, String.Empty)
    63.         Dim nstr As String() = strsplt(thetext, 3)
    64.         Dim output As String = String.Empty
    65.         For i As Integer = 0 To nstr.GetUpperBound(0)
    66.             output &= deRandomize(nstr(i))
    67.         Next
    68.         If output = _PassKey Then
    69.             Return True
    70.         Else
    71.             Return False
    72.         End If
    73.     End Function
    74.  
    75. #End Region
    76.  
    77. #Region " PRIVATE FUNCTIONS "
    78.     Private Function strsplt(ByVal thetext As String, Optional ByVal num As Integer = 1) As String()
    79.         Dim arr As New List(Of String)
    80.         Dim i As Integer
    81.         Dim y As String
    82.         For i = 0 To thetext.Length - 1 Step num
    83.             If i + num > thetext.Length - 1 Then
    84.                 y = thetext.Substring(i, (thetext.Length - i))
    85.             Else
    86.                 y = thetext.Substring(i, num)
    87.             End If
    88.  
    89.             arr.Add(y)
    90.         Next
    91.         Return arr.ToArray
    92.     End Function
    93.  
    94.     Private Function key_locator(ByVal code As String) As Integer
    95.         For i As Integer = 0 To strcode.GetUpperBound(0)
    96.             If strcode(i) = code Then
    97.                 Return i
    98.             End If
    99.         Next
    100.         Return Nothing
    101.     End Function
    102.  
    103.     Private Function add_random_key(ByVal thetext As String) As Integer()
    104.         Dim newcode As New List(Of Integer)
    105.         Dim rnd As New Random()
    106.         Dim r As Integer = rnd.Next(1, strcode.Length - 2)
    107.         Dim x As Integer
    108.         Dim tmp As Integer
    109.         For i As Integer = 0 To thetext.Length - 1
    110.             x = key_locator(thetext.Substring(i, 1))
    111.             tmp = x + r
    112.             If tmp > strcode.Length - 1 Then
    113.                 tmp = tmp - (strcode.Length - 1)
    114.             End If
    115.             newcode.Add(tmp)
    116.         Next
    117.         newcode.Add(r)
    118.         Return newcode.ToArray
    119.     End Function
    120.  
    121.     Private Function convert_keyto_value(ByVal thetext As String) As String
    122.         Dim a As Integer() = add_random_key(thetext)
    123.         Dim strReturn As String = String.Empty
    124.         Do Until Array.IndexOf(a, strcode.Length) = -1
    125.             a = add_random_key(thetext)
    126.         Loop
    127.         For i As Integer = 0 To a.GetUpperBound(0)
    128.             strReturn &= strcode(a(i))
    129.         Next
    130.         Return strReturn
    131.     End Function
    132.  
    133.     Private Function deRandomize(ByVal thetext As String) As String
    134.         Dim arr As String() = strsplt(thetext, thetext.Length - 1)
    135.         Dim s, t, newcode As Integer
    136.         Dim output As String = String.Empty
    137.  
    138.         For x As Integer = 0 To thetext.Length - 2
    139.             s = key_locator(arr(0).Substring(x, 1))
    140.             t = key_locator(arr(1))
    141.             newcode = s - t
    142.             If newcode < 0 Then
    143.                 newcode += (strcode.Length - 1)
    144.             End If
    145.             If newcode = 0 And s <> 0 Then
    146.                 newcode = (strcode.Length - 1)
    147.             End If
    148.             output &= strcode(newcode)
    149.         Next
    150.         Return output
    151.     End Function
    152. #End Region
    153.  
    154. 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:
    1. Dim Lic As New License
    2. Dim pKey As String = lic.GenerateRandomPassKey 'You need to save this key because it is used to validate any licenses you create
    3. lic.PassKey = pKey
    4. Dim LicenseKey As String = lic.GenerateKey
    Validating Licenses
    vb.net Code:
    1. Dim Lic As New License
    2. Lic.PassKey = YOUR_PASSKEY
    3. 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.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width