AES256 CBC mode- decoder- encoder - & simple string handling

Share your advanced PureBasic knowledge/code with the community.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

AES256 CBC mode- decoder- encoder - & simple string handling

Post by walbus »

Full working AES256 CBC mode encoder and decoder, by Werner Albus, http://www.nachtoptik.de :-)
For string´s, ascii, unicode, and all other

Samples here :
Encrypt and decrypt any string´s in ascii or unicode
With included MD5 Checking !

! Looking also below Posting for simple String handling !

Code: Select all

; Full working AES256 CBC mode encoder and decoder, by Werner Albus, www.nachtoptik.de :-)
; Usable for string´s, ascii, unicode, and all other
; For speed up define the IV buffer global
; Use quad´s, if you want this feature !

; The length of the source and destination be divisible by 16 and the buffer must not be less than 16

; No warranty whatsoever
; Use at your own risk

EnableExplicit
Define decrypted$, encrypted$, md5$, md5_result$, extender$, result.q, len_buffer

Procedure.q AES256CBC_Encoder(*inBuffer.Quad, *outBuffer.Quad, bytes.q, *key, *i_v)
  ;(AES256_CBC mode encoder , by Werner Albus , www.nachtoptik.de :-)
  ;Many thanks NicTheQuick (www.freakscorner.de) for optimization this Code
  If (bytes % 16) : ProcedureReturn 0 : EndIf
  
  Protected *iv.Quad = AllocateMemory(16)
  Protected rounds.q = (bytes >> 4)
  Protected result.q
  
  CopyMemory(*i_v, *iv, 16)
  
  While result<bytes
    If Not AESEncoder(*inBuffer, *outBuffer, 16, *key, 256, *iv)
      FreeMemory(*iv) : ProcedureReturn 0
    EndIf
    *iv\q ! *outBuffer\q
    *outBuffer + 8
    *iv + 8
    *iv\q ! *outBuffer\q
    *outBuffer + 8
    *iv - 8
    *inBuffer + 16
    result + 16
  Wend

  FreeMemory(*iv)
  ProcedureReturn result
EndProcedure

;----------------------------

Procedure.q AES256CBC_Decoder(*inBuffer.Quad, *outBuffer.Quad, bytes.q, *key, *i_v)
  ;(AES256_CBC mode encoder , by Werner Albus , www.nachtoptik.de :-)
  ;Many thanks NicTheQuick (www.freakscorner.de) for optimization this Code
  If (bytes % 16) : ProcedureReturn 0 : EndIf
  
  Protected *iv.Quad = AllocateMemory(16)
  Protected rounds.q = (bytes >> 4)
  Protected result.q
  
  CopyMemory(*i_v, *iv, 16)
  
  While result<bytes
    If Not AESDecoder(*inBuffer, *outBuffer, 16, *key, 256, *iv)
      FreeMemory(*iv) : ProcedureReturn 0
    EndIf
    *iv\q ! *inBuffer\q
    *inBuffer + 8
    *iv + 8
    *iv\q ! *inBuffer\q
    *inBuffer + 8
    *iv - 8
    *outBuffer + 16
    result + 16
  Wend
  
  FreeMemory(*iv)
  ProcedureReturn result
EndProcedure

;---------- Sample - decode and encode a string -------------

decrypted$="This is a Test, the quick brown fox jumps over the lazy dog , 0123456789 , TICK_TACK" ; Teststring

Debug "": Debug "String to encrypt "+Str(Len(decrypted$))+" Char´s : " : Debug decrypted$ : Debug""

extender$=Space(64-Len(Str(Len(decrypted$))))+Str(Len(decrypted$)); Create a extender

; Fill the end off String with Spaces, make divisible by 16 and add the MD5 and the extender

md5$=MD5Fingerprint(@decrypted$,StringByteLength(decrypted$)) ; Create md5
decrypted$=LSet(decrypted$,Len(decrypted$)-Mod(Len(decrypted$),16)+16)+md5$+extender$
len_buffer=StringByteLength(decrypted$) : encrypted$=Space(len_buffer)

