Jump to content

Recommended Posts

Posted

I made this script and it does work, but coming from more skilled coders, what do you think to my coding choices/style? Anything redundant/sloppy/unoptimized? I know this is only a test for myself to test out the encryption, but it is functional and has more than that demo one they had on it. I coded this all from scratch. One thing I noticed though is if I decrypt an AES file with the wrong password, it flags an error yet does not flag an error if I do the same with RC4. Either thats a coding issue on my end or thats the AES system knowing it decrypted incorrectly due to a wrong password.

#include <Crypt.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <file.au3>

Global $drive
Global $dir
Global $filename
Global $ext
Global $alg = $CALG_AES_256

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 291, 119, 192, 124)
$Label1 = GUICtrlCreateLabel("File Encryption and Decryption", 24, 16, 249, 24)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$encrypt = GUICtrlCreateButton("Encrypt a file", 24, 56, 75, 25, BitOR($WS_GROUP, $BS_DEFPUSHBUTTON))
$decrypt = GUICtrlCreateButton("Decrypt a file", 192, 56, 75, 25, $WS_GROUP)
$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
$AES = GUICtrlCreateRadio("AES (Win XP and above)", 24, 38, 140, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$RC4 = GUICtrlCreateRadio("RC4", 194, 38, 128, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $encrypt
            Encryptfile()

        Case $decrypt
            Decryptfile()

        Case $RC4
            ;MsgBox(0,'','RC4')
            $alg = $CALG_RC4

        Case $AES
            ;MsgBox(0,'','AES')
            $alg = $CALG_AES_256


    EndSwitch
WEnd





Func Decryptfile()
    $openfile = FileOpenDialog('Choose file to decrypt', @MyDocumentsDir, 'All Files (*.*)', 3)
    If $openfile <> '' Then
        $pathsplit = _PathSplit($openfile, $drive, $dir, $filename, $ext)
        $password = InputBox('Password', 'Enter the password', '', '*')
        $key = _Crypt_HashData($password, $CALG_SHA1)
        $savefile = FileSaveDialog('Save as', @MyDocumentsDir, 'All Files (*' & $pathsplit[4] & ')', 16)
        $pathsplitsave = _PathSplit($savefile, $drive, $dir, $filename, $ext)
        If $pathsplitsave[4] <> $pathsplit[4] Then $savefile = $savefile & $pathsplit[4]

        If $savefile <> '' Then
            If $openfile <> $savefile Then
                _Crypt_DecryptFile($openfile, $savefile, $key, $alg)
                If @error Then MsgBox(0, 'Error', 'There was an errror.')
            Else
                MsgBox(0, 'Error', 'You cannot overwrite the original file with the new file. Save as a different name.')
            EndIf
        EndIf


    EndIf



EndFunc   ;==>Decryptfile







Func Encryptfile()
    $openfile = FileOpenDialog('Choose file to encrypt', @MyDocumentsDir, 'All Files (*.*)', 3)
    If $openfile <> '' Then
        $pathsplit = _PathSplit($openfile, $drive, $dir, $filename, $ext)
        $password = InputBox('Password', 'Enter the password', '', '*')
        $key = _Crypt_HashData($password, $CALG_SHA1)
        $savefile = FileSaveDialog('Save as', @MyDocumentsDir, 'File (*' & $pathsplit[4] & ')', 16)
        $pathsplitsave = _PathSplit($savefile, $drive, $dir, $filename, $ext)
        If $pathsplitsave[4] <> $pathsplit[4] Then $savefile = $savefile & $pathsplit[4]




        If $savefile <> '' Then
            If $openfile <> $savefile Then
                _Crypt_EncryptFile($openfile, $savefile, $key, $alg)
                If @error Then MsgBox(0, 'Error', 'There was an errror.')
            Else
                MsgBox(0, 'Error', 'You cannot overwrite the original file with the new file. Save as a different name.')
            EndIf

        EndIf

    EndIf



EndFunc   ;==>Encryptfile
Posted (edited)

Add #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 to the top of your Script to see if all variables are declared correctly.

Edited by guinness

UDF List:

 
_AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples...

Updated: 22/04/2018

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...