FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileCryptFile() : Encrypt/Decrypt files. CNG AES 256 bit CBC

Post by SKAN » 07 Apr 2021, 10:45

ozzii wrote:
07 Apr 2021, 07:19
I will wait for the new updated script and try to understand it ;)
Yes. I'll start now.
I have been been working on the important question you had raised.
ozzii wrote:
06 Apr 2021, 03:36
I can remember the secret key SKAN (for example) but not 48 numbers with 3 digits....
I have a solution now, but I'll post that later.
I will update this function soon.
Thanks. :)

ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: FileCryptFile() : Encrypt/Decrypt files. CNG AES 256 bit CBC

Post by ozzii » 08 Apr 2021, 02:42

Thanks.
And like I always say, a BIG thanks for all your work with AH.

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileCryptFile() : Encrypt/Decrypt files. CNG AES 256 bit CBC

Post by SKAN » 08 Apr 2021, 13:44

ozzii wrote:
07 Apr 2021, 07:19
Thank you. I will wait for the new updated script and try to understand it ;)
Updated :thumbup:

ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by ozzii » 09 Apr 2021, 03:05

Thanks.
I have done some tests and no error message, just the done msgbox.
I have change this (to a non existing file):

Code: Select all

Original  :=  A_AhkPath . "\..\licsssense.txt"
Encrypted :=  "license.txt.aeaaaaa"

KeyPhrase := "unbent-snugly-mousy-swivel-collide"         ; Phrase generated @ https://www.useapassphrase.com/

FileCryptFile("encrypt", Original,  Encrypted, KeyPhrase)
FileCryptFile("decrypt", Encrypted, Decrypted, KeyPhrase)
But in your code, I see some error test :headwall:

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by SKAN » 09 Apr 2021, 03:17

ozzii wrote:
09 Apr 2021, 03:05
Thanks.
I have done some tests and no error message, just the done msgbox.
I can't put a MsgBox inside code. It has to be outside the function.
Always check ErrorLevel to know if function was successful.

Code: Select all

SetWorkingDir, %A_ScriptDir%

Original  :=  A_AhkPath . "\..\licsssense.txt"
Encrypted :=  "license.txt.aeaaaaa"

KeyPhrase := "unbent-snugly-mousy-swivel-collide"         ; Phrase generated @ https://www.useapassphrase.com/

FileCryptFile("encrypt", Original,  Encrypted, KeyPhrase)
If (ErrorLevel)
MsgBox % ErrorLevel

ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by ozzii » 09 Apr 2021, 06:18

Thanks for the explanation.
I thought to will saw the msgbox because I saw the error messages into the code.
But now that I look closer, no msgbox inside the function :oops:

I have put this at the end of your script

Code: Select all

If (ErrorLevel)
	MsgBox % ErrorLevel
else
	MsgBox Done

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by SKAN » 09 Apr 2021, 06:27

Yes @ozzii

Error checking can also be done as follows:

Code: Select all

If not FileCryptFile("encrypt", Original,  Encrypted, KeyPhrase)
MsgBox % ErrorLevel

ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by ozzii » 10 Apr 2021, 04:22

Thank you SKAN for this second method (one line shorter).

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by SKAN » 17 Apr 2021, 08:48

Code updated: FileCryptFile() v0.66.

Small update. Added a version check.

carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by carno » 17 Apr 2021, 15:32

Thanks! This is another similar program I use all the time:

Code: Select all

; http://autohotkey.com/board/topic/90702-encrypt-decrypt-text/
; https://www.autohotkey.com/boards/viewtopic.php?t=5838

; ===================================================================================
; AHK Version ...: AHK_L 1.1.11.01 x64 Unicode
; Win Version ...: Windows 7 Professional x64 SP1
; Author ........: Originated from jNizM, tweaked by Soft
; Script ........: EnCrypt_DeCrypt.ahk
; Description ...: Encrypt & Decrypt Data
; License .......: WTFPL
; ===================================================================================

; GLOBAL SETTINGS ===================================================================