Debug "Decrypted String MD5 - Before Encryption :" ; Looking the MD5 before encryption
Debug md5$ : Debug ""

result=AES256CBC_Encoder(@decrypted$,@encrypted$,len_buffer,?key,?iv) ; Encoding

Debug "Encrypted "+Str(result)+" Bytes :" : Debug encrypted$ : Debug "" ; Encoding result

decrypted$=Space(len_buffer/(#PB_Compiler_Unicode+1)) ; For the result, create now a new destination buffer

result=AES256CBC_Decoder(@encrypted$,@decrypted$,len_buffer,?key,?iv) ; Decoding

If Mid(decrypted$,Len(decrypted$)-32,10)<>Space(10) Or Not result; Check result
  Debug"Decrypting ERROR " : Debug decrypted$ ; Decoding result´s a error
Else
  Debug"Decrypted "+Str(result)+" Bytes :" : Debug decrypted$ ; Decoding result is OK
  
  md5$=Mid(decrypted$,Len(decrypted$)-95,32)
  
  Debug "": Debug "Read back Decrypted String MD5 - After Decryption : "
  Debug md5$
  
  decrypted$=Left(decrypted$,Val(Right(decrypted$,22)))
  Debug "": Debug "Decrypted String "+Str(Len(decrypted$))+" Char´s : " : Debug decrypted$ ; Cut extender
  
  md5_result$=MD5Fingerprint(@decrypted$,StringByteLength(decrypted$)) ; Create md5
  Debug"" : Debug "Show resultet new String MD5 - After Decryption :"
  Debug md5_result$
EndIf

Debug""
If md5$=md5_result$
  Debug "Congratulation - All over OK :-)))"
Else
  Debug "Checksum ERROR"
EndIf

;--------------------------------------------- Data -------------------------------------------------

DataSection
  key:
  Data.b $06 , $a9 , $21 , $40 , $36 , $b8 , $a2 , $5b , $50 , $2e , $03 , $d6 , $36 , $12 , $01 , $06
  Data.b $05 , $1a , $13 , $35 , $29 , $d9 , $b5 , $4d , $41 , $e3 , $10 , $a3 , $24 , $00 , $10 , $12
  iv:
  Data.b $3e , $1f , $ba , $42 , $8d , $9e , $b4 , $40 , $b4 , $22 , $da , $71 , $2c , $5f , $ac , $43
EndDataSection
Last edited by walbus on Fri Mar 07, 2014 12:31 pm, edited 9 times in total.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: AES CBC Encryption - full working decoder and encoder

Post by walbus »

A first little fix is added
Many thanks JHPJHP
For Next works not with Quads
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: AES CBC Encryption - full working decoder and encoder

Post by netmaestro »

Debug "Encrypted "+Str(result)+" Bytes :" : Debug encrypted$ : Debug "" ; Encoding result
What you see here may or may not be accurate, the reason being that AES encoding can and often does produce encoded bytes that include zeros. While the memory block occupied by the string 'encrypted$' will contain the encrypted bytes in their entirety, they cannot be read directly as a string with confidence. The first zero you encounter will end the reading. If you want to view the results it's best to use a memory block for output and traverse it byte by byte outputting the chr value of each byte in the block.
BERESHEIT
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: AES CBC Encryption - full working decoder and encoder

Post by walbus »

Yes, that's obvious!
It is just a simple example to show that you can not read it after encoding.
00 Unicode or 0 Ascii cut the String.
For seeing real, use here as sample the PB Memoryviewer.

Edited:

More you can see here, a innovative Suite & SO &DLL

www.quick-aes-256.de

Best regards Werner
Last edited by walbus on Wed Mar 04, 2015 6:08 pm, edited 1 time in total.
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: AES256 CBC mode - decoder and encoder -Updated 02.27.201

Post by coco2 »

Very interesting, thank you I will be testing your code :)
Post Reply