Autor Beitrag
Dragonclaw
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 196

Windows Vista
Delphi 7 Prof.
BeitragVerfasst: Sa 02.06.07 18:43 
Also Ich bin gerade dabei RSA umzusetzen.Dazu hab ich eine eigene Klasse (Trsa) in einer eigenen Unit gemacht.
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:
unit RSA;

interface

type
  Trsa = class(TObject)
  private
    { Private-Deklarationen }
    function IsPrime(ANumber: Integer): Boolean;
    function GcD(n, m: Integer): Integer;
  public
    { Public-Deklarationen }
    var P, Q, N, M, D, E : Integer;  // Variablen für den public und den Privat Key
    procedure Set_P_Q();             //P und Q bestimmen 
    procedure Set_all_left();        //Alles andere bestimmen 
  end;

implementation

{ Trsa }
function Trsa.GcD(n, m: Integer): Integer;  //Berechnet den größten gemeinsamen Teiler von 2 Zahlen
begin
    if m = 0 then Result := n
  else Result := GcD(m, n mod m);
end;

function Trsa.IsPrime(ANumber: Integer): Boolean;  //liefert TRUE wenn die zahl prim ist
Var
 Count : Integer;
begin
  Result  :=true;
  for Count := 2 to Trunc(sqrt(ANumber)) do
    begin
      if (ANumber mod Count = 0) then
        begin
          Result := false;
          break;
        end;
    end;
end;

procedure Trsa.Set_all_left;       // Alles andere wird gesetzt
begin
 N := P * Q;
 M := (P-1) * (Q-1);
 E := 2;
 N := 2;
 repeat
  Inc(E,1);
 until GcD(E,M) = 1;

 while (1 + n*m) mod e <> 0 do Inc(N,1);

 D := (1 + n*m) div e;
end;

procedure Trsa.Set_P_Q();   
var
  n: Integer;
begin
Randomize;
P := 4; 
while (not IsPrime(P)) do //solange P nicht prim ist soll er ne neue Zahl mit der Formel n²-n+41 ermitteln
  begin
    n := Random(70) + 10;
    P := n*n - n + 41;
  end;

Q := 4;
while (not IsPrime(Q)) AND (Q <> P) do //solange Q nicht prim ist und Q gleich P soll er das neu berechnen
  begin
    n := Random(70) + 10;
    Q := n*n + n + 17;
  end;
end;

end.


Und hier der Aufruf in der Hauptunit:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.Button1Click(Sender: TObject);
var
 Myrsa: Trsa;
begin
 Myrsa := Myrsa.Create;
 Myrsa.Set_P_Q();
 Myrsa.Set_all_left;
 Edit1.Text := IntToStr(Myrsa.P);
 Edit2.Text := IntToStr(Myrsa.Q);
 Edit3.Text := IntToStr(Myrsa.E);
 Edit4.Text := IntToStr(Myrsa.D);
 end;


Wenn ich jetzt den Button drucken berechnet er schön alle gewollten Zahlen, aber wenn ich dann das Programm beenden will kommt ne AccessViolation. Jetzt hab ich aber keine Ahnung warum das kommt? Muss ich die Variable am Ende freigeben? Oder hab ich sonst irgendwas übersehen?


Viele Grüße

dragonclaw
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 03.06.07 10:52 
Du musst eine Klasse mit Variable := TKlassentyp.Create; erzeugen ... Steht alles in der Hilfe drin ... bzw. auch häufig genug schon hier im Forum ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Dragonclaw Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 196

Windows Vista
Delphi 7 Prof.
BeitragVerfasst: So 03.06.07 11:11 
Tu ich doch...

ausblenden Delphi-Quelltext
1:
Myrsa := Myrsa.Create;					


Sonst würde der Fehler ja schon beim Clicken des Button des Button erscheinen und nicht erst beim Beenden.

Oder was meinst du???
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: So 03.06.07 12:05 
ausblenden Delphi-Quelltext
1:
Myrsa := TRSA.Create;					

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Dragonclaw Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 196

Windows Vista
Delphi 7 Prof.
BeitragVerfasst: So 03.06.07 13:33 
Okay... da hab ich Instanze und Klasse vertauscht.

vielen Dank für die hilfe!
Nilan
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 29

Win VistaSP1

BeitragVerfasst: So 03.06.07 15:43 
user profile iconBenBE hat folgendes geschrieben:
Du musst eine Klasse mit Variable := TKlassentyp.Create; erzeugen ... Steht alles in der Hilfe drin ... bzw. auch häufig genug schon hier im Forum ...


warum ist das so?

Nilan
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: So 03.06.07 15:49 
Warum dreht sich die Erde?

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Nilan
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 29

Win VistaSP1

BeitragVerfasst: So 03.06.07 16:05 
Wikipedia hat folgendes geschrieben:
Physikalische Grundlagen

Aufgrund ihres Drehimpulses vollführt die Erde eine Drehbewegung. Der Drehimpuls ist das Produkt aus der Drehgeschwindigkeit der Erde (ausgedrückt als Winkelgeschwindigkeit) und ihrem Trägheitsmoment.


:mrgreen: