It is currently Fri Dec 13, 2019 4:43 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: [PB 4.40] AES encryption: Streaming demo
PostPosted: Fri Aug 14, 2009 2:20 am 
Offline
PureBasic Bullfrog
PureBasic Bullfrog
User avatar

Joined: Wed Jul 06, 2005 5:42 am
Posts: 8006
Location: Fort Nelson, BC, Canada
A small example showing how to use AES in the streaming version. Also note the use of the new #PB_Window_Tool flag:
Code:
; 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.

_________________
Veni, vidi, vici.


Last edited by netmaestro on Fri Aug 14, 2009 2:51 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Aug 14, 2009 2:47 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 3423
Location: New Zealand
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye