Delphi FMX Stream Read Cardinal on OSX is different to WIN64 / WIN32 RAD Studio 10.2.3 LockBox AES

  

I have a string filled with text and integer values, that is loaded at a TMemoryStream from a file.

Reading from this stream by some ReadString Function and Stream.Read(i, SizeOf(Cardinal)) is resulting in expected values for all String and Int values on Windows32/64.

Doing the same on OSX an expected int value of 211 is read with 37827 and the position value of the stream gets wrong after reading this value.

Normally I thought cardinal is some platform independed data type?
Any idea…?

If I compare the string loaded from debugger of OSX and WIn64, they are identical.

……

I did some deeper investigation now and found the reason in some AES encoding and decoding made by Lockbox 3.6.3 after textfile load/save in stream.

The sample code is failing after include DecodeAES and EncodeAES on OSX… Windows is working. The reason seems to be some missing Char in the Unicode table used for encoding. The error at OSX side comes from the Lockbox coding. How to do some AES Endcoding/Decoding for strings or files, that is working platform independent?

private
{ Private declarations }
ms: TMemoryStream;
ss: TStringStream;

function DecodeAES(const Code: String): String;
var
CodecAES: TCodec;
Decode: String;
begin
Decode := ”;
CodecAES := TCodec.Create(Nil);
try
CodecAES.CryptoLibrary := Clib;
CodecAES.StreamCipherId := ‘native.StreamToBlock’;
CodecAES.Password := ‘1234’;
CodecAES.BlockCipherId := ‘native.AES-256’;
CodecAES.ChainModeId:= ‘native.CBC’;
CodecAES.DecryptString(Decode,Code);
finally
FreeAndNil(CodecAES);
end;
result := Decode;
end;

function EncodeAES(const Pure: String): String;
var
CodecAES: TCodec;
Encode: String;
begin
Encode := ”;
CodecAES := TCodec.Create(Nil);
try
CodecAES.CryptoLibrary := Clib;
CodecAES.StreamCipherId := ‘native.StreamToBlock’;
CodecAES.Password := ‘1234’;
CodecAES.BlockCipherId := ‘native.AES-256’;
CodecAES.ChainModeId:= ‘native.CBC’;
CodecAES.EncryptString(Pure,Encode);
finally
FreeAndNil(CodecAES);
end;
result := Encode;
end;


procedure TForm8.btnreadstreamClick(Sender: TObject);
var
I: Cardinal;
st: String;
begin
ss.clear;
ms.clear;
ss.LoadFromFile(TPath.GetHomePath + TPath.DirectorySeparatorChar +
‘testdata’ + TPath.DirectorySeparatorChar+ ‘test.txt’);
st := DecodeAES(ss.DataString);
ss.Clear;
ss.WriteString(st);
ms.LoadFromStream(ss);
ms.Position := 0;
Edit1.Text := ReadStringStream(ms);
ms.Read(i, SizeOf(Cardinal));
Edit1.Text := Edit1.Text+’ ‘+ i.ToString;
end;

procedure TForm8.btnsavestreamClick(Sender: TObject);
var
I: Cardinal;
st: String;
begin
ms.Clear;
ms.Position := 0;
writeStringStream(‘192.168.120.10’, ms);
i := 211;
ms.Write(i,SizeOf(Cardinal));
ss.LoadFromStream(ms);
st := EncodeAES(ss.DataString);
ss.Clear;
ss.WriteString(st);
ss.SaveToFile(TPath.GetHomePath + TPath.DirectorySeparatorChar + ‘testdata’
+ TPath.DirectorySeparatorChar+ ‘test.txt’);
end;

Comments are closed.