Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #comments-start
- File Encryptor/Decryptor. This code simply combines both functions into a single usable app.
- Have removed all but AES-256 encryption methods.
- Original code can be below at the following links:
- Encrypt Function: https://www.autoitscript.com/autoit3/docs/libfunctions/_Crypt_EncryptFile.htm
- Decrypt Function: https://www.autoitscript.com/autoit3/docs/libfunctions/_Crypt_DecryptFile.htm
- Compiled binary can be found at: https://bitbucket.org/TheChiefMeat/file-encryptor-decryptor/downloads/File%20Encryptor-Decryptor.zip
- #comments-end
- #include <ComboConstants.au3>
- #include <Crypt.au3>
- #include <GUIConstantsEx.au3>
- #include <MsgBoxConstants.au3>
- #include <StringConstants.au3>
- $PortableGUI = GUICreate("File Encryptor/Decryptor", 320, 100, -1, -1, True)
- $Encrypt = GUICtrlCreateButton("Encrypt", 20, 15, 130, 40)
- $Decrypt = GUICtrlCreateButton("Decrypt", 165, 15, 130, 40)
- GUISetState(@SW_SHOW)
- While 1
- Switch GUIGetMsg()
- Case $GUI_EVENT_CLOSE
- ExitLoop
- Case $Encrypt
- GUIDelete($PortableGUI)
- Encrypt()
- Case $Decrypt
- GUIDelete($PortableGUI)
- Decrypt()
- EndSwitch
- WEnd
- Func Encrypt()
- Local $iAlgorithm = $CALG_AES_256
- Local $hGUI = GUICreate("File Encrypter", 425, 100)
- Local $idSourceInput = GUICtrlCreateInput("", 5, 5, 200, 20)
- Local $idSourceBrowse = GUICtrlCreateButton("...", 210, 5, 35, 20)
- Local $idDestinationInput = GUICtrlCreateInput("", 5, 30, 200, 20)
- Local $idDestinationBrowse = GUICtrlCreateButton("...", 210, 30, 35, 20)
- GUICtrlCreateLabel("Password:", 5, 60, 200, 20)
- Local $idPasswordInput = GUICtrlCreateInput("", 5, 75, 200, 20)
- Local $idCombo = GUICtrlCreateCombo("", 210, 75, 100, 20, $CBS_DROPDOWNLIST)
- GUICtrlSetData($idCombo, "AES (256bit)", "AES (256bit)")
- Local $idEncrypt = 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 $idSourceBrowse
- $sFilePath = FileOpenDialog("Select a file to encrypt.", "", "All files (*.*)") ; Select a file to encrypt.
- If @error Then
- ContinueLoop
- EndIf
- GUICtrlSetData($idSourceInput, $sFilePath) ; Set the inputbox with the filepath.
- Case $idDestinationBrowse
- $sFilePath = FileSaveDialog("Save the file as ...", "", "All files (*.*)") ; Select a file to save the encrypted data to.
- If @error Then
- ContinueLoop
- EndIf
- GUICtrlSetData($idDestinationInput, $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 "AES (256bit)"
- $iAlgorithm = $CALG_AES_256
- EndSwitch
- Case $idEncrypt
- $sSourceRead = GUICtrlRead($idSourceInput) ; Read the source filepath input.
- $sDestinationRead = GUICtrlRead($idDestinationInput) ; Read the destination filepath input.
- $sPasswordRead = GUICtrlRead($idPasswordInput) ; 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 1
- 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 4 Or 5
- MsgBox($MB_SYSTEMMODAL, "Error", "Encryption 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.
- EXIT
- EndFunc ;==>Example
- Func Decrypt()
- Local $iAlgorithm = $CALG_AES_256
- Local $hGUI = GUICreate("File Decrypter", 425, 100)
- Local $idSourceInput = GUICtrlCreateInput("", 5, 5, 200, 20)
- Local $idSourceBrowse = GUICtrlCreateButton("...", 210, 5, 35, 20)
- Local $idDestinationInput = GUICtrlCreateInput("", 5, 30, 200, 20)
- Local $idDestinationBrowse = GUICtrlCreateButton("...", 210, 30, 35, 20)
- GUICtrlCreateLabel("Password:", 5, 60, 200, 20)
- Local $idPasswordInput = GUICtrlCreateInput("", 5, 75, 200, 20)
- Local $idCombo = GUICtrlCreateCombo("", 210, 75, 100, 20, $CBS_DROPDOWNLIST)
- GUICtrlSetData($idCombo, "AES (256bit)", "AES (256bit)")
- Local $idDecrypt = GUICtrlCreateButton("Decrypt", 355, 70, 65, 25)
- GUISetState(@SW_SHOW, $hGUI)
- Local $sDestinationRead = "", $sFilePath = "", $sPasswordRead = "", $sSourceRead = ""
- While 1
- Switch GUIGetMsg()
- Case $GUI_EVENT_CLOSE
- ExitLoop
- Case $idSourceBrowse
- $sFilePath = FileOpenDialog("Select a file to decrypt.", "", "All files (*.*)") ; Select a file to decrypt.
- If @error Then
- ContinueLoop
- EndIf
- GUICtrlSetData($idSourceInput, $sFilePath) ; Set the inputbox with the filepath.
- Case $idDestinationBrowse
- $sFilePath = FileSaveDialog("Save the file as ...", "", "All files (*.*)") ; Select a file to save the decrypted data to.
- If @error Then
- ContinueLoop
- EndIf
- GUICtrlSetData($idDestinationInput, $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 "AES (256bit)"
- $iAlgorithm = $CALG_AES_256
- EndSwitch
- Case $idDecrypt
- $sSourceRead = GUICtrlRead($idSourceInput) ; Read the source filepath input.
- $sDestinationRead = GUICtrlRead($idDestinationInput) ; Read the destination filepath input.
- $sPasswordRead = GUICtrlRead($idPasswordInput) ; 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 decrypt and a password has been set.
- If _Crypt_DecryptFile($sSourceRead, $sDestinationRead, $sPasswordRead, $iAlgorithm) Then ; Decrypt the file.
- MsgBox($MB_SYSTEMMODAL, "Success", "Operation succeeded.")
- Else
- Switch @error
- Case 1
- 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 4 Or 5
- MsgBox($MB_SYSTEMMODAL, "Error", "Decryption 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.
- EXIT
- EndFunc ;==>Example
Advertisement