开发者

Problem with TRandomStream - Delphi TurboPower LockBox 3

开发者 https://www.devze.com 2023-04-07 07:23 出处:网络
I am using TurboPower LockBox 3 (http://sourceforge.net/projects/tplockbox/ and http://lockbox.seanbdurkin.id.au/tiki-index.php)

I am using TurboPower LockBox 3 (http://sourceforge.net/projects/tplockbox/ and http://lockbox.seanbdurkin.id.au/tiki-index.php)

I have a problem with the TRandomStream. I'm trying to generate a chunk of 32 byte random data, but the output does not seem to be random.

The code is:

function StringToHex(S: String): String;
var I: Integer;
begin
  Result:= '';
  for I := 1 to length (S) do
    Result:= Result+IntToHex(ord(S[i]),2)+' ';
end;

procedure TForm1.btn1Click(Sender: TObject);
const
  Len = 32;
var
  j,k: Integer;
  P: Pointer;
  Str: AnsiString;
begin
  GetMem(P,Len);
  TRandomStream.Instance.Randomize;
  SetLength(Str,Len);
  for j := 1 to 20 do
  begin
    TRandomStream.Instance.Read(P^,Len);
    for k := 0 to Len-1 do
    begin
      Str[k+1]:=PAnsiChar(P)[k];
    end;
    mmoOutput.Lines.Add('> '+StringToHex(Str));
  end;
  FreeMem(P,Len);
end;

The output is:

> 91 79 00 77 FD F7 1C 51 22 64 BA 07 9F 87 8F B2 85 94 92 84 6B F7 5B 55 1A 0D DE E5 44 4A 56 DA 
> A5 A6 C3 3D DB 01 69 61 5A 66 D8 ED 3F 3B 4D 00 A5 D7 CB 84 BB 40 CE 3E 5A 90 54 DD BF 63 0E 2C 
> A5 5F 75 70 76 1F 08 25 5A E0 8D F0 9C 0B 15 F7 A5 9F C1 A7 F9 7C FF BA 5A 60 58 60 B5 97 F4 25 
> A5 9F 23 E2 09 D2 74 52 5A 60 AC 5E 6E D8 B6 81 A5 9F 33 B5 2B B8 B5 15 5A 60 4C 51 96 DB 68 66 
> A5 9F B3 4D 3A 51 4C 0D 5A 60 4C E6 D5 4C F8 83 A5 9F B3 11 AF 19 DB 2D 5A 60 4C 8E D2 D6 49 70 
> A5 9F B3 31 55 5D D1 28 5A 60 4C CE B7 26 56 C8 A5 9F B3 31 86 7A 83 A0 5A 60 4C CE E1 A5 B8 E8 
> A5 9F B3 31 0E 64 14 5E 5A 60 4C CE 31 9F CC EB A5 9F B3 31 4E B0 9B 4A 5A 60 4C CE B1 69 6C 04 
> A5 9F B3 31 4E 12 D6 AE 5A 60 4C CE B1 BD 6A C9 A5 9F B3 31 4E 22 A9 D0 5A 60 4C CE B1 5D 5D F1 
> A5 9F B3 31 4E A2 41 DF 5A 60 4C CE B1 5D F2 30 A5 9F B3 31 4E A2 05 54 5A 60 4C CE B1 5D 9A 2D 
> A5 9F B3 31 4E A2 25 FA 5A 60 4C CE B1 5D DA 12 A5 9F B3 31 4E A2 25 2B 5A 60 4C CE B1 5D DA 3C 
> A5 9F B3 31 4E A2 25 B3 5A 60 4C CE B1 5D DA 8C A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C 
> A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C 
> A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C 
> A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C 
> A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C A5 9F B3 31 4E A2 25 F3 开发者_如何学Go5A 60 4C CE B1 5D DA 0C 
> A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C 
> A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C 
> A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C 
> A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C 
> A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C A5 9F B3 31 4E A2 25 F3 5A 60 4C CE B1 5D DA 0C

Which does not seem to be random.

What am I doing wrong?

Thanks and regards.


OK. I've found the problem and a fix. Procedure TRandomStream.Crunch uses Compiler Version switching:

For Compiler Version > 17

FValue := FValue * Factor + 1 ;

Whilst for Compiler Version <= 17 (which is my case), the use of the function SquarePlus1Int64_NoOverflowChecks equates to:

FValue := FValue * FValue + 1; 

To fix change:

function SquarePlus1Int64_NoOverflowChecks(Factor: int64): int64;
begin
  result := Factor * Factor + 1
end;

to

function MultiplyPlus1Int64_NoOverflowChecks(Value,Factor: int64): int64;
begin
  result := Value * Factor + 1
end;

and in TRandomStream.Crunch change:

FValue := SquarePlus1Int64_NoOverflowChecks(FValue);

to

FValue := MultiplyPlus1Int64_NoOverflowChecks(FValue, Factor);
0

精彩评论

暂无评论...
验证码 换一张
取 消