Imports System.Security.Cryptography Imports System.Text Imports DigitalData.Modules.Logging Public Class LicenseManagerLegacy Public Shared _licenses As LicensesLegacy Private _Logger As Logger Private _LogConfig As LogConfig Public LicenseString As String Public LicenseStringArray(,) As String ' ++++++++++++++++++++++++++++++++++++++++++++++ Methoden ++++++++++++++++++++++++++++++++++++++++++++++ ''' ''' Konstruktor für den Lizenz-Manager ''' ''' Passwort zum Entschlüsseln des Lizenzkeys ''' verschlüsselter Lizenzkey ''' Sub New(LogConfig As LogConfig, ByVal _Password As String, ByVal _Key As String) _LogConfig = LogConfig _Logger = LogConfig.GetLogger Password = _Password Key = _Key LicenseString = DecodeLicenseKey(Key) LicenseStringArray = SplitLicenseString(LicenseString) LoadLicenses() End Sub ''' ''' Konstruktor für den Lizenz-Manager ohne License load ''' ''' Passwort zum Entschlüsseln des Lizenzkeys Sub New(ByVal password As String) Me.Password = password End Sub ''' ''' Lädt alle Lizenzen aus dem Lizenz-Array ''' ''' Public Sub LoadLicenses() _licenses = New LicensesLegacy(_LogConfig, LicenseStringArray) End Sub ''' ''' Codiert eine Zeichenkette ''' ''' zu verschlüsselnde Zeichenkette ''' das zur Verschlüsselung verwendete Passwort ''' liefert eine verschlüsselte Zeichenkette ''' Public Function EncodeLicenseKey(ByVal str As String, ByVal password As String) Dim oRijndael As New RijndaelManaged Dim oMD5 As New MD5CryptoServiceProvider Dim oKey() As Byte = oMD5.ComputeHash(Encoding.UTF8.GetBytes(password)) oMD5.Clear() oRijndael.Key = oKey oRijndael.GenerateIV() Dim oInitialVector() As Byte = oRijndael.IV Dim oStream As New IO.MemoryStream oStream.Write(oInitialVector, 0, oInitialVector.Length) Dim oCryptoStream As New CryptoStream(oStream, oRijndael.CreateEncryptor, CryptoStreamMode.Write) Dim oData() As Byte = Encoding.UTF8.GetBytes(str) oCryptoStream.Write(oData, 0, oData.Length) oCryptoStream.FlushFinalBlock() Dim oEncodedData() As Byte = oStream.ToArray() Dim oResult As String = Convert.ToBase64String(oEncodedData) oCryptoStream.Close() oRijndael.Clear() Return oResult End Function ''' ''' Decodiert den verschlüsselten Lizenzkey ''' ''' verschlüsselter Lizenzkey ''' ''' Public Function DecodeLicenseKey(ByVal licenseCodeStr As String) Try Dim oRijndael As New RijndaelManaged Dim oIVLength As Integer = 16 Dim oMD5 As New MD5CryptoServiceProvider Dim oKey() As Byte = oMD5.ComputeHash(Encoding.UTF8.GetBytes(Me.Password)) oMD5.Clear() Dim oEncodedData() As Byte Dim oStream Dim oInitialVector(15) As Byte Dim oCryptoStream As CryptoStream Dim oData() As Byte Try oEncodedData = Convert.FromBase64String(licenseCodeStr) oStream = New IO.MemoryStream(oEncodedData) oStream.Read(oInitialVector, 0, oIVLength) oRijndael.IV = oInitialVector oRijndael.Key = oKey oCryptoStream = New CryptoStream(oStream, oRijndael.CreateDecryptor, CryptoStreamMode.Read) ReDim oData(oStream.Length - oIVLength) Catch ex As Exception Return Nothing End Try Dim oInteger As Integer = 0 Try oInteger = oCryptoStream.Read(oData, 0, oData.Length) Dim oResult As String = Encoding.UTF8.GetString(oData, 0, oInteger) oCryptoStream.Close() oRijndael.Clear() Return oResult Catch ex As Exception ' Falsches Passwort _Logger.Warn("DecodeLicenseKey: Password Incorrect") _Logger.Error(ex) Return Nothing End Try Catch ex As Exception ' Falsches Passwort _Logger.Warn("DecodeLicenseKey: Password Incorrect") _Logger.Error(ex) End Try Return Nothing End Function ''' ''' Zerlegt den entschlüsselten Lizenzkey ''' ''' entschlüsselter Lizenzkey ''' ''' Public Function SplitLicenseString(ByVal licenseStr As String) As String(,) Try If licenseStr IsNot Nothing Then Dim licenseTemp() As String = licenseStr.Split(";") Dim licenses(licenseTemp.Length, 3) As String Dim i As Integer = 0 If licenseTemp IsNot Nothing Then For Each lizenz As String In licenseTemp Dim temp() = lizenz.Split(":") licenses(i, 0) = temp(0) licenses(i, 1) = temp(1) If temp.Length > 2 Then licenses(i, 2) = temp(2) licenses(i, 3) = temp(3) Dim expires As Date Date.TryParse(licenses(i, 1), expires) End If i += 1 Next Return licenses End If End If Catch ex As Exception _Logger.Warn("Error in SplitLicenseString") _Logger.Error(ex) End Try Return Nothing End Function ' ++++++++++++++++++++++++++++++++++++++++++++++ Properties ++++++++++++++++++++++++++++++++++++++++++++++ ''' ''' Liefert das Passwort zum Entschlüsseln des Lizenzschlüssels ''' ''' ''' ''' Public ReadOnly Property Password() As String ''' ''' Liefert eine Sammlung von Lizenzobjekten ''' ''' ''' ''' Public ReadOnly Property Licenses() As LicensesLegacy Get Return _licenses End Get End Property ''' ''' Liefert oder setzt den Lizenzschlüssel ''' ''' ''' ''' Public Property Key() As String End Class