Cipher AES, How IV and CBC mode works
Posted: Tue Nov 28, 2017 1:33 pm
How IV and CBC mode works - alterable code - full working
I see again and again that it is not understood how an IV or the CBC mode works.
This almost inevitably results in a wrong application, which can have fatal consequences.
Unfortunately, the PB manual is not helpful here
I have therefore broken down the way in which it works here
This codes works primary with any block chiffres
Also you can looking how this works, for creating other, not from PB supported crypt modes
The handling of these little tricky codes is extremely simple
Make a try, it is as sample available for creating CFB and OFB mode with this base codes, be creative !
Its only a little brain jogging
https://en.wikipedia.org/wiki/Block_cip ... _.28CFB.29
I see again and again that it is not understood how an IV or the CBC mode works.
This almost inevitably results in a wrong application, which can have fatal consequences.
Unfortunately, the PB manual is not helpful here
I have therefore broken down the way in which it works here

This codes works primary with any block chiffres
Also you can looking how this works, for creating other, not from PB supported crypt modes
The handling of these little tricky codes is extremely simple

Make a try, it is as sample available for creating CFB and OFB mode with this base codes, be creative !
Its only a little brain jogging

https://en.wikipedia.org/wiki/Block_cip ... _.28CFB.29
Code: Select all
; Author W. Albus - www.nachtoptik.de
; No warranty whatsoever
; Use at your own risk
;----------------- CBC Crypting - base code ------------------
Procedure AES_Encoder(*buffer_in.quad, *buffer_out.quad, bytes, *key, *iv )
Protected i, rounds=bytes>>4 -1
Static.q Dim iv(1)
CopyMemory(*iv, @iv(0), 16)
For i=0 To rounds
*buffer_in\q ! iv(0) : *buffer_in+8
*buffer_in\q ! iv(1) : *buffer_in-8
result=AESEncoder(*buffer_in ,*buffer_out ,16 ,*key ,256 ,0 , #PB_Cipher_ECB)
CopyMemory(*buffer_out, @iv(0), 16)
*buffer_in+16 : *buffer_out+16
Next i
ProcedureReturn result
EndProcedure
;---------------------------------------------
Procedure AES_Decoder(*buffer_in.quad, *buffer_out.quad, bytes, *key, *iv )
Protected i, rounds=bytes>>4 -1
Static.q Dim iv(1)
MoveMemory(*iv, @iv(0), 16)
For i=0 To rounds
result=AESDecoder(*buffer_in ,*buffer_out ,16 ,*key ,256 ,0 , #PB_Cipher_ECB)
*buffer_out\q ! iv(0) : *buffer_out+8
*buffer_out\q ! iv(1) : *buffer_out+8
CopyMemory(*buffer_in, @iv(0), 16)
*buffer_in+16
Next i
ProcedureReturn result
EndProcedure
;---------------------------------------------
string$="aaaaaaaaaaaaaaaa"+"bbbbbbbbbbbbbbbb"+"cccccccccccccccc"+"dddddddddddddddd"
len_string=StringByteLength(string$)
*buffer_0=AllocateMemory(len_string)
string_result$=Space(len_string)
; --------------------------------------------
AES_Encoder(@string$, *buffer_0, len_string, ?KEY, ?IV )
ShowMemoryViewer(*buffer_0,len_string)
; --------------------------------------------
AES_decoder(*buffer_0, @string_result$, len_string, ?KEY, ?IV )
; ShowMemoryViewer(@string_result$,len_string)
Debug string_result$
; --------------------------------------------
DataSection ; Sample key and initialization vector for coder test demo
KEY:
Data.b $09 , $a9 , $20 , $40 , $35 , $b8 , $a1 , $5b , $52 , $2e , $03 ,$d5 , $34 , $11 , $00 , $08
Data.b $11 , $b8 , $31 , $61 , $26 , $c3 , $32 , $64 , $d9 , $f3 , $01 ,$a4 , $27 , $61 , $56 , $29
IV:
Data.b $3d , $ae , $ba , $43 , $9d , $9e , $b5 , $30 , $b4 , $23 , $da ,$80 , $2d , $9f , $ac , $45
EndDataSection