I have this script:
Code: Select all
; prep
VarSetCapacity(str, 128) ; Strlen*2 due to unicode
len := StrPut("testingstringthatcontains64characters123456885464562132132222452", &str)
fileappend % StrGet(&str) "`n", result.txt, UTF-16
; encrypt
loop, 100
{
size := CryptAES(str, len*2, "asd", true) ; len*2 due to unicode
fileappend % StrGet(&str) "`n", result.txt, UTF-16
fileappend % size "`n", result.txt, UTF-16
}
; decrypt
CryptAES(str, size, "asd", false)
fileappend % StrGet(&str) "`n", result.txt, UTF-16
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 !DllCall("Advapi32\CryptAcquireContext","Ptr*",hP,"Uint",0,"Uint",0,"Uint",24,"UInt",0xF0000000) ;PROV_RSA_AES, CRYPT_VERIFYCONTEXT
|| !DllCall("Advapi32\CryptCreateHash","Ptr",hP,"Uint",SHA1,"Uint",0,"Uint",0,"Ptr*",H )
|| !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))
CryptDestroyKey(hK),CryptReleaseContext(hP,0)
return sz
}
1. It never reaches the 100 repetitions, the script closes itself after a random amount.
2. All results are different and when I try to decrypt them (reading it from another script) the output is not even close to the input.
3. Sometimes it outputs weird things like for example:
㜀ꌑ襥ᛉ﹉垍챭嚟芐提잵䶝ꪋ㡖鮒נ憏蕤흧⥑ᦝ薒�霢嶌气�娝ͳ�퍧:덐㪹뢦䤘˽ૡ˨⽭鼏물颷墿ᙥ뱲⡄㠯甘娹�ਇଂ⺅裢旌흆鐐鷻郴㪶幡\??\G:\Desktop\test10.ahk
That's the path to my script.
For example:
Output from first script (without the loop, 100):
Code: Select all
testingstringthatcontains64characters123456885464562132132222452
㜀ꌑ襥ᛉ﹉垍챭嚟芐提잵䶝ꪋ㡖鮒נ憏蕤흧⥑ᦝ薒�霢嶌气�娝ͳ�퍧:덐㪹뢦䤘˽ૡ˨⽭鼏물颷墿ᙥ뱲⡄㠯甘娹�ਇଂ⺅裢旌흆鐐鷻郴㪶幡
144
testingstringthatcontains64characters123456885464562132132222452
Code: Select all
VarSetCapacity(str, 128) ; Strlen*2 due to unicode
StrPut("㜀ꌑ襥ᛉ﹉垍챭嚟芐提잵䶝ꪋ㡖鮒נ憏蕤흧⥑ᦝ薒�霢嶌气�娝ͳ�퍧:덐㪹뢦䤘˽ૡ˨⽭鼏물颷墿ᙥ뱲⡄㠯甘娹�ਇଂ⺅裢旌흆鐐鷻郴㪶幡", &str)
size = 144
; decrypt
CryptAES(str, size, 5 10 "asd", false)
fileappend % StrGet(&str) "`n", result.txt, UTF-16
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 !DllCall("Advapi32\CryptAcquireContext","Ptr*",hP,"Uint",0,"Uint",0,"Uint",24,"UInt",0xF0000000) ;PROV_RSA_AES, CRYPT_VERIFYCONTEXT
|| !DllCall("Advapi32\CryptCreateHash","Ptr",hP,"Uint",SHA1,"Uint",0,"Uint",0,"Ptr*",H )
|| !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))
CryptDestroyKey(hK),CryptReleaseContext(hP,0)
return sz
}
Is this intended? If it is, how can I achieve the following?:
1. From Script1 using CryptAES() to encrypt an string and saving it to a txt.
2. From Script2 read the encrypted string saved on a txt and decrypt it using CryptAES()
Thanks in advance.