AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

AES Encryption Function ~ the syntaxes

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Chavez



Joined: 20 Aug 2008
Posts: 256

PostPosted: Thu Nov 06, 2008 2:55 pm    Post subject: AES Encryption Function ~ the syntaxes Reply with quote

http://www.autohotkey.com/forum/viewtopic.php?t=23719

^ I refer to this post up here. I understand the use of functions, but this doesn't make sense to me. Can anyone clear this up for me?
_________________
-Chavez.
Back to top
View user's profile Send private message MSN Messenger
BoBo²
Guest





PostPosted: Thu Nov 06, 2008 8:40 pm    Post subject: Reply with quote

Sorry I'm a complete idiot when it comes to complex things like this, nevertheless I'd recommend to get in touch (PM) Sean and/or Laszlo (or whomever has outed him-/herself as an de-/encryption geek)
Back to top
Chavez



Joined: 20 Aug 2008
Posts: 256

PostPosted: Fri Nov 07, 2008 11:22 am    Post subject: Reply with quote

BoBo² wrote:
Sorry I'm a complete idiot when it comes to complex things like this, nevertheless I'd recommend to get in touch (PM) Sean and/or Laszlo (or whomever has outed him-/herself as an de-/encryption geek)


Sean hasn't answered yet. I'll try Laszlo.
_________________
-Chavez.
Back to top
View user's profile Send private message MSN Messenger
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Fri Nov 07, 2008 5:03 pm    Post subject: Reply with quote

You can use Sean's Crypt_AES function on data in memory this way (XP or Vista):
Code:
Password = AutoHotkey
Data = 12345678901234567
Len := StrLen(Data)
Buffer = %Data%0123456789abcdef ; allocate 16 byte more space

size := Crypt_AES(&Buffer, Len, Password, 256, 1)
MsgBox % "Plaintext size = " Len "`nCiphertext size = " size "`nCiphertext: " SubStr(Buffer,1,size)

size := Crypt_AES(&Buffer, size, Password, 256, 0)
MsgBox % "`Plaintext size = " size "`nPlaintext: " SubStr(Buffer,1,size)

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
}
Back to top
View user's profile Send private message
Kai



Joined: 08 Aug 2009
Posts: 17

PostPosted: Fri Aug 21, 2009 10:17 am    Post subject: Reply with quote

How to decrypt encrypted data only?

i.e. If I encrypt a string, 123456, and the encrypted string is 2aosi231, how to decrypt encrypted string later?

I wonder if that script encrypt and decrypt a string sequentially, but I think that is a little useless. I can't believe this, so I'm asking for help.

I want to encrypt a password on an ini file, and later retrieve it.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Fri Aug 21, 2009 3:59 pm    Post subject: Reply with quote

Set the last parameter of Crypt_AES to 0. Be careful, the encrypted data can contain \0, so string functions might not work with it.
Back to top
View user's profile Send private message
Kai



Joined: 08 Aug 2009
Posts: 17

PostPosted: Fri Aug 21, 2009 7:52 pm    Post subject: Reply with quote

Laszlo wrote:
Set the last parameter of Crypt_AES to 0.


thanks lazlo but I knew that...

i.e.:

Code:
Password = AutoHotkey
Data = 2aosi231 ; encrypted data before with the same password
Len := StrLen(Data)
Buffer = %Data%0123456789abcdef ; allocate 16 byte more space

size := Crypt_AES(&Buffer, size, Password, 256, 0)
MsgBox % "`Plaintext size = " size "`nPlaintext: " SubStr(Buffer,1,size)


Size parameter isn't specified... I tried StrLen of data or buffer as size parameter, but it doesn't work, blank string.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Fri Aug 21, 2009 8:47 pm    Post subject: Reply with quote

You got the size parameter when the original data was encrypted. You have to use that.
Back to top
View user's profile Send private message
Kai



Joined: 08 Aug 2009
Posts: 17

PostPosted: Fri Aug 21, 2009 8:51 pm    Post subject: Reply with quote

Laszlo wrote:
You got the size parameter when the original data was encrypted. You have to use that.


This means that I have to store the original size too...?
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Fri Aug 21, 2009 9:33 pm    Post subject: Reply with quote

You have to save the size of the encrypted data, not the size of the data, and use it at decryption.
Back to top
View user's profile Send private message
Kai



Joined: 08 Aug 2009
Posts: 17

PostPosted: Sat Aug 22, 2009 2:00 pm    Post subject: Reply with quote

Ehr... the second parameter variable "size" on this

Code:
Crypt_AES(&Buffer, size, Password, 256, 0)


is the encrypted string, isn't it?

If so, what to set in "Buffer"?
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Sat Aug 22, 2009 2:15 pm    Post subject: Reply with quote

Buffer contains the encrypted binary data, the actual size used in bytes is contained in the variable "size". It is not always the same as the length of the input (the length of cleartext), or the allocated memory for Buffer.
Back to top
View user's profile Send private message
Kai



Joined: 08 Aug 2009
Posts: 17

PostPosted: Sat Aug 22, 2009 2:26 pm    Post subject: Reply with quote

Laszlo wrote:
Buffer contains the encrypted binary data, the actual size used in bytes is contained in the variable "size". It is not always the same as the length of the input (the length of cleartext), or the allocated memory for Buffer.


Okay now I understood. Thank you Laszlo Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group