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:
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 public 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;
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]