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)
}