#NoEnv
#SingleInstance force

#Include Crypt.ahk
#Include CryptConst.ahk
#Include CryptFoos.ahk

; SCRIPT ============================================================================

Gui, Margin, 10, 10
Gui, Font, s10, Segoe UI

Gui, Add, Edit, xm ym w300 h120 vStr hwndEditEnDecrypt, Enter text or drag file to en/decrypt here.

Gui, Add, Edit, xm y+5 w300 vStr2, Password
;Gui, Add, Edit, xm y+5 w300 vStr2 hwndEditPw,
;EM_SetCueBanner(EditPw, "Password")
Gui, Add, Edit, xm y+10 w300 h120 vEnDeCrypt ReadOnly, En/Decrypted text appears here and is also copied to the clipboard!

Gui, Add, DropDownList, xm y+5 w300 AltSubmit vEncryption, RC4 (Rivest Cipher)
    |RC2 (Rivest Cipher)
    |3DES (Data Encryption Standard)
    |3DES 112 (Data Encryption Standard)
    |AES 128 (Advanced Encryption Standard)
    |AES 192 (Advanced Encryption Standard)
    |AES 256 (Advanced Encryption Standard)||
Gui, Add, Button, xm-1 y+10 w300, Copy
Gui, Add, Button, xm-1 y+15 w100, Encrypt
Gui, Add, Button, xm+201 yp w100, Decrypt
Gui, +LastFound
Gui, Show,, En/Decrypt
WinSet, Redraw
Return

ButtonCopy:
    Gui, Submit, NoHide
    GuiControl,, Copy, Copy
    ControlGet, OutputVar, Selected,,,
    ;MsgBox % OutputVar
    Clipboard := OutputVar
    StringReplace, Clipboard, Clipboard, %A_Space%, , All
    ;MsgBox % Clipboard
    MsgBox, 96, Your Selection, %Clipboard%, 1 ; No sound!
    Send, !{Tab}	
Return

ButtonEncrypt:
    Gui, Submit, NoHide
    GuiControl,, EnDeCrypt, % Crypt.Encrypt.StrEncrypt(Str, Str2, Encryption, 1)
    Clipboard := % Crypt.Encrypt.StrEncrypt(Str, Str2, Encryption, 1)
Return

ButtonDecrypt:
    Gui, Submit, NoHide
    GuiControl,, EnDeCrypt, % Crypt.Encrypt.StrDecrypt(Str, Str2, Encryption, 1)
    Clipboard := % Crypt.Encrypt.StrDecrypt(Str, Str2, Encryption, 1)
Return

GuiDropFiles:
if A_GuiControl = Str
{
    Draged := FileOpen(A_GuiEvent, "r")
    GuiControl,, Str, % Draged.Read()
 
}
Return

; EXIT ==============================================================================

GuiClose:
GuiEscape:
ExitApp

EM_SetCueBanner(hWnd, Cue)
{
    Static EM_SETCUEBANNER := 0x1501
    Return DllCall("User32.dll\SendMessage", "Ptr", hWnd, "UInt", EM_SETCUEBANNER, "Ptr", True, "WStr", Cue)
}

User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by Drugwash » 23 Apr 2021, 07:05

Hi Suresh, long time no see. :)

Thank you so much for this function, I was just looking for one for my old diary script. Originally I had implemented a rudimentary bit-shifting in machine code but that one fails in a x64 environment. Worked fine in Win9x and XP though, as they were 32bit.

Just tested your function in Linux Mint under Wine 5.14 and it works just fine. Assuming I won't starve in the mean time (no joke here) I'll try to update MyDiary for x64 compatibility and use your function with it if it's okay with you. You can find the current 32bit version of it in the package linked to in my signature if you're curious.

On another note and completely unrelated, when you get some spare time could you pretty please take a look at the issue in this topic and confirm or deny my finding? There's been no feedback whatsoever for too long and I just don't know if it's me or something else. Worse thing is Wine also yields the same ugly result since forever and as such all my scripts that use a similar technique (including MyDiary) look bad.

