As I see in OllyDBG problem is in erasing other variables by SetLength(dynamic array).
I have made demo, to show access violation problem:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type TChAr65535 = array[1..65535] of char;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
Const
LenAESKey = 16; // Both for simple and CBC AES samples (May be 16, 24 or 32)
INT_AES_ENCRYPT = 0;
INT_AES_DECRYPT = 1;
procedure aes_cbc_hack(const asource :TChAr65535;
const LenDataCBC :word;
var adestination :TChAr65535;
var wdestinationlen:word);
var KeyBin: array [0..Pred(LenAESKey)] of Byte=(
$01,$02,$03,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04);
var DataIn, DataIn2, DataOut: array of Byte;
var IV: array [0..Pred(16)] of Byte;
begin
sleep(7);
SetLength(DataIn,LenDataCBC); FillChar(DataIn, LenDataCBC, 0);
sleep(8);
SetLength(DataIn2,LenDataCBC);FillChar(DataIn, LenDataCBC, 0);
SetLength(DataOut,LenDataCBC);FillChar(DataIn, LenDataCBC, 0);
wdestinationlen:=LenDataCBC and $FFF0;
// Data, Key and Initial Vector (Just a sample)
sleep(11);
move(asource[(LenDataCBC and $000F)+1],DataOut,wdestinationlen); //LenDataCBC must be in form 16*n
sleep(13);
FillChar(IV, SizeOf(IV), 0);
sleep(14);
// CBC Decrypts encrypted data
Move(DataOut,DataIn2,wdestinationlen);
sleep(15);
//AESCBCCryDec(DataIn2,KeyBin,LenAESKey,INT_AES_DECRYPT,LenDataCBC,IV);
sleep(16);
// Results
Move(DataIn2,adestination,wdestinationlen);
end;
var buf2_th ,
buf1_th :TChAr65535;
NumRead, NumWritten:word;
f_c68:file;
begin
system.Assign(f_c68,'~Encoded.zip');
filemode := fmOpenRead;
Reset (f_c68,1);
BlockRead (f_c68,buf2_th,Sizeof(buf2_th),NumRead);
system.close(f_c68);
aes_cbc_hack(buf2_th,NumRead,buf1_th,NumWritten);
end;
end.