Quote:
ok now i done it.
How?
I want to implement AES encrypt/decrypt on my AHK/PHP scripts.
Can someone help me, please?
I'm trying to do this for 1 week...
Last tried:
AHK Script:
Code:
sTextOriginl := "Nel mezzo del cammin di nostra vita"
sPassword := "TestKey1"
SID := 256
nSize := Text_AES(sTextEncrypt, &sTextOriginl, StrLen(sTextOriginl), sPassword, SID, True) ; Encryption
sHex := Hex4Bin(&sTextEncrypt, nSize)
MsgBox % sHex
nSize := Bin4Hex(sTextEncrypt, sHex)
Text_AES(sTextDecrypt, &sTextEncrypt, nSize, sPassword, SID, False)
MsgBox % sTextDecrypt
Return
Text_AES(ByRef sResult, pData, nSize, sPassword, SID = 256, bEncrypt = True)
{
VarSetCapacity(sResult, bEncrypt ? nSize+16 : nSize)
DllCall("RtlMoveMemory", "Uint", &sResult, "Uint", pData, "Uint", nSize)
nSize := Crypt_AES(&sResult, nSize, sPassword, SID, bEncrypt)
NumPut(0, sResult, nSize, "char"), VarSetCapacity(sResult,-1)
Return nSize
}
Hex4Bin(p, l)
{
VarSetCapacity(h,l*2)
o:=A_FormatInteger
SetFormat, Integer, H
Loop, %l%
h.=SubStr(*p++,-1)
SetFormat, Integer, %o%
StringReplace, h, h, x, 0, All
Return h
}
Bin4Hex(ByRef b, h)
{
VarSetCapacity(b,l:=StrLen(h)//2)
Loop, %l%
NumPut("0x" . SubStr(h,2*A_Index-1,2),b,A_Index-1,"Uchar")
NumPut(0,b,l,"Uchar"), VarSetCapacity(b,-1)
Return l
}
Crypt_AES(pData, nSize, sPassword, SID = 256, bEncrypt = True) {
CALG_AES_256 := 1 + CALG_AES_192 := 1 + CALG_AES_128 := 0x660E
CALG_SHA1 := 1 + CALG_MD5 := 0x8003
DllCall("advapi32\CryptAcquireContextA", "UintP", hProv, "Uint", 0, "str"
, "Microsoft Enhanced RSA and AES Cryptographic Provider" . (A_OSVersion="WIN_XP" ? " (Prototype)" : ""), "Uint", 24, "Uint", 0)
DllCall("advapi32\CryptCreateHash", "Uint", hProv, "Uint", CALG_SHA1, "Uint", 0, "Uint", 0, "UintP", hHash)
DllCall("advapi32\CryptHashData", "Uint", hHash, "Uint", &sPassword, "Uint", StrLen(sPassword), "Uint", 0)
DllCall("advapi32\CryptDeriveKey", "Uint", hProv, "Uint", CALG_AES_%SID%, "Uint", hHash, "Uint", SID<<16, "UintP", hKey)
DllCall("advapi32\CryptDestroyHash", "Uint", hHash)
If bEncrypt
DllCall("advapi32\CryptEncrypt", "Uint", hKey, "Uint", 0, "Uint", True, "Uint", 0, "Uint", pData, "UintP", nSize, "Uint", nSize+16)
Else DllCall("advapi32\CryptDecrypt", "Uint", hKey, "Uint", 0, "Uint", True, "Uint", 0, "Uint", pData, "UintP", nSize)
DllCall("advapi32\CryptDestroyKey", "Uint", hKey)
DllCall("advapi32\CryptReleaseContext", "Uint", hProv, "Uint", 0)
Return nSize
}
PHP Code:
http://www.phpclasses.org/browse/package/4238.htmlI modified this on AESCipher.class.php encrypt function:
Code:
return $this->_cipher->hexToString($output);
to
Code:
return $output;
in order to return hex.
PHP File:
Code:
include_once('./AES.class.php');
include_once('./AESCipher.class.php');
$password = 'TestKey1';
$input = 'Nel mezzo del cammin di nostra vita';
$Cipher = new AESCipher(AES::AES256);
print('input: '.$input.'<br />');
print('key: '.$password.'<br />');
$start = microtime(true);
$cryptext = $Cipher->encrypt($input, $password);
$end = microtime(true);
print('AESCipher Class encryption time (256bit): '.(($end - $start)*1000).'ms <br />');
print 'cryptext: '.$cryptext.'<br />';
Results are:
AHK:
Quote:
0d75715f66c4c8a45e98e640979c300c54df0e4427477fcf0e8c991febeefa4e34b0b85a556b70e6668161c6f2c5b7a1
PHP:
Quote:
input: Nel mezzo del cammin di nostra vita
key: TestKey1
AESCipher Class encryption time (256bit): 5.9909820556641ms
cryptext: 65adf1b899f929ceffa3e6768ca7d42eb8693951ef314d171246e51452a251a34defe64e9a0b9546d85d4a89b76a979e
Someone give me a solution! Thanks!
PS: sorry for my bad english.