David Kroll
David Kroll

Reputation: 39

Delphi 11.1 vs Delphi XE5 data type differences - Lockbox

I'm converting a Delphi XE5 app to Delphi 11.1 and having issues getting the same encrypted strings using the same version of Lockbox on each.

EncryptSymStr() calls TCodec.EncryptString(), which calls TSimpleCodec.EncryptString(), which then calls Stream_to_Base64(). Up to the call to Stream_to_Base64(), everything looks the same in debug.

The first difference I see is in Stream_to_Base64() for the variable ThreeBytes.

In XE5, I see:

ThreeBytes [0] = 253
           [1] = 206
           [2] = 0

In Delphi 11, I see:

ThreeBytes [0] = 105
           [1] = 155
           [2] = 1

Since I am using the same version of Lockbox in both environments, I assume there is some difference in the data types from XE5 to 11.

var
oCodeRDL:   TCodec;

cmdstr := '...---... ETS4162735 BUBBA '; 
cmdStr := EncryptSymStr(cmdStr);

function EncryptSymStr(Value: string): string;
var
   CipherText: ansistring;
begin
   if AllowEncryption then
   begin
      oCodeRDL.EncryptString(Value, CipherText);
      Result := CipherText;
   end
   else
      Result := Value;      
end;

procedure TCodec.EncryptString(
  const Plaintext: string; var CipherText_Base64: ansistring);
begin
InterfacesAreCached := True;
BeginEncDec;
FCodec.EncryptString( Plaintext, CipherText_Base64);
EndEncDec
end;

procedure TSimpleCodec.EncryptString(
  const Plaintext: string;
  var CipherText_Base64: ansistring);
var
  Temp: TMemoryStream;
  L: integer;
begin
Temp := TMemoryStream.Create;
try
  Begin_EncryptMemory( Temp);
  L := Length( Plaintext) * SizeOf( Char);
  if L > 0 then
    EncryptMemory( Plaintext[1], L);
  End_EncryptMemory;
  if FisUserAborted then
      CipherText_Base64 := ''
    else
      begin
      Temp.Position := 0;
      if isNotBase64Converter then
          CipherText_Base64 := Stream_to_Base64( Temp)
        else
          // If its already a Base64 encoder, no point in double-encoding it as base64.
          CipherText_Base64 := Stream_to_AnsiString( Temp)
      end
finally
  Temp.Free
end end;

  function  Stream_to_Base64( Source: TStream): ansistring;
var
  ThreeBytes: packed array[ 0..2 ] of byte;
  BytesRead: integer;
  P, j, i: integer;

begin
SetLength( result, (Source.Size + 2) div 3 * 4);
Source.Position := 0;
P := 1;
repeat
  BytesRead := Source.Read( ThreeBytes, 3);
  if BytesRead = 0 then break;
  for j := BytesRead to 2 do
    ThreeBytes[j] := 0;
  for j := 0 to BytesRead do
    begin
    result[ P ] := Base64Chars[ ( ThreeBytes[0] shr 2) + 1];
    Inc( P);
    for i := 0 to 1 do
      ThreeBytes[i] := (ThreeBytes[i] shl 6) + (ThreeBytes[i+1] shr 2);
    ThreeBytes[ 2] := ThreeBytes[ 2] shl 6
    end
until BytesRead < 3;
if BytesRead > 0 then
  for j := BytesRead to 2 do
    begin
    result[ P] := '=';
    Inc( P)
    end
end;

Upvotes: 0

Views: 203

Answers (0)

Related Questions