home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / TestRegist206899632007.psc / CGenericRegistration.cls < prev    next >
Text File  |  2001-05-27  |  7KB  |  179 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CGenericRegistration"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. '*******************************************************************************
  15. ' MODULE:       CGenericRegistration
  16. ' FILENAME:     C:\My Code\vb\Registration\CGenericRegistration.cls
  17. ' AUTHOR:       Phil Fresle
  18. ' CREATED:      26-May-2001
  19. ' COPYRIGHT:    Copyright 2001 Frez Systems Limited. All Rights Reserved.
  20. '
  21. ' DESCRIPTION:
  22. ' This class is designed to create a generic 25 character licence key for an
  23. ' application, similar to some recent Microsoft products. It first generates
  24. ' nine random characters from a list of possible 32 valid characters. This gives
  25. ' us 32^9 (or 35,184,372,088,832) possible combinations for each application.
  26. '
  27. ' An MD5 digest is made of the 9 random characters after appending some
  28. ' characters specific to our application (this might be the applications name
  29. ' or simply some random combination of characters). The longer the string the
  30. ' more difficult it will be for a hacker who knows our method to crack.
  31. '
  32. ' MD5 returns a 16 byte digest in hex. We convert this back to bytes and obtain
  33. ' a MOD 32 of each byte. We use the resultant number to lookup in our list
  34. ' of valid characters.
  35. '
  36. ' The 16 MD5 generated characters are added to the original 9 randomly generated
  37. ' characters to create our key.
  38. '
  39. ' By supplying different application specific characters to be added before
  40. ' generating the MD5 we can be fairly sure of uniqueness between applications.
  41. '
  42. ' A possible enhancement might be to reduce the number of random characters and
  43. ' instead supply characters that identify the type of licence, for instance
  44. ' what version of the software it is for, whether it is a network or standalone
  45. ' version, etc.
  46. '
  47. ' This is 'free' software with the following restrictions:
  48. '
  49. ' You may not redistribute this code as a 'sample' or 'demo'. However, you are free
  50. ' to use the source code in your own code, but you may not claim that you created
  51. ' the sample code. It is expressly forbidden to sell or profit from this source code
  52. ' other than by the knowledge gained or the enhanced value added by your own code.
  53. '
  54. ' Use of this software is also done so at your own risk. The code is supplied as
  55. ' is without warranty or guarantee of any kind.
  56. '
  57. ' Should you wish to commission some derivative work based on this code provided
  58. ' here, or any consultancy work, please do not hesitate to contact us.
  59. '
  60. ' Web Site:  http://www.frez.co.uk
  61. ' E-mail:    sales@frez.co.uk
  62. '
  63. ' MODIFICATION HISTORY:
  64. ' 1.0       27-May-2001
  65. '           Phil Fresle
  66. '           Initial Version
  67. '*******************************************************************************
  68. Option Explicit
  69.  
  70. ' 32 Numerics and alphas - we are missing I, O, S and Z not just because
  71. ' we only want 32 characters but also because I could be mistaken for
  72. ' the number 1, O for 0, S for 5 and Z for 2.
  73. Private Const VALID_CHARS As String = "0123456789ABCDEFGHJKLMNPQRTUVWXY"
  74.  
  75. Private Const RANDOM_LOWER As Long = 0
  76. Private Const RANDOM_UPPER As Long = 31
  77.  
  78. '*******************************************************************************
  79. ' GenerateKey (FUNCTION)
  80. '
  81. ' PARAMETERS:
  82. ' (In/Out) - sAppChars - String - Application specific characters to be used
  83. '                                 during the MD5 operation.
  84. '
  85. ' RETURN VALUE:
  86. ' String - The key
  87. '
  88. ' DESCRIPTION:
  89. ' Generates a random key by first selecting 9 random characters from our 32
  90. ' valid characters, adding our application specific characters, creating an
  91. ' MD5 digest, and using the digest to select the other 16 characters for
  92. ' our key.
  93. '*******************************************************************************
  94. Public Function GenerateKey(sAppChars As String) As String
  95.     Dim lChar           As Long
  96.     Dim lCount          As Long
  97.     Dim sInitialChars   As String
  98.     Dim oMD5            As CMD5
  99.     Dim sMD5            As String
  100.     Dim sKey            As String
  101.     
  102.     Randomize
  103.     
  104.     ' We first generate 9 random characters that are members of VALID_CHARS
  105.     sInitialChars = ""
  106.     For lCount = 1 To 9
  107.         lChar = Int((RANDOM_UPPER - RANDOM_LOWER + 1) * Rnd + RANDOM_LOWER)
  108.         sInitialChars = sInitialChars & Mid(VALID_CHARS, lChar + 1, 1)
  109.     Next
  110.     
  111.     ' We now get an MD5 of our initial chars plus out application chars
  112.     ' The application chars should be different for each application to
  113.     ' ensure that a key for one of our applications is not valid on another
  114.     ' of our applications. If hackers know we are using this method for
  115.     ' generating our keys we should ensure that the application characters
  116.     ' are very long to help prevent cracking.
  117.     Set oMD5 = New CMD5
  118.     sMD5 = oMD5.MD5(sInitialChars & sAppChars)
  119.     Set oMD5 = Nothing
  120.     
  121.     ' We now take each byte-pair from the MD5, convert it back to a byte
  122.     ' value from the hex code, do a MOD 32, and then select the appropriate
  123.     ' character from our VALID_CHARS
  124.     sKey = sInitialChars
  125.     
  126.     For lCount = 1 To 16
  127.         lChar = CLng("&H" & Mid(sMD5, (lCount * 2) - 1, 2)) Mod 32
  128.         sKey = sKey & Mid(VALID_CHARS, lChar + 1, 1)
  129.     Next
  130.     
  131.     GenerateKey = sKey
  132. End Function
  133.  
  134. '*******************************************************************************
  135. ' IsKeyOK (FUNCTION)
  136. '
  137. ' PARAMETERS:
  138. ' (In/Out) - sKey      - String - Key to check
  139. ' (In/Out) - sAppChars - String - Application specific characters used in
  140. '                                 generating the key.
  141. '
  142. ' RETURN VALUE:
  143. ' Boolean - True if valid
  144. '
  145. ' DESCRIPTION:
  146. ' Takes the key, recalculates the MD5 part and tests for equality.
  147. '*******************************************************************************
  148. Public Function IsKeyOK(sKey As String, _
  149.                         sAppChars As String) As Boolean
  150.                         
  151.     Dim lChar           As Long
  152.     Dim lCount          As Long
  153.     Dim sInitialChars   As String
  154.     Dim oMD5            As CMD5
  155.     Dim sMD5            As String
  156.     Dim sTestKey        As String
  157.     
  158.     ' Get the initial 9 characters, which were our random characters
  159.     sInitialChars = Left(sKey, 9)
  160.     
  161.     ' Recalculate the MD5 digest
  162.     Set oMD5 = New CMD5
  163.     sMD5 = oMD5.MD5(sInitialChars & sAppChars)
  164.     Set oMD5 = Nothing
  165.     
  166.     ' We now take each byte-pair from the MD5, convert it back to a byte
  167.     ' value from the hex code, do a MOD 32, and then select the appropriate
  168.     ' character from our VALID_CHARS
  169.     sTestKey = sInitialChars
  170.     
  171.     For lCount = 1 To 16
  172.         lChar = CLng("&H" & Mid(sMD5, (lCount * 2) - 1, 2)) Mod 32
  173.         sTestKey = sTestKey & Mid(VALID_CHARS, lChar + 1, 1)
  174.     Next
  175.     
  176.     ' Check for equality
  177.     IsKeyOK = (sTestKey = sKey)
  178. End Function
  179.