[PB 4.40] AESEncoder() and Strings

Everything else that doesn't fall into one of the other PB categories.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

[PB 4.40] AESEncoder() and Strings

Post by Little John »

Hi all!
Example in the documentation of AESEncoder() wrote:If AESEncoder(@String$, *CipheredString, Len(String$), ?Key, 128, ?InitializationVector)
Debug "Ciphered: "+PeekS(*CipheredString)
So do the data that are produced by AESEncoder() never contain Chr(0), so that it's always safe to use PeekS() like in this example?

Regards, Little John
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: [PB 4.40] AESEncoder() and Strings

Post by netmaestro »

The encoded string will indeed contain zeros, you can't use peeks() on it. If you want to look at it in its raw form you'll have to think of something else.
BERESHEIT
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [PB 4.40] AESEncoder() and Strings

Post by Little John »

Hi netmaestro,

thanks for clarifying that!
I hope it will be corrected in the docs.

Regards, Little John
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: [PB 4.40] AESEncoder() and Strings

Post by netmaestro »

Tiny demo to show:

Code: Select all

;; Text encryption test...

key$ = "Four score and seven years ago, and so on and so on"

a$="You can't handle the truth! Son, we live in a world that has walls. And those walls have to be guarded by men with guns."
a$+" Who's gonna do it? You? You, Lt. Weinberg? I have a greater responsibility than you can possibly fathom."
a$+" You weep for Santiago and you curse the Marines. You have that luxury. You have the luxury of Not knowing what I know:"
a$+" that Santiago's death, while tragic, probably saved lives. and my existence, while grotesque and incomprehensible"
a$+" to you, saves lives...You don't want the truth. Because deep down, in places you don't talk about at parties,"
a$+" you want me on that wall. You need me on that wall. We use words like honor, code, loyalty...we use these words"
a$+" As the backbone to a life spent defending something. You use 'em as a punchline. I have neither the time nor the inclination"
a$+" to explain myself to a man who rises and sleeps under the blanket of the very freedom I provide, then questions the manner"
a$+" in which I provide it! I'd rather you just said thank you and went on your way. Otherwise, I suggest you pick up a weapon"
a$+" and stand a post. Either way, I don't give a damn what you think you're entitled to! "

Structure Init_Vector ; I picked an init vector size = cipher blocksize
  byte.a[32]
EndStructure

*initvector.Init_Vector = AllocateMemory(32)

RandomSeed(189470) ; for testing I'm using same init vector every time

For i=0 To 31
  *initvector\byte[i] = Random(255)
Next

*encoded = AllocateMemory(Len(a$))

AESEncoder(@a$,*encoded,Len(a$),@key$,128,*initvector)
For i=*encoded To *encoded+MemorySize(*encoded)
  charlist$+RSet(Str(PeekB(i)&$FF),3,"0")+","
Next
charlist$ = Left(charlist$,Len(charlist$)-1)

Debug "Encoded memory block contains "+Str(CountString(charlist$,"000"))+" zero bytes"

*decoded = AllocateMemory(MemorySize(*encoded))
AESDecoder(*encoded,*decoded,MemorySize(*encoded),@key$,128,*initvector)

result$ = PeekS(*decoded,MemorySize(*decoded))

MessageRequester("",result$, $80)
BERESHEIT
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [PB 4.40] AESEncoder() and Strings

Post by Little John »

netmaestro wrote:Tiny demo to show:
Oh yes! We actually should be aware of this, when using AESEncoder().
Thanks again!

Regards, Little John
Post Reply