I am using Delphi 10.3.2 I am trying to use the DCPCrypt units with the firemonkey framework to encrypt a string. It works 100% on Win32, Win64 and macOS 32 targets, the result is always the same. But when I compile for macOS 64, the result is different.
This is the code used:
function EncodeAES(code:ansistring; key:ansistring):string;
var
s,u:ansistring;
enc: TEncoding;
k,iv, Data, Crypt: TBytes;
Cipher: TDCP_rijndael;
begin
u:='';
enc:=TEncoding.ANSI;
Data := enc.GetBytes(code); // VMpuXJGbUNOv
k:=enc.GetBytes(key); // kj3214ed)k32nre2
iv:=enc.GetBytes(u);
Cipher:=TDCP_rijndael.Create(nil);
Cipher.Init(K[0], 128, @iv[0]);
Crypt:=Copy(Data, 0, Length(Data));
BytePadding(Crypt, Cipher.BlockSize, pmPKCS7);
Cipher.EncryptECB(Crypt[0], Crypt[0]);
Cipher.Free;
s:=tencoding.ANSI.GetString(crypt);
result:=StringToHex(s);
end;
and this is the BytePadding function:
procedure BytePadding(var Data: TBytes; BlockSize: integer; PaddingMode: TPaddingMode);
var
I, DataBlocks, DataLength, PaddingStart, PaddingCount: integer;
begin
BlockSize := BlockSize div 8;
if PaddingMode in [pmZeroPadding, pmRandomPadding] then
if Length(Data) mod BlockSize = 0 then Exit;
DataBlocks := (Length(Data) div BlockSize) + 1;
DataLength := DataBlocks * BlockSize;
PaddingCount := DataLength - Length(Data);
if PaddingMode in [pmANSIX923, pmISO10126, pmPKCS7] then
if PaddingCount > $FF then Exit;
PaddingStart := Length(Data);
SetLength(Data, DataLength);
case PaddingMode of
pmZeroPadding, pmANSIX923, pmISO7816: // fill with $00 bytes
FillChar(Data[PaddingStart], PaddingCount, 0);
pmPKCS7: // fill with PaddingCount bytes
FillChar(Data[PaddingStart], PaddingCount, PaddingCount);
pmRandomPadding, pmISO10126: // fill with random bytes
for I := PaddingStart to DataLength-1 do Data[I] := Random($FF);
end;
case PaddingMode of
pmANSIX923, pmISO10126:
Data[DataLength-1] := PaddingCount; // set end-marker with number of bytes added
pmISO7816:
Data[PaddingStart] := $80; // set fixed end-markder $80
end;
end;
I call the function like:
procedure TForm1.BtnClick(Sender: TObject);
var
s:string;
begin
s:=EncodeAES('VMpuXJGbUNOv','kj3214ed)k32nre2');
end;
Result with Win32/Win64/macOS 32 (correct): E32A9DE47CC60BDB70CA27885128D17A
Result with macOS 64 (wrong): CF622155545E485AC3A083E8A0478493
What am I doing wrong?
EncodeAES()
code is simply not doing that. – Remy LebeauUInt32
instead ofCardinal
– Remy Lebeau