user1580348
user1580348

Reputation: 6053

DEC encryption creates same results after each program start

Using DEC (Delphi Encryption Compendium Version 5.2) in Delphi XE2, I encrypt a string with the following code below.
However, AFTER EACH PROGRAM START the same sequence of encryption results are generated. I have tried to use Randomize in FormCreate, but it does not help.

uses
  DECUtil, DECCipher, DECHash, DECFmt;

const
  MyPW = 'MyPassword';

var
  AKDFIndex: LongWord = 1;

function MyEncryptEx(const AText: string;
                     const APassword: string;
                     ATextFormat: TDECFormatClass;
                     AHashClass: TDECHashClass;
                     ACipherMode: TCipherMode;
                     ACipherClass: TDECCipherClass): string;
var
  ASalt: Binary;
  AData: Binary;
  APass: Binary;
begin
  with ValidCipher(ACipherClass).Create, Context do
    try
      ASalt := RandomBinary(16);
      APass := ValidHash(AHashClass).KDFx(APassword[1], length(APassword) * SizeOf(APassword[1]),
               ASalt[1], length(ASalt), KeySize, TFormat_Copy, AKDFIndex);
      Mode := ACipherMode;
      Init(APass);
      SetLength(AData, length(AText) * SizeOf(AText[1]));
      Encode(AText[1], AData[1], length(AData));
      Result := ValidFormat(ATextFormat).Encode(ASalt + AData + CalcMAC);          
    finally
      Free;
      ProtectBinary(ASalt);
      ProtectBinary(AData);
      ProtectBinary(APass);
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
end;

procedure TForm1.btnTestClick(Sender: TObject);
var
  EncrText: string;
begin
  EncrText := MyEncryptEx(edtHWID.Text, MyPW, TFormat_ESCAPE, THash_Whirlpool, cmCBCx, TCipher_Rijndael); 
end;

It seems that some Randomizer is not initialized after program start. So how can I get different encryption results after each program start?

Upvotes: 1

Views: 481

Answers (1)

Blorgbeard
Blorgbeard

Reputation: 103467

I found some source code. Looks like DECUtil's RandomBinary uses its own seed. You could try calling RandomSeed(RandomSystemTime, 4) instead of Randomize.

Upvotes: 2

Related Questions