Autor Beitrag
mimi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: So 11.04.04 13:27 
Hallo,
ich habe vor ein verschlüss... programm zu schreiben wo ich den start wert der Random funktion nutze
das prinzip sind so aus:
ich erstelle ein array mit 255 zahlen die den Ascci code dastellen und die bringe ich per zufall durcheinander.
beim entschlüsseln tausche ich einfach den wert mit dem index und fertig alles ist kein problem auser:
wie erstelle ich ein zufalls array wo jede zahl nur einmal drin vorkommt, bei meinen versuchen hängt er sich leider immer bei 240 auf, hier mein qullcode:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Buttons;

type
  TForm1 = class(TForm)
    Edit1: TLabeledEdit;
    Edit2: TLabeledEdit;
    Edit3: TLabeledEdit;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Edit4: TLabeledEdit;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function isarray(wert:Integer;a:array of Byte):bool;
var
  i:Integer;
begin

  for i:=0 to High(a) do begin
    if a[i] = wert then begin
      result:=True;
      Exit;
    end
  end;
  result:=False;  
end;

procedure ArrayToFile(t:array of Byte);
var
  L:TStringList;
  i:Integer;
begin
  l:=TSTringLIst.Create;
  for i:=0 to High(t) do begin
    l.add( intToStr(t[i]) );
  end;
  l.SaveToFile('d:\test.txt');
end;


procedure TForm1.BitBtn1Click(Sender: TObject);
var
  ascci:array[0..255] of Byte;
  i,r:Integer;
  raus:Bool;
begin
  RandSeed:=StrToInt(Edit3.Text);
  r:=0;
  
 for i:=0 to high(ascci) do begin
   repeat
     r:=random(high(ascci));
   until isArray(r,ascci) = False;
   ascci[i]:=r;

 end;

  for i:=0 to Length(Edit1.Text)-1 do begin
    Edit2.Text:=Edit2.Text + CHR( ascci[ord(Edit1.Text[i]) ] );
  end;
  ShowMessage(IntToSTr(Length(Edit1.Text) ) + '\' + IntToStr(Length(Edit2.Text) ));
  ArrayToFile(ascci);
end;

function GetArrayIndex(wert:Integer; ascci:array of Byte): Integer;
var
  i:Integer;
begin
  result:=-1;
  for i:=0 to High(ascci) do begin
    if ascci[i] = wert then begin
      result:=i;
      exit;
    end;

  end;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
var
  ascci,Temp:array[0..255] of Byte;
  i,r:Integer;
begin
  r:=0; i:=0;
  RandSeed:=StrToInt(Edit3.Text);
  
  for i:=0 to high(ascci) do begin
    r:=random(high(ascci));
    ascci[r]:=i;
  end;

  // array mit standartwerten festlegen
  for i:=0 to High(temp) do
    Temp[i]:=ascci[i];

  for i:=0 to Length(Edit2.Text) do begin
    Edit4.Text:=Edit4.Text + CHR( GetArrayIndex(ord(edit2.text[i]),ascci)  );
  end;
end;

end.

und irgendwie stellt er die 0 immer mehrmals in einem array ich weiß einfach nicht warum...[/delphi]

_________________
MFG
Michael Springwald, "kann kein englisch...."
catweasel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 487
Erhaltene Danke: 1

Win 7 64bit
Delphi 7 Second Sedition V7.2
BeitragVerfasst: So 11.04.04 16:32 
Musst du ma hier guggen :

www.delphi-forum.de/...hlight=nicht+doppelt
Catweasel

_________________
Pommes werden schneller fertig wenn man sie vor dem Frittieren einige Minuten in siedendes Fett legt.
mimi Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: So 11.04.04 18:21 
danke, es hat prima funktioniert, nur habe ich noch ein problem:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
Procedure CreateRandomValues(Var a:Array of byte); 
Var i,v,index1,index2:Integer; 
Begin 
  For i:=0 to High(a) do a[i]:=i; //Werte vorbelegen;

  For i:=0 to 255 do Begin
    index1:=Random(Length(a));
    index2:=Random(Length(a));
    v:=a[index1];
    a[index1]:=a[index2];
    a[index2]:=v;
  End;
End;

function GetArrayIndex(wert:Integer; ascci:array of Byte): Integer;
var
  i:Integer;
begin
  result:=-1;
  for i:=0 to High(ascci) do begin
    if ascci[i] = wert then begin
      result:=i;
      exit;
    end;  
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  ascci:array[0..255] of Byte;
  i:Integer;
begin
  RandSeed:=StrToInt(Edit3.Text);
  CreateRandomValues(ascci);

  for i:=0 to Length(Edit1.Text) do begin
    Edit2.Text:=Edit2.Text + CHR( ascci[ ord(Edit1.Text[i]) ] );
  end;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
var
  ascci:array[0..255] of Byte;
  i:Integer;
begin
  RandSeed:=StrToInt(Edit3.Text);
  CreateRandomValues(ascci);

  for i:=0 to Length(Edit2.Text) do begin
    Edit4.Text:=Edit4.Text + CHR( GetArrayIndex(ord(edit2.text[i]),ascci)  );
  end;
end;


beispiel:
Edit1.Text:='Hallo dies ist ein kleiner Test von heute';
Edit2.Text:='„$Äll2d~˜ý2~ý 2˜~é2]l˜~é˜r2€˜ý 2Té2ݘ– ˜';
Edit3.Text:='3';
Edit4.Text:=oHall dies ist ein kleiner Test vn heute;

warum geht das nicht genau, kann mir das jemmand bitte sagen ???

_________________
MFG
Michael Springwald, "kann kein englisch...."