jcpetu Posted March 4, 2018 Share Posted March 4, 2018 Hi people, I'm trying to include a password hint withing and encrypted file without success. Would any one help me please. thanks in advance and regards. expandcollapse popup#include <Crypt.au3> #include <File.au3> Global $FileToEncrypt = "FileToEncrypt.txt" Global $FileEncrypted = "EncryptedFile.txt" Global $FileEncryptedWithHint = "EncryptedFileWithHint.txt" Global $FileEncryptedWithOutHint = "EncryptedFileWithOutHint.txt" Global $FileDecrypted = "FileDecrypted.txt" Global $ALG = $CALG_AES_128 Global $PasswordHint = "Hint here" Global $PasswordToEncryptWith = "securepass" Encryptfile() DecryptFile() Func Encryptfile() While 1 $PasswordToEncryptWith = InputBox("Enter password", "Enter password that is used to encrypt the file.") If $PasswordToEncryptWith > "" Then ExitLoop EndIf $PasswordHint = InputBox("Enter a password hint", "Enter password hint that is used to remember the password.") If $PasswordHint > "" Then ExitLoop EndIf WEnd _Crypt_EncryptFile($FileToEncrypt, $FileEncrypted, $PasswordToEncryptWith, $ALG) $FileEncryptedWithHint = FileOpen($FileEncrypted, $FO_APPEND) FileWrite($FileEncryptedWithHint, @CRLF & $PasswordHint) FileClose($FileEncryptedWithHint) EndFunc ;==>Encryptfile Func DecryptFile() $FileOpen = FileOpen($FileEncryptedWithHint) $Hint = FileReadLine($FileEncryptedWithHint, -1) ;read last line password ConsoleWrite("Your hint is = " & $Hint & @CRLF) $FileRead = FileRead($FileOpen) $FileWithoutHint = StringReplace($FileRead, $Hint, "") FileClose($FileOpen) $FileToDecrypt=FileOpen($FileEncryptedWithOutHint) FileWrite($FileToDecrypt, $RemovedPassword) _Crypt_DecryptFile($FileEncryptedWithOutHint, $FileDecrypted, $PasswordToEncryptWith, $ALG) EndFunc ;==>DecryptFile Link to comment Share on other sites More sharing options...
Ascer Posted March 4, 2018 Share Posted March 4, 2018 @jcpetu Let's start with Encode function. I've write some for you. expandcollapse popup;=================================================================================================== ; Function: doEncryptFile($sFile [, $iAlgorithm = $ALG]) ; ; Description: Encode file using user password and store user passwdHint at end of file. ; Parameter(s): $sFile - string | Path to encrypted file. ; $iAlgorithm - int | (Default = $ALG) Algorithm type. ; ; Return(s): On Success set error to 0 and returns an 1D array ; [0] - Path to encrypted file. ; [1] - Password. ; [2] - PasswordHint. ; On Failure set error and return 1-6. {read Console for more info} ; ; Author(s): Ascer {created for jcpetu on AutoIt forums - 2018-03-04, 18:12} ;=================================================================================================== Func doCryptFile($sFile, $iAlgorithm = $ALG) ; Open Inputbox to enter crypt password. Local $sPassword = InputBox("Enter password", "Enter password that is used to encrypt the file.") ; Check if user click cancel button or inputbox is empty. If @error or $sPassword = "" Then ConsoleWrite("+++ Error 1. User click Cancel button or no data given." & @CRLF) Return SetError(1, @extended, 1) EndIf ; Open Inputbox to enter crypt password hint. Local $sPasswordHint = InputBox("Enter a password hint", "Enter password hint that is used to remember the password.") ; Check if user click cancel button or inputbox is empty. If @error or $sPasswordHint = "" Then ConsoleWrite("+++ Error 1. User click Cancel button or no data given." & @CRLF) Return SetError(1, @extended, 1) EndIf ; Get File name from Path. Local $sFileName = StringSplit($sFile, "\") ; Check for errors. If @error Then ConsoleWrite("+++ Error 2. Failed to find file name." & @CRLF) Return SetError(2, @extended, 2) EndIf ; Specify file name from spilt array. $sFileName = IsArray($sFileName) ? $sFileName[UBound($sFileName) - 1] : "" ; Check for spilt errors. If $sFileName = "" Then ConsoleWrite("+++ Error 3. Failed to get data from split array." & @CRLF) Return SetError(3, @extended, 1) EndIf ; Get File path removing file name Local $sFilePath = StringReplace($sFile, $sFileName, "") ; Create a File path for new Encrypted file. Local $sEncryptedFilePath = $sFilePath & "\Encrypted_" & $sFileName ; Check if file already exists then delete it! If FileExists($sEncryptedFilePath) Then FileDelete($sEncryptedFilePath) EndIf ; Crypt file and save as name of file and _encrypted _Crypt_EncryptFile($sFile, $sEncryptedFilePath, $sPassword, $ALG) ; Check for errors If @error Then ConsoleWrite("Error 4. Failed to encrypt file using func _Crypt_EncryptFile." & @CRLF) Return SetError(4, @extended, 4) EndIf ; Open a new encrypted file for append. Local $sEncryptedFile = FileOpen($sEncryptedFilePath, $FO_APPEND) ; Check for errors. If @error Then ConsoleWrite("Error 5. Failed to open a new encrypted file for reading." & @CRLF) Return SetError(5, @extended, 5) EndIf ; Append password at end of encrypted file. FileWrite($sEncryptedFile, $sPasswordHint) ; Check for errors. If @error Then ConsoleWrite("Error 6. Failed to append hint password." & @CRLF) Return SetError(5, @extended, 6) EndIf ; Prepare array for return. Local $aReturnArray = [$sEncryptedFilePath, $sPassword, $sPasswordHint] ; Return array. Return SetError(0, 0, $aReturnArray) EndFunc ;==>doCryptFile() Example how to use... #include <Crypt.au3> #include <File.au3> ; Put here path to your file Local $FILE_PATH = @ScriptDir & "\Data.txt" Local $aRet = doCryptFile($FILE_PATH) If Not @error Then ConsoleWrite("Successfully encoded file!" & @CRLF) ConsoleWrite("Return..." & @CRLF) ConsoleWrite("Path to file: " & $aRet[0] & @CRLF) ConsoleWrite("Password: " & $aRet[1] & @CRLF) ConsoleWrite("PasswordHint: " & $aRet[2] & @CRLF) EndIf Link to comment Share on other sites More sharing options...
jcpetu Posted March 4, 2018 Author Share Posted March 4, 2018 Hi Ascer, thanks for the response. Thing is that the procedure to store the hint is OK, but when I want to Decrypt the file I got a corrupted file filed with: ÿÿÿÿ Link to comment Share on other sites More sharing options...
Ascer Posted March 4, 2018 Share Posted March 4, 2018 @jcpetu You have to Cut passwordHint from end of Encrypted_File like this Local $PASSWORD_HINT = "sdfsdfsf" Local $PATH_TO_FILE = @ScriptDir & "\Encoded_Data.txt" Local $sEncodedFile = FileRead(FileOpen($PATH_TO_FILE)) Local $sEncodedData = StringTrimRight($sEncodedFile, StringLen($PASSWORD_HINT)) Link to comment Share on other sites More sharing options...
jcpetu Posted March 4, 2018 Author Share Posted March 4, 2018 Ascer, I added the code but I keep getting a corrupted file when decripted. expandcollapse popup#include <Crypt.au3> #include <File.au3> Global $ALG = $CALG_AES_128 Global $sEncryptedFilePath, $sPasswordHint Global $FileDecrypted = "FileDecrypted.txt" Global $sPassword ; Put here path to your file Global $FILE_PATH = @ScriptDir & "\Data.txt" Local $aRet = doCryptFile($FILE_PATH) If Not @error Then ConsoleWrite("Successfully encoded file!" & @CRLF) ConsoleWrite("Return..." & @CRLF) ConsoleWrite("Path to file: " & $aRet[0] & @CRLF) ConsoleWrite("Password: " & $aRet[1] & @CRLF) ConsoleWrite("PasswordHint: " & $aRet[2] & @CRLF) EndIf doDecryptFile($FILE_PATH) ;=================================================================================================== ; Function: doEncryptFile($sFile [, $iAlgorithm = $ALG]) ; ; Description: Encode file using user password and store user passwdHint at end of file. ; Parameter(s): $sFile - string | Path to encrypted file. ; $iAlgorithm - int | (Default = $ALG) Algorithm type. ; ; Return(s): On Success set error to 0 and returns an 1D array ; [0] - Path to encrypted file. ; [1] - Password. ; [2] - PasswordHint. ; On Failure set error and return 1-6. {read Console for more info} ; ; Author(s): Ascer {created for jcpetu on AutoIt forums - 2018-03-04, 18:12} ;=================================================================================================== Func doCryptFile($sFile, $iAlgorithm = $ALG) ; Open Inputbox to enter crypt password. $sPassword = InputBox("Enter password", "Enter password that is used to encrypt the file.") ; Check if user click cancel button or inputbox is empty. If @error Or $sPassword = "" Then ConsoleWrite("+++ Error 1. User click Cancel button or no data given." & @CRLF) Return SetError(1, @extended, 1) EndIf ; Open Inputbox to enter crypt password hint. $sPasswordHint = InputBox("Enter a password hint", "Enter password hint that is used to remember the password.") ; Check if user click cancel button or inputbox is empty. If @error Or $sPasswordHint = "" Then ConsoleWrite("+++ Error 1. User click Cancel button or no data given." & @CRLF) Return SetError(1, @extended, 1) EndIf ; Get File name from Path. Local $sFileName = StringSplit($sFile, "\") ; Check for errors. If @error Then ConsoleWrite("+++ Error 2. Failed to find file name." & @CRLF) Return SetError(2, @extended, 2) EndIf ; Specify file name from spilt array. $sFileName = IsArray($sFileName) ? $sFileName[UBound($sFileName) - 1] : "" ; Check for spilt errors. If $sFileName = "" Then ConsoleWrite("+++ Error 3. Failed to get data from split array." & @CRLF) Return SetError(3, @extended, 1) EndIf ; Get File path removing file name Local $sFilePath = StringReplace($sFile, $sFileName, "") ; Create a File path for new Encrypted file. $sEncryptedFilePath = $sFilePath & "\Encrypted_" & $sFileName ; Check if file already exists then delete it! If FileExists($sEncryptedFilePath) Then FileDelete($sEncryptedFilePath) EndIf ; Crypt file and save as name of file and _encrypted _Crypt_EncryptFile($sFile, $sEncryptedFilePath, $sPassword, $ALG) ; Check for errors If @error Then ConsoleWrite("Error 4. Failed to encrypt file using func _Crypt_EncryptFile." & @CRLF) Return SetError(4, @extended, 4) EndIf ; Open a new encrypted file for append. Local $sEncryptedFile = FileOpen($sEncryptedFilePath, $FO_APPEND) ; Check for errors. If @error Then ConsoleWrite("Error 5. Failed to open a new encrypted file for reading." & @CRLF) Return SetError(5, @extended, 5) EndIf ; Append password at end of encrypted file. FileWrite($sEncryptedFile, $sPasswordHint) ; Check for errors. If @error Then ConsoleWrite("Error 6. Failed to append hint password." & @CRLF) Return SetError(5, @extended, 6) EndIf ; Prepare array for return. Local $aReturnArray = [$sEncryptedFilePath, $sPassword, $sPasswordHint] ; Return array. Return SetError(0, 0, $aReturnArray) EndFunc ;==>doCryptFile Func doDecryptFile($sFile, $iAlgorithm = $ALG) $FileOpen = FileOpen($sEncryptedFilePath) $FileRead = FileRead($FileOpen) $RemovedHint = StringTrimRight($FileRead, StringLen($sPasswordHint)) FileWrite($FileOpen, $RemovedHint) FileClose($FileOpen) _Crypt_DecryptFile($sEncryptedFilePath, $FileDecrypted, $sPassword, $ALG) EndFunc ;==>doDecryptFile Link to comment Share on other sites More sharing options...
Ascer Posted March 5, 2018 Share Posted March 5, 2018 @jcpetu I digged this _Decrypt function and looks like has bugs in MSDN or AutoIt Crypt.au3. When you crypt file and save it example as Encrypted_Data.txt you can easy use _Decrypt function but... After edit file, example remove PasswordHint and save you will get output like a ". . . . " and error 420 {failed to decrypt final source} It meeans that function FileWrite change something in Encrypted_Data.txt that you cannot decrypt it. I've used other function _Decrypt_Data to decode just file content and save it as a new file Decrypted_Data.txt Additional: i've used function BinaryToString cuz output of _Decrypt_Data function is in BinaryString. This is full code... changed algorithm to AES_256 since ALG look like broken. expandcollapse popup;=================================================================================================== ; Function: doEncryptFile($sFile [, $iAlgorithm = 0x00006610]) ; ; Description: Encode file using user password and store user passwdHint at end of file. ; Parameter(s): $sFile - string | Path to encrypted file. ; $iAlgorithm - int | (Default = 0x00006610) Algorithm type. AES_256 ; ; Return(s): On Success set error to 0 and returns an 1D array ; [0] - Path to encrypted file. ; [1] - Password. ; [2] - PasswordHint. ; On Failure set error and return 1-6. {read Console for more info} ; ; Author(s): Ascer {created for jcpetu on AutoIt forums - 2018-03-04, 18:12} ;=================================================================================================== Func doCryptFile($sFile, $iAlgorithm = 0x00006610) ; Open Inputbox to enter crypt password. Local $sPassword = InputBox("Enter password", "Enter password that is used to encrypt the file.") ; Check if user click cancel button or inputbox is empty. If @error or $sPassword = "" Then ConsoleWrite("+++ Error 1. User click Cancel button or no data given." & @CRLF) Return SetError(1, @extended, 1) EndIf ; Open Inputbox to enter crypt password hint. Local $sPasswordHint = InputBox("Enter a password hint", "Enter password hint that is used to remember the password.") ; Check if user click cancel button or inputbox is empty. If @error or $sPasswordHint = "" Then ConsoleWrite("+++ Error 1. User click Cancel button or no data given." & @CRLF) Return SetError(1, @extended, 1) EndIf ; Get File name from Path. Local $sFileName = StringSplit($sFile, "\") ; Check for errors. If @error Then ConsoleWrite("+++ Error 2. Failed to find file name." & @CRLF) Return SetError(2, @extended, 2) EndIf ; Specify file name from spilt array. $sFileName = IsArray($sFileName) ? $sFileName[UBound($sFileName) - 1] : "" ; Check for spilt errors. If $sFileName = "" Then ConsoleWrite("+++ Error 3. Failed to get data from split array." & @CRLF) Return SetError(3, @extended, 1) EndIf ; Get File path removing file name Local $sFilePath = StringReplace($sFile, $sFileName, "") ; Create a File path for new Encrypted file. Local $sEncryptedFilePath = $sFilePath & "Encrypted_" & $sFileName ; Check if file already exists then delete it! If FileExists($sEncryptedFilePath) Then FileDelete($sEncryptedFilePath) EndIf ; Crypt file and save as name of file and _encrypted _Crypt_EncryptFile($sFile, $sEncryptedFilePath, $sPassword, $iAlgorithm) ; Check for errors If @error Then ConsoleWrite("Error 4. Failed to encrypt file using func _Crypt_EncryptFile." & @CRLF) Return SetError(4, @extended, 4) EndIf ; Open a new encrypted file for append. Local $sEncryptedFile = FileOpen($sEncryptedFilePath, $FO_APPEND) ; Check for errors. If @error Then ConsoleWrite("Error 5. Failed to open a new encrypted file for reading." & @CRLF) Return SetError(5, @extended, 5) EndIf ; Append password at end of encrypted file. FileWrite($sEncryptedFile, $sPasswordHint) ; Check for errors. If @error Then ConsoleWrite("Error 6. Failed to append hint password." & @CRLF) Return SetError(5, @extended, 6) EndIf ; Prepare array for return. Local $aReturnArray = [$sEncryptedFilePath, $sPassword, $sPasswordHint] ; Return array. Return SetError(0, 0, $aReturnArray) EndFunc ;==>doCryptFile() ;=================================================================================================== ; Function: doDecryptFile($sFile, $sPassword, $sPasswordHint [, $iAlgorithm = 0x00006610]) ; ; Description: Decode file using password and cut out hint password at at end of file. ; Parameter(s): $sFile - string | Path to encrypted file. ; $sPassword - string | Password do decrypt returned by doCryptFile()[1] ; $sPasswordHint - string | PasswordHint do decrypt returned by doCryptFile()[2] ; $iAlgorithm - int | (Default = 0x00006610) Algorithm type AES_256 ; ; Return(s): On Success set error to 0 and returns path to decrypted file ; On Failure set error and return 1-9. {read Console for more info} ; ; Author(s): Ascer {created for jcpetu on AutoIt forums - 2018-03-05, 08:02} ;=================================================================================================== Func doDecryptFile($sFile, $sPassword, $sPasswordHint, $iAlgorithm = 0x00006610) ; Open encrypted file. Local $sEncryptedFile = FileOpen($sFile) ; Check for errors. If @error Then ConsoleWrite("+++ Error 1. Failed to open file." & @CRLF) Return SetError(1, @extended, 1) EndIf ; Read file for data Local $sData = FileRead($sEncryptedFile) ; Check for errors. If @error or $sData = "" Then ConsoleWrite("+++ Error 2. Failed to read file or no data given." & @CRLF) Return SetError(2, @extended, 2) EndIf ; Get length of password hint for later usage Local $sPasswordHintLength = StringLen($sPasswordHint) ; Get password hint from file Local $sHint = StringRight($sData, $sPasswordHintLength) ; Check for errors. If @error or $sHint = "" Then ConsoleWrite("+++ Error 3. Failed to get password hint from file." & @CRLF) Return SetError(3, @extended, 3) EndIf ; Check valid password hint as parameter and cut from file. If $sHint <> $sPasswordHint Then ConsoleWrite("+++ Error 4. Invalid password hint in 3th parameter or failed to get hint from file." & @CRLF) Return SetError(4, @extended, 4) EndIf ; Get data to decrypt without hint. Local $sEncryptedData = StringTrimRight($sData, $sPasswordHintLength) ; Close current file FileClose($sEncryptedFile) ; Get File name from Path. Local $sFileName = StringSplit($sFile, "\") ; Check for errors. If @error Then ConsoleWrite("+++ Error 6. Failed to find file name." & @CRLF) Return SetError(6, @extended, 6) EndIf ; Specify file name from spilt array. $sFileName = IsArray($sFileName) ? $sFileName[UBound($sFileName) - 1] : "" ; Check for spilt errors. If $sFileName = "" Then ConsoleWrite("+++ Error 7. Failed to get data from split array." & @CRLF) Return SetError(7, @extended, 7) EndIf ; Get File path removing file name. Local $sFilePath = StringReplace($sFile, $sFileName, "") ; Get searching keywords. Local $sKeyword = "Encrypted_" Local $sReplaceKeyword = "Decrypted_" ; Check if encrypted file contains phrase $sKeyword Then replace to $sReplaceKeyword else add $sReplaceKeyword If StringInStr($sFileName, $sKeyword) Then $sFileName = StringReplace($sFileName, $sKeyword, $sReplaceKeyword) Else $sFileName = $sReplaceKeyword & $sFileName EndIf ; Create a File path for new Decrypted file. Local $sDecryptedFilePath = $sFilePath & $sFileName ; Check if file already exists then delete it! If FileExists($sDecryptedFilePath) Then FileDelete($sDecryptedFilePath) EndIf ; Decrypt data file using other function. Local $sDecryptData = BinaryToString(_Crypt_DecryptData($sEncryptedData, $sPassword, $iAlgorithm)) ; Check for errors. If @error Then ConsoleWrite("+++ Error 8. Failed to decrypt data file." & @CRLF) Return SetError(8, @extended, 8) EndIf ; Write data to new file. FileWrite($sDecryptedFilePath, $sDecryptData) ; Check for errors. If @error Then ConsoleWrite("+++ Error 9. Failed to write decrypt data into new file" & @CRLF) Return SetError(9, @extended, 9) EndIf ; Success with decrypt file return path. Return SetError(0, 0, $sDecryptedFilePath) EndFunc ;==>doCryptFile() ; Put here path to your file Local $FILE_PATH = @ScriptDir & "\Data.txt" Local $aRet = doCryptFile($FILE_PATH) If Not @error Then ConsoleWrite("Successfully encoded file!" & @CRLF) ConsoleWrite("Return..." & @CRLF) ConsoleWrite("Path to encrypted file: " & $aRet[0] & @CRLF) ConsoleWrite("Password: " & $aRet[1] & @CRLF) ConsoleWrite("PasswordHint: " & $aRet[2] & @CRLF) EndIf Local $sRet = doDecryptFile($aRet[0], $aRet[1], $aRet[2]) If Not @error Then ConsoleWrite("Successfully decoded file!" & @CRLF) ConsoleWrite("Return..." & @CRLF) ConsoleWrite("Path to decrypted file: " & $sRet & @CRLF) EndIf jcpetu 1 Link to comment Share on other sites More sharing options...
jcpetu Posted March 5, 2018 Author Share Posted March 5, 2018 Hi Ascer, first of all I really appreciate the time and effort you dedicate to respond so thank you. unfortunately when the file is decripted it works properly for a text file with only one line of text, but in other cases of Word files or files of over 200 lines of text it shows some error related with the hint ( +++ Error 4. Invalid password hint in 3th parameter or failed to get hint from file., etc) I attach the code you provide me with some slight modifications just for you to see if you want, thanks again. expandcollapse popup#include <Crypt.au3> #include <File.au3> Global $sPassword="" Global $sPasswordHint="" ; Put here path to your file ;Global $FILE_PATH = @ScriptDir & "\2018-01-08.docx" ; Microsoft Word NOK --------------------------------------------------- ;Global $FILE_PATH = @ScriptDir & "\Test1.txt" ; five line of text NOK ------------------------------------------------------ ;Global $FILE_PATH = @ScriptDir & "\Test2.txt" ; one line of text OK -------------------------------------------------------- Global $FILE_PATH = @ScriptDir & "\Data.txt" ;over 200 lines of text NOK ---------------------------------------------------- Global $aRet = doCryptFile($FILE_PATH) If Not @error Then ConsoleWrite("Successfully encoded file!" & @CRLF) ConsoleWrite("Return..." & @CRLF) ConsoleWrite("Path to encrypted file: " & $aRet[0] & @CRLF) ConsoleWrite("Password: " & $aRet[1] & @CRLF) ConsoleWrite("PasswordHint: " & $aRet[2] & @CRLF) EndIf $sRet = doDecryptFile($aRet[0], $aRet[1], $aRet[2]) ConsoleWrite("file path="&$aRet[0]&" "&"Password="& $aRet[1]&" "&"Hint="& $aRet[2]&@CRLF) If Not @error Then ConsoleWrite("Successfully decoded file!" & @CRLF) ConsoleWrite("Return..." & @CRLF) ConsoleWrite("Path to decrypted file: " & $sRet & @CRLF) EndIf ;=================================================================================================== ; Function: doEncryptFile($sFile [, $iAlgorithm = 0x00006610]) ; ; Description: Encode file using user password and store user passwdHint at end of file. ; Parameter(s): $sFile - string | Path to encrypted file. ; $iAlgorithm - int | (Default = 0x00006610) Algorithm type. AES_256 ; ; Return(s): On Success set error to 0 and returns an 1D array ; [0] - Path to encrypted file. ; [1] - Password. ; [2] - PasswordHint. ; On Failure set error and return 1-6. {read Console for more info} ; ; Author(s): Ascer {created for jcpetu on AutoIt forums - 2018-03-04, 18:12} ;=================================================================================================== Func doCryptFile($sFile, $iAlgorithm = 0x00006610) ; Open Inputbox to enter crypt password. $sPassword = InputBox("Enter password", "Enter password that is used to encrypt the file.") ; Check if user click cancel button or inputbox is empty. If @error Or $sPassword = "" Then ConsoleWrite("+++ Error 1. User click Cancel button or no data given." & @CRLF) Return SetError(1, @extended, 1) EndIf ; Open Inputbox to enter crypt password hint. $sPasswordHint = InputBox("Enter a password hint", "Enter password hint that is used to remember the password.") ; Check if user click cancel button or inputbox is empty. If @error Or $sPasswordHint = "" Then ConsoleWrite("+++ Error 1. User click Cancel button or no data given." & @CRLF) Return SetError(1, @extended, 1) EndIf ; Get File name from Path. Local $sFileName = StringSplit($sFile, "\") ; Check for errors. If @error Then ConsoleWrite("+++ Error 2. Failed to find file name." & @CRLF) Return SetError(2, @extended, 2) EndIf ; Specify file name from spilt array. $sFileName = IsArray($sFileName) ? $sFileName[UBound($sFileName) - 1] : "" ; Check for spilt errors. If $sFileName = "" Then ConsoleWrite("+++ Error 3. Failed to get data from split array." & @CRLF) Return SetError(3, @extended, 1) EndIf ; Get File path removing file name Local $sFilePath = StringReplace($sFile, $sFileName, "") ; Create a File path for new Encrypted file. Local $sEncryptedFilePath = $sFilePath & "Encrypted_" & $sFileName ; Check if file already exists then delete it! If FileExists($sEncryptedFilePath) Then FileDelete($sEncryptedFilePath) EndIf ; Crypt file and save as name of file and _encrypted _Crypt_EncryptFile($sFile, $sEncryptedFilePath, $sPassword, $iAlgorithm) ; Check for errors If @error Then ConsoleWrite("Error 4. Failed to encrypt file using func _Crypt_EncryptFile." & @CRLF) Return SetError(4, @extended, 4) EndIf ; Open a new encrypted file for append. Local $sEncryptedFile = FileOpen($sEncryptedFilePath, $FO_APPEND) ; Check for errors. If @error Then ConsoleWrite("Error 5. Failed to open a new encrypted file for reading." & @CRLF) Return SetError(5, @extended, 5) EndIf ; Append password at end of encrypted file. FileWrite($sEncryptedFile, $sPasswordHint) ; Check for errors. If @error Then ConsoleWrite("Error 6. Failed to append hint password." & @CRLF) Return SetError(5, @extended, 6) EndIf ; Prepare array for return. Local $aReturnArray = [$sEncryptedFilePath, $sPassword, $sPasswordHint] ; Return array. Return SetError(0, 0, $aReturnArray) EndFunc ;==>doCryptFile ;=================================================================================================== ; Function: doDecryptFile($sFile, $sPassword, $sPasswordHint [, $iAlgorithm = 0x00006610]) ; ; Description: Decode file using password and cut out hint password at at end of file. ; Parameter(s): $sFile - string | Path to encrypted file. ; $sPassword - string | Password do decrypt returned by doCryptFile()[1] ; $sPasswordHint - string | PasswordHint do decrypt returned by doCryptFile()[2] ; $iAlgorithm - int | (Default = 0x00006610) Algorithm type AES_256 ; ; Return(s): On Success set error to 0 and returns path to decrypted file ; On Failure set error and return 1-9. {read Console for more info} ; ; Author(s): Ascer {created for jcpetu on AutoIt forums - 2018-03-05, 08:02} ;=================================================================================================== Func doDecryptFile($sFile, $sPassword, $sPasswordHint, $iAlgorithm = 0x00006610) ; Open encrypted file. Local $sEncryptedFile = FileOpen($sFile) ; Check for errors. If @error Then ConsoleWrite("+++ Error 1. Failed to open file." & @CRLF) Return SetError(1, @extended, 1) EndIf ; Read file for data Local $sData = FileRead($sEncryptedFile) ;ConsoleWrite("$sData="&$sData&@CRLF) ; Check for errors. If @error Or $sData = "" Then ConsoleWrite("+++ Error 2. Failed to read file or no data given." & @CRLF) Return SetError(2, @extended, 2) EndIf ; Get length of password hint for later usage Local $sPasswordHintLength = StringLen($sPasswordHint) ;ConsoleWrite("$sPasswordHintLength="&$sPasswordHintLength&@CRLF) ; Get password hint from file Local $sHint = StringRight($sData, $sPasswordHintLength) ;ConsoleWrite("$sHint="&$sHint&@CRLF) ; Check for errors. If @error Or $sHint = "" Then ConsoleWrite("+++ Error 3. Failed to get password hint from file." & @CRLF) Return SetError(3, @extended, 3) EndIf ; Check valid password hint as parameter and cut from file. If $sHint <> $sPasswordHint Then ConsoleWrite("+++ Error 4. Invalid password hint in 3th parameter or failed to get hint from file." & @CRLF) Return SetError(4, @extended, 4) EndIf ; Get data to decrypt without hint. Local $sEncryptedData = StringTrimRight($sData, $sPasswordHintLength) ; Close current file FileClose($sEncryptedFile) ; Get File name from Path. Local $sFileName = StringSplit($sFile, "\") ; Check for errors. If @error Then ConsoleWrite("+++ Error 6. Failed to find file name." & @CRLF) Return SetError(6, @extended, 6) EndIf ; Specify file name from spilt array. $sFileName = IsArray($sFileName) ? $sFileName[UBound($sFileName) - 1] : "" ; Check for spilt errors. If $sFileName = "" Then ConsoleWrite("+++ Error 7. Failed to get data from split array." & @CRLF) Return SetError(7, @extended, 7) EndIf ; Get File path removing file name. Local $sFilePath = StringReplace($sFile, $sFileName, "") ; Get searching keywords. Local $sKeyword = "Encrypted_" Local $sReplaceKeyword = "Decrypted_" ; Check if encrypted file contains phrase $sKeyword Then replace to $sReplaceKeyword else add $sReplaceKeyword If StringInStr($sFileName, $sKeyword) Then $sFileName = StringReplace($sFileName, $sKeyword, $sReplaceKeyword) Else $sFileName = $sReplaceKeyword & $sFileName EndIf ; Create a File path for new Decrypted file. Local $sDecryptedFilePath = $sFilePath & $sFileName ; Check if file already exists then delete it! If FileExists($sDecryptedFilePath) Then FileDelete($sDecryptedFilePath) EndIf ; Decrypt data file using other function. Local $sDecryptData = BinaryToString(_Crypt_DecryptData($sEncryptedData, $sPassword, $iAlgorithm)) ; Check for errors. If @error Then ConsoleWrite("+++ Error 8. Failed to decrypt data file." & @CRLF) Return SetError(8, @extended, 8) EndIf ; Write data to new file. FileWrite($sDecryptedFilePath, $sDecryptData) ; Check for errors. If @error Then ConsoleWrite("+++ Error 9. Failed to write decrypt data into new file" & @CRLF) Return SetError(9, @extended, 9) EndIf ; Success with decrypt file return path. Return SetError(0, 0, $sDecryptedFilePath) EndFunc ;==>doDecryptFile regards. Link to comment Share on other sites More sharing options...
Ascer Posted March 6, 2018 Share Posted March 6, 2018 @jcpetu Try to play with this code may be useful #include <Crypt.au3> ConsoleWrite(Main()) Func Main() Local $sData = FileRead(FileOpen(@ScriptDir & "\Data.txt")) Local $sPassword = "hello" Local $sPasswordHint = "123" Local $sCrypt = _Crypt_EncryptData($sData, $sPassword, 0x00006610) If @error Then Return SetError(@error, @extended, 1) EndIf Local $sCryptAddHint = $sCrypt & $sPasswordHint $sCrypt = Binary($sCrypt) Local $sTrimCrypt = StringTrimRight($sCryptAddHint, StringLen($sCryptAddHint) - StringLen($sCrypt)) Local $sDecrypt = _Crypt_DecryptData($sTrimCrypt, $sPassword, 0x00006610) If @error Then Return SetError(@error, @extended, 2) EndIf Return BinaryToString($sDecrypt) EndFunc Link to comment Share on other sites More sharing options...
jcpetu Posted March 6, 2018 Author Share Posted March 6, 2018 Ascer, I will, thanks a lot and regards. Link to comment Share on other sites More sharing options...
jcpetu Posted March 6, 2018 Author Share Posted March 6, 2018 Ascer, I was playing around with the function and I found that it's possible to encrypt data, write to a file and then uncrypt it. But it only works with some type of files, eg: it works OK with .docx, au3, txt, xlsx but it doesn't work with ppt and pdf for instance. It seems that in some cases Autoit Filewrite function encodes the file in a different manner than the original application. Thought it is not an alternative to include the hint in the file. Thanks again. Link to comment Share on other sites More sharing options...
Jowy Posted March 7, 2018 Share Posted March 7, 2018 @jcpetu, always try to convert everything to binary before working on any file. Working with binary, you would convert whatever file you want and also any kind of file can be be used and for instance ppt and pdf. However, for your Hint , convert it to binary and add it to the end of the file. When reading/writing file, use the $FO_BINARY flag in the read/write file. In that case you will succeed. But, make sure the number of bytes read/written when encrypting and decrypting a file are the same. If I have time, I could write for you a function showing you this behavior, but I'm just giving you a hint . Link to comment Share on other sites More sharing options...
jcpetu Posted March 7, 2018 Author Share Posted March 7, 2018 Thanks Jowy, I'll try it. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now