Page 1 of 1

4.40b4 AES

Posted: Sun Oct 11, 2009 6:32 pm
by DataMiner
Hi everybody!

I played around with this new AES feature and found some strange effects:

Code: Select all

; AES Demo Unicode
;
String$ = "to short"
; Is it a bug or a feature? if this string is shorter than 16 characters, nothing will happen. So you have to fill it up.
Debug "Original-String: " + String$ + "  Length: " + Str(Len(String$))
If Len(String$) < 16: String$ + Space(16 - Len(String$)): Debug "String filled with Space to: " + Str(Len(String$)) + " characters": EndIf

BufferLen.l = StringByteLength(String$) ; This is the main problem for deciphering. You need to know the byte length of the original string! 
*CipheredString   = AllocateMemory(BufferLen)
If AESEncoder(@String$, *CipheredString, BufferLen, ?Key, 128, 0, #PB_Cipher_ECB)
  String$ = PeekS(*CipheredString)
  Debug "Ciphered: " + String$
EndIf
FreeMemory(*CipheredString)

If String$
  *DecipheredString = AllocateMemory(BufferLen)
  AESDecoder(@String$, *DecipheredString, BufferLen, ?Key, 128, 0, #PB_Cipher_ECB)
  String$ = Trim(PeekS(*DecipheredString, BufferLen / SizeOf(Character)))
  Debug "Deciphered: "+ String$ + "  Length: " + Str(Len(String$))
  FreeMemory(*DecipheredString)
EndIf

DataSection
  Key:
  Data.b $06, $A9, $21, $40, $36, $B8, $A1, $5B, $51, $2E, $03, $D5, $34, $12, $00, $06
EndDataSection
So, did I miss something?

Re: 4.40b4 AES

Posted: Sun Oct 11, 2009 6:39 pm
by netmaestro
So, did I miss something?
Yes, you can't do PeekS() on a ciphered memory block as it may contain zero bytes.

Re: 4.40b4 AES

Posted: Mon Oct 12, 2009 5:32 pm
by DataMiner
Thanks for your reply! I didn't know that. But why is it working if i fill the string up to 16 chars? Or is just luck?

Re: 4.40b4 AES

Posted: Mon Oct 12, 2009 5:36 pm
by netmaestro
Probably just luck. When you encrypt a text with AES the resulting encrypted memory block can and usually does contain Chr(0) bytes. This is not a problem, it's just that retrieving those encrypted bytes cannot be done using PeekS(), as the first Chr(0) that PeekS() encounters will make it believe that the string has ended and no more characters will be read.