; ------------------------------------------------------------------------------
; EnAndDeCrypt.au3
; (just a snippet)
; ------------------------------------------------------------------------------        ; DEBUG & COMMENT AREA
#region includes
#include <Array.au3>
#include <File.au3>
#include <String.au3>
#endregion includes

#region init
HotKeySet( "{F10}", "_exitScript" )
#endregion init

#region declaration
$iCount     = 0
$sPath      = @ScriptDir & "\"
$aFiles     = _FileListToArray( $sPath )
$sCont      = "container.bmp"
$sCmd       = " /C cd """ & @ScriptDir & """ && copy /B *.bmp " & $sCont
$sTmpFile   = @TempDir & "\EnAndDeCrypt_savepoint.txt"
Local $aLinesOfTmpFile[1]
#endregion declaration

#region help functions
Func _exitScript()
    Exit
EndFunc

Func _debug( $sString )
    MsgBox( 262144, "DEBUG", $sString )
EndFunc
#endregion help functions

#region functions
Func _enCrypt( $sFile, $sMsg )
    ; open the given file
;~  $hFile = FileOpen( $sFile, 0 )                                                      ; normal mode
    $hFile = FileOpen( $sFile, 16 )                                                     ; binary mode
    If $hFile = -1 Then
        MsgBox( 16, "Error", "Datei '" & $sFile & "' konnte nicht geöffnet werden." )
        Exit
    EndIf

    ; read the first 2000 (1999) Bytes (that's 4000 charcaters) as HEX (BIN) of the given file
    ; do this with all given files by "_enCrypt( ... )"
    $sTmp       = ""
    $sReadRes   = FileRead( $hFile, 1999 )
    If @error   = 1 Then MsgBox( 16, "Error", "Fehler beim Lesen." )
    If @error   = -1 Then MsgBox( 48, "Hinweis", "EOF." )
    $sTmp      = $sTmp & $sReadRes                                                     ; $sTemp -> output (result)

    ; close the file
    FileClose( $hFile )
;~                                                                                      MsgBox( 48, $sMsg, $sTmp )
    ; open the tmp file
    $hFile = FileOpen( @TempDir & "\EnAndDeCrypt_savepoint.txt", 1 )
    If $hFile = -1 Then
        MsgBox( 16, "Error", "Datei konnte nicht geöffnet werden." )
        Exit
    EndIf

    ; write into the tmp file (each 50 lines of a file, on 1 line into the tmp file)
    $iWrite = FileWrite( $hFile, $sTmp & @CRLF )
    If $iWrite = 0 Then
        MsgBox( 16, "Error", "Datei konnte nicht beschrieben werden." )
        Exit
    EndIf

    ; close the file
    FileClose( $hFile )
;~                                                                                      _debug( $sTmp )
EndFunc

Func _deCrypt()
    ; open container file
;~  $hFile = FileOpen( @ScriptDir & "\" & $sCont, 0 )                                   ; nornal mode
    $hFile = FileOpen( @ScriptDir & "\" & $sCont, 16 )                                  ; binary mode
    If $hFile = -1 Then
        MsgBox( 16, "Error", "Datei '" & $sCont & "' konnte nicht geöffnet werden." )
        Exit
    EndIf
;~                                                                                      _debug( @ScriptDir & "\" & $sCont )
    ; read the container file
    $sReadCont = FileRead( $hFile )                                                     ; $sReadCont -> output (result)
    If @error = 1 Then MsgBox( 16, "Error", "Fehler beim Lesen." )
    If @error = -1 Then MsgBox( 48, "Hinweis", "EOF, der Container-Datei." )

    ; close the file
    FileClose( $hFile )
;~                                                                                      _debug( $sReadCont )
    ; get the line number
    $iLineCount = _FileCountLines( $sTmpFile )
    If @error = 1 Then MsgBox( 16, "Error", "Fehler beim Lesen der Zeilen." )
;~                                                                                      _debug( "Es sind " & $iLineCount & " Zeilen, also " & $iLineCount & " Dateien!" )
    ; open the tmp file
    $hFile = FileOpen( $sTmpFile, 0 )
    If $hFile = -1 Then
        MsgBox( 16, "Error", "Datei '" & $sTmpFile & "' konnte nicht geöffnet werden." )
        Exit
    EndIf

    ; read the first 2000 (1999) Bytes (that's 4000 charcaters) as HEX (BIN) of the tmp file
    ; then read the next 2000 Bytes of the next line (the next file in tmp file)
    $sReadTmpFileLine = FileRead( $hFile )
    If @error = 1 Then MsgBox( 16, "Error", "Fehler beim Lesen." )
    If @error = -1 Then MsgBox( 48, "Hinweis", "EOF, der Tmp-Datei." )

    $sStart = 1
    $iLine  = 1
    Do
        $sReadTmpFileLine2 = StringMid( $sReadTmpFileLine, $sStart, 4000 )
        $t = StringLen( $sReadTmpFileLine2 )
        _debug( $t )

        $iCompare = StringInStr( $sReadCont, $sReadTmpFileLine2 )
        If $iCompare = 0 Then
            MsgBox( 48, "Hinweis", "Nicht enthalten in Container-Datei." )
            ClipPut( $sReadTmpFileLine2 )
        Else
            MsgBox( 64, "Information", "Ist enthalten in Container-Datei." )
        EndIf

        $sStart = $sStart + 4002
        $iLine = $iLine + 1
    Until $iLine = $iLineCount
EndFunc
#endregion functions

#region actions, calls
; create a tmp file
$iCreate = _FileCreate( $sTmpFile )
If $iCreate = 0 Then MsgBox( 16, "Error", "Es konnte nicht in TEMP geschrieben werden." )

; run the function _enCrypt() till the last file
Do
    If $aFiles[$iCount + 1] <> "EnAndDeCrypt.au3" Then _enCrypt( $sPath & $aFiles[$iCount + 1], $aFiles[$iCount + 1] )
    $iCount = $iCount + 1
Until $iCount = $aFiles[0]

; do the copy /B (small encrypting)
$iCmd = ShellExecuteWait( @ComSpec, $sCmd, "", "", @SW_HIDE )
;~ If $iCmd = 0 Then MsgBox( 16, "Error", "Es ist ein Fehler aufgetreten." )
;~                                                                                      _debug( @ComSpec & $sCmd )
; run the function _deCrypt() ... this will be separate later
_deCrypt()
#endregion actions, calls