James Posted February 17, 2008 Share Posted February 17, 2008 (edited) Hi,Just a modified version of _ReplaceStringInFile() which encrypts the file. Lately I have been interested in using the command line and have come up with some functions for it such as, reading the output without using STDOut etc.Here is the code:expandcollapse popup#include <String.au3> If @Compiled Then Switch $CmdLine[0] Case 0 MsgBox(0, @ScriptName, "FileEncrypter.exe by James Brooks!") Case 3 $sMode = ($CmdLine[1]); /Encrypt or /Decrypt $sFile = $CmdLine[2]; The file to work on $sKey = $CmdLine[3]; The key to use Case Else MsgBox(4096 + 48, @ScriptName, "Error - Commandline Usage:" & @LF & @LF & 'FileEncrypter.exe /encrypt <FileName> key' & @LF & _ 'FileEncrypter.exe /decrypt <FileName> key') EndSwitch EndIf Switch StringUpper($sMode) Case "/ENCRYPT" Local $iRetVal = 0 Local $sFile, $aFileLines, $nCount, $sEndsWith, $hFile ; Check if file is readonly .. If StringInStr(FileGetAttrib($sFile), "R") Then SetError(6) EndIf ;=============================================================================== ;== Read the file into an array ;=============================================================================== $hFile = FileOpen($sFile, 0) If $hFile = -1 Then SetError(1) EndIf Local $s_TotFile = FileRead($hFile, FileGetSize($sFile)) If StringRight($s_TotFile, 2) = @CRLF Then $sEndsWith = @CRLF ElseIf StringRight($s_TotFile, 1) = @CR Then $sEndsWith = @CR ElseIf StringRight($s_TotFile, 1) = @LF Then $sEndsWith = @LF Else $sEndsWith = "" EndIf $aFileLines = StringSplit(StringStripCR($s_TotFile), @LF) FileClose($hFile) ;=============================================================================== ;== Open the output file in write mode ;=============================================================================== $hWriteHandle = FileOpen($sFile, 2) If $hWriteHandle = -1 Then SetError(2) EndIf ;=============================================================================== ;== Loop through the array and search for $szSearchString ;=============================================================================== For $nCount = 1 To $aFileLines[0] $aFileLines[$nCount] = _StringEncrypt(1, $aFileLines[$nCount], $sKey) $iRetVal = $iRetVal + 1 Next ;=============================================================================== ;== Write the lines back to original file. ;=============================================================================== For $nCount = 1 To $aFileLines[0] - 1 If FileWriteLine($hWriteHandle, $aFileLines[$nCount]) = 0 Then SetError(3) FileClose($hWriteHandle) EndIf Next ; Write the last record and ensure it ends with the same as the input file If $aFileLines[$nCount] <> "" Then FileWrite($hWriteHandle, $aFileLines[$nCount] & $sEndsWith) FileClose($hWriteHandle) Case "/DECRYPT" Local $iRetVal = 0 Local $sFile, $aFileLines, $nCount, $sEndsWith, $hFile ; Check if file is readonly .. If StringInStr(FileGetAttrib($sFile), "R") Then SetError(6) EndIf ;=============================================================================== ;== Read the file into an array ;=============================================================================== $hFile = FileOpen($sFile, 0) If $hFile = -1 Then SetError(1) EndIf Local $s_TotFile = FileRead($hFile, FileGetSize($sFile)) If StringRight($s_TotFile, 2) = @CRLF Then $sEndsWith = @CRLF ElseIf StringRight($s_TotFile, 1) = @CR Then $sEndsWith = @CR ElseIf StringRight($s_TotFile, 1) = @LF Then $sEndsWith = @LF Else $sEndsWith = "" EndIf $aFileLines = StringSplit(StringStripCR($s_TotFile), @LF) FileClose($hFile) ;=============================================================================== ;== Open the output file in write mode ;=============================================================================== $hWriteHandle = FileOpen($sFile, 2) If $hWriteHandle = -1 Then SetError(2) EndIf ;=============================================================================== ;== Loop through the array and search for $szSearchString ;=============================================================================== For $nCount = 1 To $aFileLines[0] $aFileLines[$nCount] = _StringEncrypt(0, $aFileLines[$nCount], $sKey) $iRetVal = $iRetVal + 1 Next ;=============================================================================== ;== Write the lines back to original file. ;=============================================================================== For $nCount = 1 To $aFileLines[0] - 1 If FileWriteLine($hWriteHandle, $aFileLines[$nCount]) = 0 Then SetError(3) FileClose($hWriteHandle) EndIf Next ; Write the last record and ensure it ends with the same as the input file If $aFileLines[$nCount] <> "" Then FileWrite($hWriteHandle, $aFileLines[$nCount] & $sEndsWith) FileClose($hWriteHandle) EndSwitchInstructions:Compile as FileEncrypter.exeOpen a CMD windowChange the directory to where the compiled FileEncrypter isType: FileEncrypter /encrypt <filename> <key>Open the encrypted fileTo decrypt just do: FileEncrypter /decrypt <filename> <key>If the file you want to encrypt/decrypt is in the same folder as FileEncrypter you do not have to write the whole location of the file:Let's say there is a file called helloworld.txt in My DocumentsWe want to encrypt that file so we put FileEncrypter.exe in the Windows dir and do the command:FileEncrypter /encrypt helloworld.txt AutoItEasy!To-Do:Add registry settings so you can encrypt and decrypt files by right clicking themInstall and Un-installNeaten encryption code - Change _ReplaceStringInFile() edited with my own versionMake the encryption code a function - Reduces code length!Edit: Removed old include (File.au3)Thank you for reading,James Edited February 17, 2008 by JamesB Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
icadea Posted February 18, 2008 Share Posted February 18, 2008 thanks. Link to comment Share on other sites More sharing options...
TPaul81 Posted February 18, 2008 Share Posted February 18, 2008 Excellent work buddy. My Scripts (so far)Myspace Layout EditorAnother Boss ScreenClipboard Collector Link to comment Share on other sites More sharing options...
James Posted February 18, 2008 Author Share Posted February 18, 2008 Thanks! I'm having a problem with making script work like a function. I will have to go back and try again later Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ 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