# Function Reference

# _Crypt_EncryptFile

Encrypts a file with specified key and algorithm

#include <Crypt.au3>
_Crypt_EncryptFile ( $sSourceFile, $sDestinationFile, $vCryptKey, $iAlgID )

## Parameters

$sSourceFile | File to process
$sDestinationFile | File to save the processed file
$vCryptKey | Password or handle to a key if the CALG_USERKEY flag is specified
$iAlgID | The algorithm to use. See _Crypt_DeriveKey().

## Return Value

Success: | True.
Failure: | False and sets the @error flag to non-zero.
@error: | 2 - Failed to open source file 3 - Failed to open destination file 10+ - Failed to create key 400+ - Failed to encrypt final piece 500+ - Failed to encrypt piece 1000+ - Failed to _Crypt_Startup()

## Remarks

The output file can be larger than the input file depending on the algorithm.

## Related

_Crypt_DecryptFile, _Crypt_DeriveKey, _Crypt_EncryptData

## Example

    #include <ComboConstants.au3>
    #include <Crypt.au3>
    #include <GUIConstantsEx.au3>
    #include <MsgBoxConstants.au3>
    #include <StringConstants.au3>

    Example()

    Func Example()
        Local $iAlgorithm = $CALG_RC4

        Local $hGUI = GUICreate("File Encrypter", 425, 100)
        Local $idInp_Source = GUICtrlCreateInput("", 5, 5, 200, 20)
        Local $idBtn_SourceBrowse = GUICtrlCreateButton("...", 210, 5, 35, 20)

        Local $idInp_Destination = GUICtrlCreateInput("", 5, 30, 200, 20)
        Local $idBtn_DestinationBrowse = GUICtrlCreateButton("...", 210, 30, 35, 20)

        GUICtrlCreateLabel("Password:", 5, 60, 200, 20)
        Local $idInp_Password = GUICtrlCreateInput("", 5, 75, 200, 20)

        Local $idCombo = GUICtrlCreateCombo("", 210, 75, 100, 20, $CBS_DROPDOWNLIST)
        GUICtrlSetData($idCombo, "3DES (168bit)|AES (128bit)|AES (192bit)|AES (256bit)|DES (56bit)|RC2 (128bit)|RC4 (128bit)", "RC4 (128bit)")
        Local $idBtn_Encrypt = GUICtrlCreateButton("Encrypt", 355, 70, 65, 25)
        GUISetState(@SW_SHOW, $hGUI)

        Local $sDestinationRead = "", $sFilePath = "", $sPasswordRead = "", $sSourceRead = ""
        While 1
            Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE
                    ExitLoop

                Case $idBtn_SourceBrowse
                    $sFilePath = FileOpenDialog("Select a file to encrypt.", "", "All files (*.*)") ; Select a file to encrypt.
                    If @error Then
                        ContinueLoop
                    EndIf
                    GUICtrlSetData($idInp_Source, $sFilePath) ; Set the inputbox with the filepath.

                Case $idBtn_DestinationBrowse
                    $sFilePath = FileSaveDialog("Save the file as ...", "", "All files (*.*)") ; Select a file to save the encrypted data to.
                    If @error Then
                        ContinueLoop
                    EndIf
                    GUICtrlSetData($idInp_Destination, $sFilePath) ; Set the inputbox with the filepath.

                Case $idCombo ; Check when the combobox is selected and retrieve the correct algorithm.
                    Switch GUICtrlRead($idCombo) ; Read the combobox selection.
                        Case "3DES (168bit)"
                            $iAlgorithm = $CALG_3DES

                        Case "AES (128bit)"
                            $iAlgorithm = $CALG_AES_128

                        Case "AES (192bit)"
                            $iAlgorithm = $CALG_AES_192

                        Case "AES (256bit)"
                            $iAlgorithm = $CALG_AES_256

                        Case "DES (56bit)"
                            $iAlgorithm = $CALG_DES

                        Case "RC2 (128bit)"
                            $iAlgorithm = $CALG_RC2

                        Case "RC4 (128bit)"
                            $iAlgorithm = $CALG_RC4

                    EndSwitch

                Case $idBtn_Encrypt
                    $sSourceRead = GUICtrlRead($idInp_Source) ; Read the source filepath input.
                    $sDestinationRead = GUICtrlRead($idInp_Destination) ; Read the destination filepath input.
                    $sPasswordRead = GUICtrlRead($idInp_Password) ; Read the password input.
                    If StringStripWS($sSourceRead, $STR_STRIPALL) <> "" And StringStripWS($sDestinationRead, $STR_STRIPALL) <> "" And StringStripWS($sPasswordRead, $STR_STRIPALL) <> "" And FileExists($sSourceRead) Then ; Check there is a file available to encrypt and a password has been set.
                        If _Crypt_EncryptFile($sSourceRead, $sDestinationRead, $sPasswordRead, $iAlgorithm) Then ; Encrypt the file.
                            MsgBox($MB_SYSTEMMODAL, "Success", "Operation succeeded.")
                        Else
                            Switch @error
                                Case 30
                                    MsgBox($MB_SYSTEMMODAL, "Error", "Failed to create the key.")
                                Case 2
                                    MsgBox($MB_SYSTEMMODAL, "Error", "Couldn't open the source file.")
                                Case 3
                                    MsgBox($MB_SYSTEMMODAL, "Error", "Couldn't open the destination file.")
                                Case 400 Or 500
                                    MsgBox($MB_SYSTEMMODAL, "Error", "Encryption error.")
                                Case Else
                                    MsgBox($MB_SYSTEMMODAL, "Error", "Unexpected @error = " & @error)
                            EndSwitch
                        EndIf
                    Else
                        MsgBox($MB_SYSTEMMODAL, "Error", "Please ensure the relevant information has been entered correctly.")
                    EndIf
            EndSwitch
        WEnd

        GUIDelete($hGUI) ; Delete the previous GUI and all controls.
    EndFunc   ;==>Example