4.40b4 AES

Just starting out? Need help? Post your questions and find answers here.
DataMiner
User
User
Posts: 25
Joined: Mon Mar 28, 2005 1:29 pm
Location: Germany

4.40b4 AES

Post 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?
I am German - that's hard enough ;)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: 4.40b4 AES

Post by netmaestro »

So, did I miss something?
Yes, you can't do PeekS() on a ciphered memory block as it may contain zero bytes.
BERESHEIT
DataMiner
User
User
Posts: 25
Joined: Mon Mar 28, 2005 1:29 pm
Location: Germany

Re: 4.40b4 AES

Post 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?
I am German - that's hard enough ;)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: 4.40b4 AES

Post 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.
BERESHEIT
Post Reply