Thanks again and all the best to you! :wave:
Part of my AHK work can be found here.

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by SKAN » 23 Apr 2021, 10:31

Drugwash wrote:
23 Apr 2021, 07:05
Hi Suresh, long time no see. :)
Hello 😎

Just tested your function in Linux Mint under Wine 5.14 and it works just fine. Assuming I won't starve in the mean time (no joke here) I'll try to update MyDiary for x64 compatibility and use your function with it if it's okay with you.
Sure. :thumbup:
I don't' use this. This is very efficient and useful only for large files (several GB's).
But one downer with encryption is, you cannot compress a file after encryption.. The data is so random you won't gain much.
The best way to encrypt a large file is to use 7z which will compress the original data before applying AES encryption.

I have written a wrapper for handling small data (16 MB) directly in memory variable.
It allows me to compress, encrypt and RePNG a variable's content.
It is still in testing phase.. Will try to post it soon.
On another note and completely unrelated, when you get some spare time could you pretty please take a look at the issue in this topic and confirm or deny my finding? There's been no feedback whatsoever for too long and I just don't know if it's me or something else. Worse thing is Wine also yields the same ugly result since forever and as such all my scripts that use a similar technique (including MyDiary) look bad.
No, its not you. :)
I will reply in that topic.
Thanks again and all the best to you! :wave:
Thank you and wish you the same. :thumbup:

User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by Drugwash » 23 Apr 2021, 14:14

Thank you for your approval, now gotta actually start working on the code. As soon as I finish with that Cinnamon applet that drives me bananas.

The encryption in MyDiary was more like a fancy feature, at the time just wanted to see if I can do it. First attempted a pure AHK implementation and it was taking ages for a few hundred kB file, so went back to my first love - machine code - and it was a blast.
Now maybe with so much privacy interference from our "masters" a real encryption may be warranted, although usually a private diary wouldn't hold any critical information. Or would it...? Dunno.

Anyway, the so-called database - a simple rich-text file - would only be a few MB at most. My 2006-2012 DB is only 1.5MB so the operations should be super-fast with no huge memory requirements. At some point I wanted to add the ability to insert images into the rich text but couldn't find an easy way - or any way, for that matter. I was still using 98SE back then and needed something compatible with AHK 1.0 (ANSI). If that feature could be added then the DB may increase dramatically in size. But for old times' sake I'd like to keep it 9x-compatible, if only for the very few die-hard fans that still stick to that OS series.

The idea of 7-zip compression is actually a good one, if only for the fact that 7-zip supports password encryption which would make usage of this function superfluous. But it would require a third-party library, complicating things a bit (not that they aren't already complicated since I ship the Hunspell library and a few dictionaries with it). Will see. First off I should fix the RichEdit wrapper and all other libraries for x64 usage which will take some time.

Well, that's about it. Sorry for the off-topic. And thank you for checking out that CopyImage() issue. Good luck with all your projects!
Part of my AHK work can be found here.

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by SKAN » 23 Apr 2021, 14:57

Drugwash wrote:
23 Apr 2021, 14:14
Now maybe with so much privacy interference from our "masters" a real encryption may be warranted, although usually a private diary wouldn't hold any critical information. Or would it...? Dunno.
I was stupid enough to store a debit card info in a text file on my desktop. I was never able to "erase" it. Data retrieval software always found it.
In NTFS, If file size is around 1000 bytes, the contents gets stored in file allocation table along with the file name.
I don't know If I would need a diary, but I want to save such small data encrypted and inside a PNG so that I can back it up online.
The idea of 7-zip compression is actually a good one, if only for the fact that 7-zip supports password encryption which would make usage of this function superfluous.
If your data is less than 16 MB, then my upcoming lib for compression and encryption would be a better choice.
I will put a link in this topic when I post my lib.

:)

User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by Drugwash » 29 Apr 2021, 16:07

NTFS has those ADSes (Alternate Data Streams) that may hold data even after it's been erased. Some good forensic software should be aware of that and clean those up properly.

