#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#Include <String.au3>

$PW1 = "VerschlüsselungsPasswort1"
$PW2 = "VerschlüsselungsPasswort2"
$RegPath = "HKEY_CURRENT_USER\Software\SerialTest" ;Am besten irgendwo hin, wo es niemand findet
$RegName = "SerialTest" ;Am besten ein verschlüsselter String (wirre Zeichenkombination)


$Form1 = GUICreate("Seriennummer", 256, 112)
GUICtrlCreateLabel("Produkt-Nummer:", 8, 8, 86, 15)
$ProdNrInput = GUICtrlCreateInput(_GetProductNr(), 8, 24, 241, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUICtrlCreateLabel("Seriennummer:", 8, 48, 74, 15)
$SerialNrInput = GUICtrlCreateInput("", 8, 64, 241, 21)
$ButtonCheck = GUICtrlCreateButton("Check", 176, 88, 74, 17)
$ButtonClear = GUICtrlCreateButton("Registry leeren", 8, 88, 80, 17)
GUISetState(@SW_SHOW)

$Form2 = GUICreate("Korrekte Seriennummer", 257, 55)
GUICtrlCreateLabel("Korrekte Seriennummer:", 8, 8, 117, 15)
$CorrectSerialNrInput = GUICtrlCreateInput(_GetSerialNr(), 8, 24, 241, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUISetState(@SW_SHOW)

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $ButtonCheck
			_CheckSerial($SerialNrInput)
		Case $ButtonClear ;Um neue Produktnummer zu generieren
			RegDelete($RegPath , $RegName)

	EndSwitch
WEnd

Func _GetProductNr()
	$RegProdNr = RegRead($RegPath , $RegName)
	If $RegProdNr = "" Then
		$ProductNr = Random(1000000000,9999999999, 1) ;Produktnummer wird Generiert (Pro PC unterschiedlich)
		RegWrite($RegPath , $RegName, "REG_SZ", _StringEncrypt(1, $ProductNr, $PW1, 2) & Random(10,99,1)) ;$SerialPosition wird hinten angehängt (Serials ähneln sonst zu stark)
	Else
		$ProductNr = _StringEncrypt(0, StringTrimRight($RegProdNr, 2), $PW1, 2)
	EndIf
	Return $ProductNr
EndFunc

Func _GetSerialNr()
	Local $CorrectSerialNr = ""
	$RegProdNr = RegRead($RegPath , $RegName)
	$SerialPosition = StringRight($RegProdNr, 2)
	$RegProdNr = StringTrimRight($RegProdNr, 2)
	$ProductNr = _StringEncrypt(0, $RegProdNr, $PW1, 2)
	$Serial = _StringEncrypt(1, $ProductNr, $PW2, 3)
	$Serial = StringTrimLeft(StringLeft($Serial, $SerialPosition + 20), $SerialPosition)
	For $i = 1 To 4
		If $i < 4 Then
			$CorrectSerialNr &= StringTrimLeft(Stringleft($Serial, $i * 5), $i * 5 - 5) & "-"
		Else
			$CorrectSerialNr &= StringTrimLeft(Stringleft($Serial, $i * 5), $i * 5 - 5)
		EndIf
	Next
	Return $CorrectSerialNr
EndFunc

Func _CheckSerial($CheckSerialInput)
	If GUICtrlRead($CheckSerialInput) = _GetSerialNr() Then
		MsgBox(0, "Test", "Korrekt")
	Else
		MsgBox(0, "Test", "Falsch")
	EndIf
EndFunc
