diff --git a/Modules.License/License.vbproj b/Modules.License/License.vbproj index d8b0085d..2787322b 100644 --- a/Modules.License/License.vbproj +++ b/Modules.License/License.vbproj @@ -74,9 +74,12 @@ + + LicenseSchema.xsd + True diff --git a/Modules.License/LicenseLegacy.vb b/Modules.License/LicenseLegacy.vb new file mode 100644 index 00000000..17563198 --- /dev/null +++ b/Modules.License/LicenseLegacy.vb @@ -0,0 +1,44 @@ +Public Class LicenseLegacy + ' ++++++++++++++++++++++++++++++++++++++++++++++ Methoden ++++++++++++++++++++++++++++++++++++++++++++++ + + ''' + ''' Konstruktor der Lizenz + ''' + Sub New(ByVal _ModuleName As String, ByVal _Expires As Date, ByVal _Type As String, ByVal _AnzProfile As String) + Modulename = _ModuleName + Expires = _Expires + Type = _Type + Anz_Profile = _AnzProfile + End Sub + + ' ++++++++++++++++++++++++++++++++++++++++++++++ Properties ++++++++++++++++++++++++++++++++++++++++++++++ + + ''' + ''' Liefert oder setzt den Namen des Moduls für diese Lizenz + ''' + Public Property Modulename() As String + ''' + ''' Liefert oder setzt das Gültigkeitsdatum der Lizenz für das Modul + ''' + Public Property Expires() As Date + ''' + ''' Liefert den Typen der Lizenz + ''' + Public Property Type() As String + ''' + ''' Liefert die Anzahl der Profile + ''' + Public Property Anz_Profile() As String + ''' + ''' Liefert ob die Lizenz schon abgelaufen ist + ''' + Public ReadOnly Property IsExpired() + Get + If Date.Today > Expires Then + Return True + Else + Return False + End If + End Get + End Property +End Class diff --git a/Modules.License/LicenseManagerLegacy.vb b/Modules.License/LicenseManagerLegacy.vb new file mode 100644 index 00000000..4033665e --- /dev/null +++ b/Modules.License/LicenseManagerLegacy.vb @@ -0,0 +1,228 @@ +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 diff --git a/Modules.License/LicensesLegacy.vb b/Modules.License/LicensesLegacy.vb new file mode 100644 index 00000000..582acbf8 --- /dev/null +++ b/Modules.License/LicensesLegacy.vb @@ -0,0 +1,104 @@ +Imports DigitalData.Modules.Logging + +Public Class LicensesLegacy + Private _licenses() As LicenseLegacy + Private _WDLizenz As String + Private _Logger As Logger + + ' ++++++++++++++++++++++++++++++++++++++++++++++ Methoden ++++++++++++++++++++++++++++++++++++++++++++++ + + ''' + ''' Konstruktor für die Lizenzen-Sammlung + ''' + ''' In Array übertragene Lizenzinformationen + Sub New(LogConfig As LogConfig, ByVal licenseStringArray(,) As String) + Try + _Logger = LogConfig.GetLogger() + + If licenseStringArray IsNot Nothing Then + Dim license1() As String = licenseStringArray(0, 1).Split("#") + + If license1.Length > 1 Then + Me.machine = license1(1) + Else + Me.machine = "all" + End If + Me.Company = license1(0) 'licenseStringArray(0, 1) + Dim i As Integer + For i = 1 To licenseStringArray.GetLength(0) - 2 ' minus 2, weil GetLength nicht null-basiert ist und das erste Element Firma ist + Dim anzprof As String = licenseStringArray(i, 3) + If anzprof.ToLower = "unlimited" Then + anzprof = 99 + End If + Me.Add(licenseStringArray(i, 0), licenseStringArray(i, 1), licenseStringArray(i, 2), anzprof) + Next + End If + Catch ex As Exception + _Logger.Error(ex) + End Try + End Sub + + + ''' + ''' Fügt der Lizenz-Sammlung eine Lizenz hinzu + ''' + ''' Name des Moduls, für das eine Lizenz angelegt werden soll + ''' Datum der Gültigkeit der Lizenz + Public Sub Add(ByVal modulename As String, ByVal expires As Date, ByVal type As String, ByVal anzprof As String) + + If _licenses IsNot Nothing Then + ReDim Preserve _licenses(_licenses.Length) + Else + ReDim Preserve _licenses(0) + End If + + _licenses(_licenses.Length - 1) = New LicenseLegacy(modulename, expires, type, anzprof) + End Sub + + + ''' + ''' Liefert eine Lizenz an Hand des Modulnamens + ''' + ''' Name des zu suchenden Moduls + ''' liefert ein Lizenzobjekt + Public Function GetLicense(ByVal modulename As String) As LicenseLegacy + If _licenses IsNot Nothing Then + For Each license As LicenseLegacy In _licenses + If license.Modulename = modulename Then + Return license + End If + Next + End If + + Return Nothing + End Function + + ' ++++++++++++++++++++++++++++++++++++++++++++++ Properties ++++++++++++++++++++++++++++++++++++++++++++++ + + ''' + ''' liefert eine Sammlung von Lizenzobjekten + ''' + Public ReadOnly Property Licenses() As LicenseLegacy() + Get + Return _licenses + End Get + End Property + + + ''' + ''' liefert eine Lizenz an Hand des Modulnamens + ''' + ''' Name des zu suchenden Moduls + Public ReadOnly Property License(ByVal modulename As String) As LicenseLegacy + Get + Return GetLicense(modulename) + End Get + End Property + + + ''' + ''' Liefert oder setzt den Firmennamen des Lizenzeigentümers + ''' + Public Property Company() As String + Public Property machine() As String +End Class