Page 1 of 1

[PB 4.40] AES encryption: Streaming demo

Posted: Fri Aug 14, 2009 2:20 am
by netmaestro
A small example showing how to use AES in the streaming version. Also note the use of the new #PB_Window_Tool flag:

Code: Select all

; AES encryption: Streaming test
; netmaestro Aug 2009
; PB 4.40 Beta 1

Structure Init_Vector ; I picked an init vector size = cipher blocksize
  byte.a[32]           ; 256 bits = 32 bytes
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

*key = AllocateMemory(32) ; Memory for the key

key$ = "Four score and seven years ago, and so on and so on" 
result$ = MD5Fingerprint(@key$, Len(key$))
PokeS(*key, result$)

test$="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." 
test$+" Who's gonna do it? You? You, Lt. Weinberg? I have a greater responsibility than you can possibly fathom." 
test$+" You weep for Santiago and you curse the Marines. You have that luxury. You have the luxury of Not knowing what I know:" 
test$+" that Santiago's death, while tragic, probably saved lives. and my existence, while grotesque and incomprehensible" 
test$+" to you, saves lives...You don't want the truth. Because deep down, in places you don't talk about at parties," 
test$+" you want me on that wall. You need me on that wall. We use words like honor, code, loyalty...we use these words" 
test$+" As the backbone to a life spent defending something. You use 'em as a punchline. I have neither the time nor the inclination" 
test$+" to explain myself to a man who rises and sleeps under the blanket of the very freedom I provide, then questions the manner" 
test$+" 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" 
test$+" and stand a post. Either way, I don't give a damn what you think you're entitled to! " 


*raw     = AllocateMemory(StringByteLength(test$)+SizeOf(character)) ; Memory for the original string
*secure  = AllocateMemory(StringByteLength(test$)+SizeOf(character)) ; Memory for the encoded string
*decoded = AllocateMemory(StringByteLength(test$)+SizeOf(character)) ; Memory for the string after decoding

PokeS(*raw, test$) ; String goes into the *raw memory block


; Ready to go; try StartAesCipher - AddCipherBuffer - FinishCipher

;=============================================================================
; Encode

  chunksize = 128 ; Chunksize must be a multiple of 16

  numparts = MemorySize(*raw)/chunksize
  lastbit  = MemorySize(*raw)%chunksize

  StartAESCipher(0, *key, 256, *initvector, #PB_Cipher_CBC|#PB_Cipher_Encode)
 
  *srcloc  = *raw     ; Start pointers at the beginning of memory blocks
  *destloc = *secure
 
  For i=1 To numparts
    AddCipherBuffer(0, *srcloc, *destloc, chunksize)
    *srcloc  + chunksize ; increment pointers for src and dest
    *destloc + chunksize ; by the number of bytes in chunksize
  Next

  If lastbit
    AddCipherBuffer(0, *srcloc, *destloc, chunksize)
  EndIf 
 
  FinishCipher(0) 

  
;=============================================================================
; Decode

  chunksize = 16 ; I picked a different chunksize for decoding, just for fun

  numparts = MemorySize(*secure)/chunksize
  lastbit  = MemorySize(*secure)%chunksize

  StartAESCipher(0, *key, 256, *initvector, #PB_Cipher_CBC|#PB_Cipher_Decode)
 
  *srcloc  = *secure     ; Start pointers at the beginning of memory blocks
  *destloc = *decoded
 
  For i=1 To numparts
    AddCipherBuffer(0, *srcloc, *destloc, chunksize)
    *srcloc  + chunksize ; increment pointers for src and dest
    *destloc + chunksize
  Next

  If lastbit
    AddCipherBuffer(0, *srcloc, *destloc, chunksize)
  EndIf 
 
  FinishCipher(0) 


;============================================================================
; Did it decode all right?

  OpenWindow(0,0,0,320,320,"Decoded text:",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_Tool)
  SetWindowColor(0,#White)
  EditorGadget(0,20,20,280,280,#PB_Editor_ReadOnly)
  SetWindowLongPtr_(GadgetID(0),#GWL_EXSTYLE,GetWindowLongPtr_(GadgetID(0),#GWL_EXSTYLE) &~ #WS_EX_CLIENTEDGE)
  SetWindowPos_(GadgetID(0), 0,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE|#SWP_NOZORDER|#SWP_FRAMECHANGED)
  SendMessage_(GadgetID(0), #EM_SETTARGETDEVICE, 0,0)
  SetGadgetText(0, PeekS(*decoded))
  Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
It's not necessary to make an MD5 fingerprint of the key, of course. I just like it because it gives your encryption a strong key without making you remember a long complex password.

Posted: Fri Aug 14, 2009 2:47 am
by idle
I went off to read the manual thinking wait a minute, thats not an internal PB function before realising the "4.40" I should pay more attention.

Great addition to the pb libs

I'm sure the example will be useful, cheers.