Jump to content

CLI FileEncrypter


James
 Share

Recommended Posts

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:

#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)
EndSwitch

Instructions:

  • Compile as FileEncrypter.exe
  • Open a CMD window
  • Change the directory to where the compiled FileEncrypter is
  • Type: FileEncrypter /encrypt <filename> <key>
  • Open the encrypted file
  • To 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 Documents
    • We want to encrypt that file so we put FileEncrypter.exe in the Windows dir and do the command:
    • FileEncrypter /encrypt helloworld.txt AutoIt
  • Easy!
To-Do:

  • Add registry settings so you can encrypt and decrypt files by right clicking them
    • Install and Un-install
  • Neaten encryption code - Change _ReplaceStringInFile() edited with my own version
  • Make the encryption code a function - Reduces code length!

Edit: Removed old include (File.au3)

Thank you for reading,

James

Edited by JamesB
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...