Just recently I saw in some application - might've been Double Commander, can't remember - an option to perform a certain number of passes when deleting a file/folder. Maybe you should consider switching to Linux (dunno if DC has a Windows version and if such feature would be available there too, in case it actually was it the application in cause). :D

Your idea of hiding data in a picture would be good if it weren't old already. It's called steganography and has been used for a long time to pass secret messages. Chances are the online AI is trained to detect such hidden data and retrieve it, so my friendly opinion would be for you to never ever post such kind of files online, or your privacy would be much much quicker invaded than leaving the data in the clear in a text file on the desktop. ;)

Besides, all encryption libraries have backdoors for the three-letter-agencies to butt in and decrypt, so such effort would only be justified against the random nosy parents or siblings or friends. :) Which is why I had built that diary anyway. BTW, the current code should only compile/run safely under AHK Basic.
Part of my AHK work can be found here.

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by SKAN » 29 Apr 2021, 22:20

Drugwash wrote:
29 Apr 2021, 16:07
NTFS has those ADSes (Alternate Data Streams) that may hold data even after it's been erased. Some good forensic software should be aware of that and clean those up properly.

Just recently I saw in some application - might've been Double Commander, can't remember - an option to perform a certain number of passes when deleting a file/folder. Maybe you should consider switching to Linux (dunno if DC has a Windows version and if such feature would be available there too, in case it actually was it the application in cause). :D
I have a function to erase files along with any associated ADS. It fills the file with random data, truncate to 0 byte then delete it.
It doesn't help if the files are small and are located on common folders like Desktop. A copy is created for every save. :(
Your idea of hiding data in a picture would be good if it weren't old already. It's called steganography and has been used for a long time to pass secret messages. Chances are the online AI is trained to detect such hidden data and retrieve it, so my friendly opinion would be for you to never ever post such kind of files online, or your privacy would be much much quicker invaded than leaving the data in the clear in a text file on the desktop. ;)
I know what it is called, but would avoid the "word". Google shouldn't know what I know. :)
Yes, AI can detect it, but I highly doubt it can retrieve any meaningful data.
For my kind of data, a password protected 7-zip is secure enough.
The hassle is that I have to note down the filename, password and a description of contents... somewhere.
Saving notes is the problem with 7zip.
Whereas, a picture is worth 1000 words. I can "tell" by just looking at it.
Besides, all encryption libraries have backdoors for the three-letter-agencies to butt in and decrypt
Law can do B&E doesn't mean there is no safety in keeping doors locked.
That said, what if the three-letter-agencies are spreading the rumor about a back-door to discourage non-essential people using it?.
A beautiful mind may exist outside their realm and could access the backdoor. Since AES-256 is officially used by them, would they take a chance?
so such effort would only be justified against the random nosy parents or siblings or friends. :)
I disagree. There is a large middle-ground between the extremes. :)

User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by Drugwash » 10 May 2021, 17:07

And the truth is also somewhere at the middle so... good luck with the project(s) and hopefully all the efforts would be worth it. :)

(sorry for late replies, not getting e-mail notifications and tend to forget a lot of things including checking the forums)
Part of my AHK work can be found here.

User avatar
joedf
Posts: 8951
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by joedf » 10 May 2021, 19:54

Nice! I love that it has performance in mind. :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by SKAN » 11 May 2021, 00:42

Drugwash wrote: (sorry for late replies, not getting e-mail notifications and tend to forget a lot of things including checking the forums)
Late is OK. I highly value your input. :)

@joedf :thumbup: :)

User avatar
Drugwash
Posts: 850
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: FileCryptFile() : Encrypt/Decrypt files. CNG-AES-256-CBC

Post by Drugwash » 27 May 2021, 09:40

SKAN wrote:
11 May 2021, 00:42
I highly value your input. :)
Same here, AHK master. (and I mean it)
Image
Part of my AHK work can be found here.

Post Reply

Return to “Scripts and Functions (v1)”