; ------------------------------------------------------------------------------
; 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.jpg"
$sCmd       = " /C cd """ & @ScriptDir & """ && copy /B *.jpg " & $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 )
    $sTmp = ""
    $iLine = 1

    ; 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 50 lines of the given file (do this with all given files)
    Do
        $sReadRes = FileRead( $hFile, $iLine )
        $sTmp    = $sTmp & $sReadRes                                                   ; $sTemp -> output (result)
        $iLine    = $iLine + 1
    Until $iLine  = 51

    ; 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 )
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( $iLineCount )
    ; 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 lines of the tmp file
    $iLine = 1
    Do
        $sReadTmpFileLine = FileReadLine( $hFile, $iLine )
        If @error = 1 Then MsgBox( 16, "Error", "Fehler beim Lesen." )
        If @error = -1 Then
            MsgBox( 48, "Hinweis", "EOF, der Tmp-Datei." )
            ExitLoop
        EndIf
        _ArrayAdd( $aLinesOfTmpFile, $sReadTmpFileLine )
        _ArrayDisplay( $aLinesOfTmpFile )

        ; compare (just a test)
        $iCompare = StringInStr( $sReadCont, $sReadTmpFileLine )
        If $iCompare = 0 Then
            MsgBox( 48, "Hinweis", "Nicht enthalten in Container-Datei." )
        Else
            MsgBox( 64, "Information", "Ist enthalten in Container-Datei." )
        EndIf

        $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