thqby / ahk2_lib

MIT License
233 stars 32 forks source link

[Crypt.ahk] Is this lib unfinished? #56

Closed skygate2012 closed 4 months ago

skygate2012 commented 4 months ago

I tried it and seems to output garbled texts.

thqby commented 4 months ago

Test code not removed. AES encryption generates unreadable binary.

skygate2012 commented 3 months ago

@thqby Can you please write an encrypt/decrypt example that could produce output that is usable elsewhere? I tried encoding the output buffer to base64 using your other function but it's invalid when I paste it to e.g. this tool.

thqby commented 3 months ago

Depending on the key, the script hashs the plaintext password and derives the key for encryption, whereas the website's encryption seems to use the entered password as the key. Right?

skygate2012 commented 3 months ago

Oh yeah, I thought the password and key are the same thing so I entered a 32 character long password.. So how does one get the derived key in order to be decrypted by other tools? I read CryptGetKeyParam and it only seems to return the key length?

skygate2012 commented 3 months ago

Btw I tried this tool which seems to accept password as the input https://aesencryption.net But still, the results are different than AHK's and they cannot encrypt/decrypt each other's output.

cipher := "test", password := "123"
lp := StrPut(cipher, "UTF-8")
buf := Buffer(lp*4, 0)
StrPut(cipher, buf, "UTF-8")
len := Crypt_AES(buf, lp, password)
buf.size := len
msgbox Base64.Encode(buf)

produces toivMITldHpmr8e8bvWREw== while that tool produces e2zd43Radgt8TWNzq326jQ==

thqby commented 3 months ago

https://anycript.com/crypto W6NhzZjGmtj1pKcNHdYVGw==

SID := 128, key := '1234567890123456', text := 'abcdefghi'

StrPut(text, buf := Buffer((size := StrPut(text, 'utf-8') - 1) + 16), 'utf-8')
CALG_AES_256 := 1 + CALG_AES_192 := 1 + CALG_AES_128 := 0x660E
DllCall("advapi32\CryptAcquireContextA", "Ptr*", &hProv := 0, "Uint", 0, "Uint", 0, "Uint", 24, "Uint", 0xF0000000)
data := Buffer(12 + (SID >> 3), 0)
StrPut(key, NumPut('uchar', 8, 'uchar', 2, 'ushort', 0, 'uint', CALG_AES_%SID%, 'uint', SID >> 3, data), SID >> 3, 'utf-8')
if !DllCall('advapi32\CryptImportKey', 'ptr', hProv, 'ptr', data, 'uint', data.Size, 'ptr', 0, 'uint', 1, 'ptr*', &hKey := 0)
    throw OSError()
r := DllCall("advapi32\CryptEncrypt", "Ptr", hKey, "Uint", 0, "Uint", True, "Uint", 0, "Ptr", buf, "Uint*", &size, "Uint", buf.Size)
DllCall("advapi32\CryptDestroyKey", "Ptr", hKey)
DllCall("advapi32\CryptReleaseContext", "Ptr", hProv, "Uint", 0)
buf.Size := size

MsgBox(Base64.Encode(buf))  ; W6NhzZjGmtj1pKcNHdYVGw==
skygate2012 commented 3 months ago

Thanks, that worked perfectly!