TheChiefMeat

AutoIt AES-256 File Encryptor-Decryptor

Jul 18th, 2017
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 8.50 KB | None | 0 0
  1. #comments-start
  2. File Encryptor/Decryptor. This code simply combines both functions into a single usable app.
  3. Have removed all but AES-256 encryption methods.
  4. Original code can be below at the following links:
  5.  
  6. Encrypt Function: https://www.autoitscript.com/autoit3/docs/libfunctions/_Crypt_EncryptFile.htm
  7. Decrypt Function: https://www.autoitscript.com/autoit3/docs/libfunctions/_Crypt_DecryptFile.htm
  8.  
  9. Compiled binary can be found at: https://bitbucket.org/TheChiefMeat/file-encryptor-decryptor/downloads/File%20Encryptor-Decryptor.zip
  10. #comments-end
  11.  
  12. #include <ComboConstants.au3>
  13. #include <Crypt.au3>
  14. #include <GUIConstantsEx.au3>
  15. #include <MsgBoxConstants.au3>
  16. #include <StringConstants.au3>
  17.  
  18. $PortableGUI = GUICreate("File Encryptor/Decryptor", 320, 100, -1, -1, True)
  19. $Encrypt = GUICtrlCreateButton("Encrypt", 20, 15, 130, 40)
  20. $Decrypt = GUICtrlCreateButton("Decrypt", 165, 15, 130, 40)
  21. GUISetState(@SW_SHOW)
  22.  
  23.     While 1
  24.         Switch GUIGetMsg()
  25.             Case $GUI_EVENT_CLOSE
  26.                 ExitLoop
  27.             Case $Encrypt
  28.             GUIDelete($PortableGUI)
  29.             Encrypt()
  30.             Case $Decrypt
  31.             GUIDelete($PortableGUI)
  32.             Decrypt()
  33.         EndSwitch
  34.     WEnd
  35.  
  36. Func Encrypt()
  37.     Local $iAlgorithm = $CALG_AES_256
  38.  
  39.     Local $hGUI = GUICreate("File Encrypter", 425, 100)
  40.     Local $idSourceInput = GUICtrlCreateInput("", 5, 5, 200, 20)
  41.     Local $idSourceBrowse = GUICtrlCreateButton("...", 210, 5, 35, 20)
  42.  
  43.     Local $idDestinationInput = GUICtrlCreateInput("", 5, 30, 200, 20)
  44.     Local $idDestinationBrowse = GUICtrlCreateButton("...", 210, 30, 35, 20)
  45.  
  46.     GUICtrlCreateLabel("Password:", 5, 60, 200, 20)
  47.     Local $idPasswordInput = GUICtrlCreateInput("", 5, 75, 200, 20)
  48.  
  49.     Local $idCombo = GUICtrlCreateCombo("", 210, 75, 100, 20, $CBS_DROPDOWNLIST)
  50.     GUICtrlSetData($idCombo, "AES (256bit)", "AES (256bit)")
  51.     Local $idEncrypt = GUICtrlCreateButton("Encrypt", 355, 70, 65, 25)
  52.     GUISetState(@SW_SHOW, $hGUI)
  53.  
  54.     Local $sDestinationRead = "", $sFilePath = "", $sPasswordRead = "", $sSourceRead = ""
  55.     While 1
  56.         Switch GUIGetMsg()
  57.             Case $GUI_EVENT_CLOSE
  58.                 ExitLoop
  59.  
  60.             Case $idSourceBrowse
  61.                 $sFilePath = FileOpenDialog("Select a file to encrypt.", "", "All files (*.*)") ; Select a file to encrypt.
  62.                 If @error Then
  63.                     ContinueLoop
  64.                 EndIf
  65.                 GUICtrlSetData($idSourceInput, $sFilePath) ; Set the inputbox with the filepath.
  66.  
  67.             Case $idDestinationBrowse
  68.                 $sFilePath = FileSaveDialog("Save the file as ...", "", "All files (*.*)") ; Select a file to save the encrypted data to.
  69.                 If @error Then
  70.                     ContinueLoop
  71.                 EndIf
  72.                 GUICtrlSetData($idDestinationInput, $sFilePath) ; Set the inputbox with the filepath.
  73.  
  74.             Case $idCombo ; Check when the combobox is selected and retrieve the correct algorithm.
  75.                 Switch GUICtrlRead($idCombo) ; Read the combobox selection.
  76.  
  77.                     Case "AES (256bit)"
  78.                         $iAlgorithm = $CALG_AES_256
  79.                     EndSwitch
  80.  
  81.             Case $idEncrypt
  82.                 $sSourceRead = GUICtrlRead($idSourceInput) ; Read the source filepath input.
  83.                 $sDestinationRead = GUICtrlRead($idDestinationInput) ; Read the destination filepath input.
  84.                 $sPasswordRead = GUICtrlRead($idPasswordInput) ; Read the password input.
  85.                 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.
  86.                     If _Crypt_EncryptFile($sSourceRead, $sDestinationRead, $sPasswordRead, $iAlgorithm) Then ; Encrypt the file.
  87.                         MsgBox($MB_SYSTEMMODAL, "Success", "Operation succeeded.")
  88.                     Else
  89.                         Switch @error
  90.                             Case 1
  91.                                 MsgBox($MB_SYSTEMMODAL, "Error", "Failed to create the key.")
  92.                             Case 2
  93.                                 MsgBox($MB_SYSTEMMODAL, "Error", "Couldn't open the source file.")
  94.                             Case 3
  95.                                 MsgBox($MB_SYSTEMMODAL, "Error", "Couldn't open the destination file.")
  96.                             Case 4 Or 5
  97.                                 MsgBox($MB_SYSTEMMODAL, "Error", "Encryption error.")
  98.                         EndSwitch
  99.                     EndIf
  100.                 Else
  101.                     MsgBox($MB_SYSTEMMODAL, "Error", "Please ensure the relevant information has been entered correctly.")
  102.                 EndIf
  103.         EndSwitch
  104.     WEnd
  105.  
  106.     GUIDelete($hGUI) ; Delete the previous GUI and all controls.
  107.     EXIT
  108. EndFunc   ;==>Example
  109.  
  110. Func Decrypt()
  111.     Local $iAlgorithm = $CALG_AES_256
  112.  
  113.     Local $hGUI = GUICreate("File Decrypter", 425, 100)
  114.     Local $idSourceInput = GUICtrlCreateInput("", 5, 5, 200, 20)
  115.     Local $idSourceBrowse = GUICtrlCreateButton("...", 210, 5, 35, 20)
  116.  
  117.     Local $idDestinationInput = GUICtrlCreateInput("", 5, 30, 200, 20)
  118.     Local $idDestinationBrowse = GUICtrlCreateButton("...", 210, 30, 35, 20)
  119.  
  120.     GUICtrlCreateLabel("Password:", 5, 60, 200, 20)
  121.     Local $idPasswordInput = GUICtrlCreateInput("", 5, 75, 200, 20)
  122.  
  123.     Local $idCombo = GUICtrlCreateCombo("", 210, 75, 100, 20, $CBS_DROPDOWNLIST)
  124.     GUICtrlSetData($idCombo, "AES (256bit)", "AES (256bit)")
  125.     Local $idDecrypt = GUICtrlCreateButton("Decrypt", 355, 70, 65, 25)
  126.     GUISetState(@SW_SHOW, $hGUI)
  127.  
  128.     Local $sDestinationRead = "", $sFilePath = "", $sPasswordRead = "", $sSourceRead = ""
  129.     While 1
  130.         Switch GUIGetMsg()
  131.             Case $GUI_EVENT_CLOSE
  132.                 ExitLoop
  133.  
  134.             Case $idSourceBrowse
  135.                 $sFilePath = FileOpenDialog("Select a file to decrypt.", "", "All files (*.*)") ; Select a file to decrypt.
  136.                 If @error Then
  137.                     ContinueLoop
  138.                 EndIf
  139.                 GUICtrlSetData($idSourceInput, $sFilePath) ; Set the inputbox with the filepath.
  140.  
  141.             Case $idDestinationBrowse
  142.                 $sFilePath = FileSaveDialog("Save the file as ...", "", "All files (*.*)") ; Select a file to save the decrypted data to.
  143.                 If @error Then
  144.                     ContinueLoop
  145.                 EndIf
  146.                 GUICtrlSetData($idDestinationInput, $sFilePath) ; Set the inputbox with the filepath.
  147.  
  148.             Case $idCombo ; Check when the combobox is selected and retrieve the correct algorithm.
  149.                 Switch GUICtrlRead($idCombo) ; Read the combobox selection.
  150.                     Case "AES (256bit)"
  151.                         $iAlgorithm = $CALG_AES_256
  152.                     EndSwitch
  153.  
  154.             Case $idDecrypt
  155.                 $sSourceRead = GUICtrlRead($idSourceInput) ; Read the source filepath input.
  156.                 $sDestinationRead = GUICtrlRead($idDestinationInput) ; Read the destination filepath input.
  157.                 $sPasswordRead = GUICtrlRead($idPasswordInput) ; Read the password input.
  158.                 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.
  159.                     If _Crypt_DecryptFile($sSourceRead, $sDestinationRead, $sPasswordRead, $iAlgorithm) Then ; Decrypt the file.
  160.                         MsgBox($MB_SYSTEMMODAL, "Success", "Operation succeeded.")
  161.                     Else
  162.                         Switch @error
  163.                             Case 1
  164.                                 MsgBox($MB_SYSTEMMODAL, "Error", "Failed to create the key.")
  165.                             Case 2
  166.                                 MsgBox($MB_SYSTEMMODAL, "Error", "Couldn't open the source file.")
  167.                             Case 3
  168.                                 MsgBox($MB_SYSTEMMODAL, "Error", "Couldn't open the destination file.")
  169.                             Case 4 Or 5
  170.                                 MsgBox($MB_SYSTEMMODAL, "Error", "Decryption error.")
  171.                         EndSwitch
  172.                     EndIf
  173.                 Else
  174.                     MsgBox($MB_SYSTEMMODAL, "Error", "Please ensure the relevant information has been entered correctly.")
  175.                 EndIf
  176.         EndSwitch
  177.     WEnd
  178.  
  179.     GUIDelete($hGUI) ; Delete the previous GUI and all controls.
  180.     EXIT
  181. EndFunc   ;==>Example
Advertisement