Einzelnen Beitrag anzeigen

qwertz543221
(Gast)

n/a Beiträge
 
#5

Re: Chinesischer Restsatz, pseudocode gesucht

  Alt 21. Jun 2009, 13:23
Das ergebnis der entschlüsselung ist jedoch inkorrekt oder undurchführbar, und die schlüsselerstellung dauert ca 2x so lange wie zuvor.

Delphi-Quellcode:
procedure tform1.rsageneratekey;
var
limit,d,phi,n:ansistring;
prime:boolean;
mathe:tmathe;
i:longint;

  function modinvers(x,y:ansistring):ansistring;
        var z,k:ansistring;
        begin
        k:='1';
        z:='1';
        while mathe.Modulo(z,x)<>'0'  do
        begin
        z:=mathe.produkt(k,phi);
        mathe.Plus1(z);
        mathe.Plus1(k);
        end;
        result:=mathe.Quotient(z,x);
        end;

begin
mathe:=tmathe.Create;
application.ProcessMessages;
    InputQuery('Primzahlgrenze', 'Limit (65792<=x)', limit);
    if (limit='')or (mathe.Vergleich(limit,'65792')<0)
    then showmessage('Dieser Schlüssel kann wegen zu kleinem Modul n nicht gewählt werden.');
   begin
    prime:=false;
    while prime=false do
    begin
    p:=mathe.Zufall(mathe,'1',limit);
    Prime:=miller_rabin(p,30);
    end;
    prime:=false;
    while prime=false do
    begin
    q:=mathe.Zufall(mathe,'1',limit);
    prime:=miller_rabin(q,30);
    end;

    begin
    n:=mathe.Produkt(p,q);
    if (vergleich(n,'65792')<0) or (vergleich(limit,'256')<0)
     then showmessage('Modulus zu klein.')
     else
       begin
       mathe.Minus1(p);
       mathe.Minus1(q);
        phi:=mathe.Produkt(p,q);

        d:=modinvers(e,n);
        dp:=modinvers(e,mathe.Differenz(p,'1'));
        dq:=modinvers(e,mathe.Differenz(q,'1'));
        qinv:=modinvers(mathe.Summe(p,'1'),mathe.Summe(q,'1'));

        LabeledEdit1.Text:=d;
        LabeledEdit2.Text:=n;
        end;
        end;
        end;
        end;


function tform1.rsadec(text:ansistring;d,n:ansistring):ansistring;
var
textneu:ansistring;
i:int64;
wa,zahl,m_1,m_2,h:ansistring;//wa ist deine variable c
mathe:tmathe;

begin
textneu:='';
result:='';
mathe:=tmathe.Create;
i:=1;

while i<=length(text) do
  begin
  application.ProcessMessages;
  zahl:=copy(text,i,length(n));
  wa:=zahl;
  {
  m_1 = c^dp mod p and m_2 = c^dq mod q
  h = (m_1 - m_2) * qInv mod p
  m= m_2 + q * h
  }

  m_1:=mathe.PotenzModulo(wa,dp,p);
  m_2:=mathe.PotenzModulo(wa,dq,q);
  h:=mathe.Produkt(mathe.Differenz(m_1,m_2),mathe.Modulo(qinv,p));

  wa:=mathe.Summe(m_2,mathe.Produkt(q,h));
  textneu:=textneu+chr(strtoint(mathe.Quotient(wa,'256')))+chr(strtoint(mathe.Modulo(wa,'256')));
  i:=i+length(n)
  end;
result:=textneu;
end;

vielleicht habe ich deine anweisungen misverstanden
  Mit Zitat antworten Zitat