newbie wrote:ok Sec, I have managed to code a complete example to use your lib, which works without any errors :
Code: Select all
Procedure AESEncrypt(*Buffer, *Key)
For a = 0 To Len(PeekS(*Buffer)) Step 16
AES256Encrypt(*Buffer + a, *Key)
Next a
ProcedureReturn *Buffer
EndProcedure
Procedure AESDecrypt(*Buffer, *Key)
For a = 0 To Len(PeekS(*Buffer)) Step 16
AES256Decrypt(*Buffer + a, *Key)
Next a
ProcedureReturn *Buffer
EndProcedure
plain.s = "I am a fan of SEC now :-) lololoLOL" ; our plain text
key.s = MD5Fingerprint("toto", 4) ; creating an AES key of 256 bits
Debug "clear = " + plain
Debug "len(clear) = " + Str(Len(plain))
Debug "key = " + key
length_required.l = Len(plain) ; length required for the buffer
Debug "length_required = " + Str(length_required)
*cypher_text = AllocateMemory(length_required) ; creating the buffer to the right input/output size
PokeS(*cypher_text, plain, length_required) ; copying our plain text into the buffer
Debug "LEN *cypher_text = " + Str(Len(PeekS(*cypher_text)))
;-encryption
*cypher_text = AESEncrypt(*cypher_text, @key) ; encrypting our plain text
Debug "encrypted = " + PeekS(*cypher_text, length_required)
Debug "LEN encrypted = " + Str(Len(PeekS(*cypher_text, length_required)))
;- decryption
;let's assume that this is a break in the code, like for instance an encrypted file that we did not encrypt
;or any encrypted data for which we do not know the size of the plain text
;*cypher_text would be @plain_text$ for instance
*plain_text = AESDecrypt(*cypher_text, @key) ; decrypting the cypher text
Debug "clear = " + PeekS(*plain_text);, length_required)
Debug "LEN clear = " + Str(Len(PeekS(*plain_text)))
These two functions can be easily combined with your libs, it's more convenient. If you have any correction to do, feel free to do it of course, I whish to learn :)
Even if this code works perfectly, I do not understand something.
From the FIPS PDF document about AES, the output cypher text size depends on the size of the plain text size, as follow :
0 <= Len(plain_text) <= 16 -> output size of 128 bits
( '<=' means less or equal)
So obviously, a 32 bytes string will have a 256 bits output.
But so, shouldn't a 33 bytes string have a 256 + 128 = 348 bits output ??
In the code, the plain text example is 35 bytes (280 bits), So if you take
the first 32 bytes, you need 256 bits of buffer + another 128 bits buffer for the remaining 3 bytes.
However in the code above, I only give 35 bytes of buffer (instead of 48 which crash ?!) and it works just fine.
I need to know if the code above is right and reliable (you know you can do a perfect lib, if it's not used right, especially for encryption, it will give BS and won't have the security level expected), and to understand what is
the problem with my understanding of the buffer encoding size.
Anyway, that's a _very_ great job from Sec, and I hope to know soon if Shannara has found what she was looking at for some time now :)
NO - the code above is not reliable (or the LIB is flawed) !
Change the code as below . . . it does not work !
Procedure AESEncrypt(*Buffer, *Key)
For a = 0 To Len(PeekS(*Buffer)) Step 16
AES256Encrypt(*Buffer + a, *Key)
Next a
ProcedureReturn *Buffer
EndProcedure
Procedure AESDecrypt(*Buffer, *Key)
For a = 0 To Len(PeekS(*Buffer)) Step 16
AES256Decrypt(*Buffer + a, *Key)
Next a
ProcedureReturn *Buffer
EndProcedure
; plain.s = "I am a fan of SEC now :-) lololoLOL" <---- This works
plain.s = "I am a fan of SEC now :-) lololoLOa" ; <----- This Doesn't Work
key.s = MD5Fingerprint("toto", 4) ; creating an AES key of 256 bits
Debug "clear = " + plain
Debug "len(clear) = " + Str(Len(plain))
Debug "key = " + key
length_required.l = Len(plain) ; length required for the buffer
Debug "length_required = " + Str(length_required)
*cypher_text = AllocateMemory(length_required) ; creating the buffer to the right input/output size
PokeS(*cypher_text, plain, length_required) ; copying our plain text into the buffer
Debug "LEN *cypher_text = " + Str(Len(PeekS(*cypher_text)))
;-encryption
*cypher_text = AESEncrypt(*cypher_text, @key) ; encrypting our plain text
Debug "encrypted = " + PeekS(*cypher_text, length_required)
Debug "LEN encrypted = " + Str(Len(PeekS(*cypher_text, length_required)))
;- decryption
;let's assume that this is a break in the code, like for instance an encrypted file that we did not encrypt
;or any encrypted data for which we do not know the size of the plain text
;*cypher_text would be @plain_text$ for instance
*plain_text = AESDecrypt(*cypher_text, @key) ; decrypting the cypher text
Debug "clear = " + PeekS(*plain_text);, length_required)
Debug "LEN clear = " + Str(Len(PeekS(*plain_text)))