AES256 CBC mode- decoder- encoder - & simple string handling
Posted: Mon Feb 24, 2014 7:53 pm
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 !

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