[AHK_H v2] CryptAES usage? Topic is solved

Ask for help, how to use AHK_H, etc.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

[AHK_H v2] CryptAES usage?

16 Jan 2019, 07:12

hi can u tell me what the proper usage of this function is

Code: Select all

#Include CryptAES.ahk

; prep
VarSetCapacity(str, 4)
len := StrPut("abcd", &str)
MsgBox StrGet(&str) ; 'abcd'

; encrypt
size := CryptAES(str, len, "password", true)
MsgBox StrGet(&str) ; chinese chars

; decrypt
CryptAES(str, size, "password", false)  ; aaand crash..
MsgBox StrGet(&str)
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: [AHK_H v2] CryptAES usage?  Topic is solved

16 Jan 2019, 18:10

There was a small bug that has been fixed now.
Also you have to consider that v2 is unicode, so string length in bytes is *2!

Code: Select all

; prep
VarSetCapacity(str, 8) ; Strlen*2 due to unicode
len := StrPut("abcd", &str)
MsgBox StrGet(&str) ; 'abcd'

; encrypt
size := CryptAES(str, len*2, "password", true) ; len*2 due to unicode
MsgBox StrGet(&str)

; decrypt
CryptAES(str, size, "password", false)
MsgBox StrGet(&str)

CryptAES(ByRef lp,sz,pw,e:=1,SID:=256){
	static AES_128:=0x660E,AES_192:=1+AES_128,AES_256:=1+AES_192,SHA1:=1+0x8003 ; MD5
	If !CryptAcquireContext(getvar(hP:=0),0,0,24,0xF0000000) ;PROV_RSA_AES, CRYPT_VERIFYCONTEXT
	|| !CryptCreateHash(hP,SHA1,0,0,getvar(H:=0) )
	|| !CryptHashData(H,&pw,StrLen(pw)*2,0)
	|| !CryptDeriveKey(hP,AES_%SID%,H,SID<<16,getvar(hK:=0))
	|| !CryptDestroyHash(H)
		return 0
	if e
		CryptEncrypt(hK,0,1,0,&lp,getvar(sz),sz+16)
	else	CryptDecrypt(hK,0,1,0,&lp,getvar(sz))
	return (CryptDestroyKey(hK),CryptReleaseContext(hP,0),sz)
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [AHK_H v2] CryptAES usage?

16 Jan 2019, 19:40

ah yes forgot the unicode thing, cool it works now, thanks
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [AHK_H v2] CryptAES usage?

18 Jan 2019, 09:19

has this been updated on github too
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [AHK_H v2] CryptAES usage?

18 Jan 2019, 11:41

awesome, thanks

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 20 guests