Hi,
below a working code to use Sec's AES libv2 :
Code: Select all
;- Based on Sec's AES lib v2
;- August 13 2004 15H15 PM GMT+1 by newbie
AESkey.s = MD5Fingerprint("toto", 4)
AES256Init(@AESkey)
Procedure Calculate_AES_Padding(*Buffer)
Debug "PeekS(*Buffer) = " + PeekS(*Buffer)
length_buff.l = GlobalSize_(*Buffer) ; buffer lenght
Debug "length_buff 1 = " + Str(length_buff)
bytes_remaining.l = length_buff % 16 ; length_buff modulo 16
Debug "bytes_remaining = " + Str(bytes_remaining)
padding.l = 16 - bytes_remaining
Debug "padding = " + Str(padding)
length_required.l = length_buff + padding
Debug "length_required (after padding) = " + Str(length_required)
ProcedureReturn length_required
EndProcedure
Procedure AESEncrypt(*Buffer)
length_buff.l = GlobalSize_(*Buffer)
a = 0
While length_buff <> 0
length_buff - 16
AES256Encrypt(*Buffer + a)
a + 16
Wend
ProcedureReturn *Buffer
EndProcedure
Procedure AESdecrypt(*Buffer)
length_buff.l = GlobalSize_(*Buffer)
a = 0
While length_buff <> 0
length_buff - 16
AES256Decrypt(*Buffer + a)
a + 16
Wend
ProcedureReturn *Buffer
EndProcedure
plain.s = "I am a fan of SEC now :-) lololoLOL" ; our plain text, 35 bytes
Debug "clear = " + plain
Debug "len(clear) = " + Str(Len(plain))
length_required.l = Calculate_AES_Padding(@plain); length required for the buffer
*cypher_text = AllocateMemory(length_required) ; creating the buffer to the right input/output size
PokeS(*cypher_text, plain) ; copying our plain text into the buffer
Debug "LEN *cypher_text = " + Str(Len(PeekS(*cypher_text)))
;-encryption
*cypher_text = AESEncrypt(*cypher_text) ; encrypting our plain text
Debug "encrypted = " + PeekS(*cypher_text, length_required)
Debug "LEN encrypted = " + Str(Len(PeekS(*cypher_text, length_required)))
;- decryption
size.l = GlobalSize_(*cypher_text)
Debug "size = " + Str(size)
*plain_text = AllocateMemory(size)
CopyMemory(AESdecrypt(*cypher_text), *plain_text , size)
Debug "clear = " + PeekS(*plain_text, size);, length_required)
Debug "LEN clear = " + Str(Len(PeekS(*plain_text)))
Notice that the encryption/decryption functions will only work if you allocate the buffer before at the right size or else the GlobalSize_() will fail.
Notice too that the debug lines which use "Len(Peeks(*buffer))" are just unreliable information, you should
not use it in the code involved to encrypt/decrypt or you will get in troubles. Indeed "Peeks()" can't get past the null bytes and will stop at the first null byte encounter, and will not read the entire encrypted text.
In special cases, when there is a null bytes in the middle of the encrypted string, and that you receive it via the network (e.g you didn't encrypt it, so you have no information about the size), and that GlobalSize_() fails and returns 0, then you might find usefull to use the following procedure :
Code: Select all
;- updated the August 13 (end of the day) to take in count the case where
the buffer submitted to the procedure is filled completly without any final null byte
Procedure GetMemSize(*Buffer, size.l)
cnt.b = 0
pos.l = 0
chr.w = 0
i.l = 0
While i < size
chr = (PeekB(*Buffer+i) & $FF)
If chr = 0 ; if 0 byte
If cnt = 0 ; if pos not registered
pos = i; register the position of the null byte
cnt = 1
EndIf
Else ; if not a 0 byte
If cnt = 1 ; if pos registered
cnt = 0; reset (last byte wasn't the last null byte)
EndIf
EndIf
i + 1
Wend
If cnt = 0 ; no final null byte found
pos = size ; the entire buffer is filled
EndIf
ProcedureReturn pos
EndProcedure
Give the buffer and the size of the buffer (size of the packet received)
and the procedure will return the size of the encrypted string within the packet. Let's say our packet size is 1024 bytes, and that only 80 bytes are filled, this function is the only way I found to calculate the size inside.
It might even be usefull to replace all of the GlobaSize_() above by this GetMemSize(), but I didn't try.
This code is may be perfectible, for now it has only be designed for text.
For files the GetMemSize() should work too.
I have spent hours just for this small piece of code, hope you'll enjoy, and thanks to our guru for the AES libs
