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 ++++++++++++++++++++++++++++++++++++++++++++++

    ''' <summary>
    ''' Konstruktor für den Lizenz-Manager
    ''' </summary>
    ''' <param name="_Password">Passwort zum Entschlüsseln des Lizenzkeys</param>
    ''' <param name="_Key">verschlüsselter Lizenzkey</param>
    ''' <remarks></remarks>
    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
    ''' <summary>
    ''' Konstruktor für den Lizenz-Manager ohne License load
    ''' </summary>
    ''' <param name="password">Passwort zum Entschlüsseln des Lizenzkeys</param>
    Sub New(ByVal password As String)
        Me.Password = password
    End Sub
    ''' <summary>
    ''' Lädt alle Lizenzen aus dem Lizenz-Array
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub LoadLicenses()
        _licenses = New LicensesLegacy(_LogConfig, LicenseStringArray)
    End Sub


    ''' <summary>
    ''' Codiert eine Zeichenkette
    ''' </summary>
    ''' <param name="str">zu verschlüsselnde Zeichenkette</param>
    ''' <param name="password">das zur Verschlüsselung verwendete Passwort</param>
    ''' <returns>liefert eine verschlüsselte Zeichenkette</returns>
    ''' <remarks></remarks>
    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


    ''' <summary>
    ''' Decodiert den verschlüsselten Lizenzkey
    ''' </summary>
    ''' <param name="licenseCodeStr">verschlüsselter Lizenzkey</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    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


    ''' <summary>
    ''' Zerlegt den entschlüsselten Lizenzkey
    ''' </summary>
    ''' <param name="licenseStr">entschlüsselter Lizenzkey</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    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 ++++++++++++++++++++++++++++++++++++++++++++++

    ''' <summary>
    ''' Liefert das Passwort zum Entschlüsseln des Lizenzschlüssels
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Password() As String


    ''' <summary>
    ''' Liefert eine Sammlung von Lizenzobjekten
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Licenses() As LicensesLegacy
        Get
            Return _licenses
        End Get
    End Property


    ''' <summary>
    ''' Liefert oder setzt den Lizenzschlüssel
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Key() As String
End Class
