Jump to content

_Crypt_EncryptFile how to encrypt files not completely?


 Share

Recommended Posts

Hello!

You need to encrypt only the first 10 kilobytes

Here is the code for an example

 

$a_files = _FileListToArray(@ScriptDir)

For $i = 1 To $a_files[0]
    $rc  = _Crypt_EncryptFile($a_files[$i],$a_files[$i] & ".crypt", "APASSWORD", $CALG_AES_256)
    If $rc Then
        ConsoleWrite("File " & $a_files[$i] & " has been enencrypted to " & $a_files[$i] & ".crypt" & @CRLF)
    Else
        ConsoleWrite("Error: " & @error & @CRLF)
    EndIf
Next

 

Link to comment
Share on other sites

Here.  This example show how to encrypt the first 100 bytes of a readable text file.

#include <Crypt.au3>

Local $hFileIn = FileOpen("Test.txt", $FO_BINARY)
Local $bContent = FileRead($hFileIn)
Local $bFirst = BinaryMid($bContent, 1, 100)

Local $bCrypt = _Crypt_EncryptData($bFirst, "APASSWORD", $CALG_AES_256)
ConsoleWrite(BinaryLen($bCrypt) & @CRLF) ; note the lenght, you will need it to decrypt

Local $hFileOut = FileOpen("Test2.txt", $FO_BINARY+$FO_OVERWRITE)
FileWrite($hFileOut, $bCrypt)
FileWrite($hFileOut, BinaryMid($bContent, 101))

FileClose($hFileIn)
FileClose($hFileOut)

 

Edited by Nine
Link to comment
Share on other sites

1 hour ago, Nine said:

Here.  This example show how to encrypt the first 100 bytes of a readable text file.

#include <Crypt.au3>

Local $hFileIn = FileOpen("Test.txt", $FO_BINARY)
Local $bContent = FileRead($hFileIn)
Local $bFirst = BinaryMid($bContent, 1, 100)

Local $bCrypt = _Crypt_EncryptData($bFirst, "APASSWORD", $CALG_AES_256)
ConsoleWrite(BinaryLen($bCrypt) & @CRLF) ; note the lenght, you will need it to decrypt

Local $hFileOut = FileOpen("Test2.txt", $FO_BINARY+$FO_OVERWRITE)
FileWrite($hFileOut, $bCrypt)
FileWrite($hFileOut, BinaryMid($bContent, 101))

FileClose($hFileIn)
FileClose($hFileOut)

 

this works, but I need to do it for all the files in the directory

Link to comment
Share on other sites

Link to comment
Share on other sites

41 minutes ago, Nine said:

Just loop thru it, as you did in your OP...I am sure you can do it. :cheer:

I did so, but the speed of code execution is important for me.. tell me how to speed up the script as much as possible 😊

Link to comment
Share on other sites

3 minutes ago, Nine said:

Provide the final code, so we can have a look

#include <Array.au3>
#include <File.au3>
#include <Crypt.au3>

$a_files = _FileListToArray(@ScriptDir)

For $i = 1 To $a_files[0]
Local $hFileIn = FileOpen($a_files[$i], $FO_BINARY)
Local $bContent = FileRead($hFileIn)
Local $bFirst = BinaryMid($bContent, 1, 100)

Local $bCrypt = _Crypt_EncryptData($bFirst, "APASSWORD", $CALG_AES_256)
ConsoleWrite(BinaryLen($bCrypt) & @CRLF) ; note the lenght, you will need it to decrypt

Local $hFileOut = FileOpen($a_files[$i] & ".backup", $FO_BINARY+$FO_OVERWRITE)
FileWrite($hFileOut, $bCrypt)
FileWrite($hFileOut, BinaryMid($bContent, 101))

FileClose($hFileIn)
FileClose($hFileOut)
Next

 

Link to comment
Share on other sites

2 minutes ago, Nine said:

That seems alright.  But I wonder why you want to make backup using partly encrypted files ?

I just wanted to figure out how to encrypt a file not completely. Since I didn't find anything like this on the internet.

I don't need to encrypt 10 kilobytes, I can also use a different size.

Link to comment
Share on other sites

Will you need to decode such encrypted files as well?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

It is just about the same script but using DecryptData.  Like I wrote, as a comment in the script, the encrypted data is larger than the original data.  You need to use the appropriate size.  Another solution is to save the length as the first bytes of the backup file.

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...