#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
; ----------------------------------------------------------------------------------------------------------------------------------
Global $Str = ""
Global $Key = ""
Global $EncPath = @ScriptDir & "\Enc"
Global $KeyPath = @ScriptDir & "\Key"
Global $File = ""
Global $NewKey = False
; ----------------------------------------------------------------------------------------------------------------------------------
$File = @ProgramFilesDir & "\AutoIt3\Include\Excel.au3"     ; ggf. auskommntieren
$Str = FileRead($File)                                      ; ggf. auskommentieren
$Key = _GenKey()                                            ; ggf. auskommentieren
; ----------------------------------------------------------------------------------------------------------------------------------
If Not FileExists($EncPath & "\") Then
   DirCreate($EncPath)
EndIf
If Not FileExists($KeyPath & "\") Then
   DirCreate($KeyPath)
EndIf
; ----------------------------------------------------------------------------------------------------------------------------------
Opt("GUICoordMode", 2)
$hGUI = GUICreate("Buchstabensuppe aus ANSI-Textdateien", 810, 550)
$idFileMenu = GUICtrlCreateMenu("&Datei")
$idOpenFile = GUICtrlCreateMenuItem("&Öffnen", $idFileMenu)
$idSaveFile = GUICtrlCreateMenuItem("&Speichern", $idFileMenu)
GUICtrlCreateMenuItem("", $idFileMenu)
$idExit = GUICtrlCreateMenuItem("&Beenden", $idFileMenu)
$idKeyMenu = GUICtrlCreateMenu("&Schlüssel")
$idLoadKey = GUICtrlCreateMenuItem("&Laden", $idKeyMenu)
$idSaveKey = GUICtrlCreateMenuItem("&Speichern", $idKeyMenu)
GUISetCoord(5, 5)
GUICtrlCreateLabel("Key:", -1, -1, -1, 60, $SS_CENTERIMAGE)
GUICtrlSetColor(-1, 0x000080)
$aPos = WinGetPos(GUICtrlGetHandle(-1))
$W = 800 - $aPos[2]
$idKey = GUICtrlCreateEdit($Key, 0, -1, $W, 60, $ES_READONLY)
GUICtrlSetFont(-1, Default, Default,Default, "Courier New")
GUICtrlSetColor(-1, 0x800000)
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetData(-1, $Key)
GUISetCoord(5, 70)
$idEdit = GUICtrlCreateEdit("", -1, -1, 800, 400)
DllCall("User32.dll", "None", "SendMessageA", "HWND", GUICtrlGetHandle(-1), "UINT", $EM_LIMITTEXT, "INT", 0, "INT", 0)
GUICtrlSetFont(-1, Default, Default,Default, "Courier New")
GUICtrlSetData(-1, $Str)
$idEncrypt = GUICtrlCreateButton("Verschlüsseln", -1, 5, 120, 25)
$idDecrypt = GUICtrlCreateButton("Entschlüsseln", 0, -1)
$idGenKey = GUICtrlCreateButton("Neuer Schlüssel", 0, -1)
GUISetCoord(5, 505)
$idStatus = GUICtrlCreateLabel(" " & StringLen($Str) & " Zeichen", -1, -1, 800, 20, $SS_SUNKEN + $SS_CENTERIMAGE)
ControlFocus($hGUI, "", $idGenKey)
GUISetState()
While True
   Switch GUIGetMsg()
      Case 0
         Sleep(10)
      Case $GUI_EVENT_CLOSE
         ExitLoop
      Case $idExit
         ExitLoop
      Case $idGenKey
         $Key = _GenKey()
         GUICtrlSetData($idKey, $Key)
      Case $idEncrypt
         $Str = _Encrypt(GUICtrlRead($idEdit), $Key)
         GUICtrlSetData($idEdit, $Str)
      Case $idDecrypt
         $Str = _Decrypt(GUICtrlRead($idEdit), $Key)
         GUICtrlSetData($idEdit, $Str)
      Case $idOpenFile
         _OpenFile()
         GUICtrlSetData($idEdit, $Str)
         GUICtrlSetData($idStatus, " " & StringLen($Str) & " Zeichen")
      Case $idLoadKey
         _LoadKey()
         GUICtrlSetData($idKey, $Key)
      Case $idSaveFile
         _SaveFile()
      Case $idSaveKey
         _SaveKey()
   EndSwitch
WEnd
GUIDelete()
Exit
; ----------------------------------------------------------------------------------------------------------------------------------
Func _Decrypt($S, $K)
   Local $aS, $A, $I, $L, $R, $T
   $L = StringLen($K)
   GUISetState(@SW_DISABLE)
   $T = TimerInit()
   $aS = StringToASCIIArray($S, Default, Default, 1)
   For $I = 0 To UBound($aS) - 1
      $A = StringInStr($K, Chr($aS[$I]), 1)
      $aS[$I] = $A
      $R = BitAND($A + $I, 0xF) + 1
      $K = StringRight($K, $L - $R) & StringLeft($K, $R)
   Next
   $S = StringFromASCIIArray($aS, Default, Default, 1)
   $T = TimerDiff($T)
   GUICtrlSetData($idStatus, " " & StringLen($S) & " Zeichen in " & Round($T) & " ms.")
   GUISetState(@SW_ENABLE)
   Return $S
EndFunc
; ----------------------------------------------------------------------------------------------------------------------------------
Func _Encrypt($S, $K)
   Local $aS, $A, $I, $L, $R, $T
   $L = StringLen($K)
   GUISetState(@SW_DISABLE)
   $T = TimerInit()
   $aS = StringToASCIIArray($S, Default, Default, 1)
   For $I = 0 To UBound($aS) - 1
      $A = $aS[$I]
      $aS[$I] = Asc(StringMid($K, $A, 1))
      $R = BitAND($A + $I, 0xF) + 1
      $K = StringRight($K, $L - $R) & StringLeft($K, $R)
   Next
   $S = StringFromASCIIArray($aS, Default, Default, 1)
   $T = TimerDiff($T)
   GUICtrlSetData($idStatus, " " & StringLen($S) & " Zeichen in " & Round($T) & " ms.")
   GUISetState(@SW_ENABLE)
   Return $S
EndFunc
; ----------------------------------------------------------------------------------------------------------------------------------
Func _GenKey()
   Local $A[255], $I, $K, $R
   For $I = 0 To 254
      $A[$I] = Chr($I + 1)
   Next
   For $I = 254 To 0 Step -1
      $R = Random(0, $I, 1)
      $K &= $A[$R]
      $A[$R] = $A[$I]
   Next
   $NewKey = True
   Return $K
EndFunc
; ----------------------------------------------------------------------------------------------------------------------------------
Func _OpenFile()
   Local $F, $P, $S
   $P = $EncPath & "\"
   $F = FileOpenDialog("Datei öffnen", $P, "Alle (*.*)", 1 + 2, "", $hGUI)
   If @Error Then
      Return False
   EndIf
   $S = FileRead($F)
   If @error = 1 Then
      MsgBox(0, "Datei öffnen", "Die Datei " & $F & " konnte nicht gelesen werden!", 0, $hGUI)
      Return False
   EndIf
   $Str = $S
   $File = $F
   Return True
EndFunc
; ----------------------------------------------------------------------------------------------------------------------------------
Func _LoadKey()
   Local $F, $K, $P
   $P = $KeyPath & "\"
   $F = FileOpenDialog("Key laden", $P, "Key (*.Key)", 1 + 2, "", $hGUI)
   If @Error Then
      Return False
   EndIf
   $K = FileRead($F)
   If @error = 1 Then
      MsgBox(0, "Datei öffnen", "Die Datei " & $F & " konnte nicht gelesen werden!", 0, $hGUI)
      Return False
   EndIf
   $Key = $K
   $NewKey = False
   Return True
EndFunc
; ----------------------------------------------------------------------------------------------------------------------------------
Func _SaveFile()
   Local $F, $M, $P
   $P = $EncPath & "\"
   $F = ""
   If $File Then
      $M = StringRegExp($File, ".*\\(.*)\.", 1)
      If Not @error Then
         $F = "C_" & $M[0] & ".txt"
      EndIf
   EndIf
   $F = FileSaveDialog("Datei speichern?", $P, "Text (*.txt)", 2 + 16, $F, $hGUI)
   If @Error Then
      Return False
   EndIf
   $File = $F
   $Str = GUICtrlRead($idEdit)
   FileWrite($File, $Str)
   If $NewKey Then
      If MsgBox(4, "Schlüssel speichern?", _
            "Der Schlüssel wurde neu generiert, soll er gespeichert werden?", 0, $hGUI) = 6 Then
         _SaveKey()
      EndIf
   EndIf
   Return True
EndFunc
; ----------------------------------------------------------------------------------------------------------------------------------
Func _SaveKey()
   Local $F, $P
   $P = $KeyPath & "\"
   $F = FileSaveDialog("Schlüssel speichern?", $P, "Key (*.key)", 2 + 16, "", $hGUI)
   If @Error Then
      Return False
   EndIf
   $Key = GUICtrlRead($idKey)
   FileWrite($F, $Key)
   $NewKey = False
   Return True
EndFunc