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