AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Sonstige Fragen zu Delphi Delphi RC4 Problem bei Wikipedia-Pseudoquellcode

RC4 Problem bei Wikipedia-Pseudoquellcode

Ein Thema von fortuneNext · begonnen am 8. Feb 2009 · letzter Beitrag vom 18. Feb 2009
Antwort Antwort
fortuneNext

Registriert seit: 11. Aug 2007
Ort: Neuss
133 Beiträge
 
Delphi 7 Enterprise
 
#1

RC4 Problem bei Wikipedia-Pseudoquellcode

  Alt 8. Feb 2009, 22:58
Hi,
alternativ zum hier bestehenden RC4-Quelltext wollte ich gerne mal den Pseudoquellcode aus Wikipedia implementieren ( http://de.wikipedia.org/wiki/RC4#Algorithmus ). Die Initialisierungsprozedur sieht nun also so aus:

Delphi-Quellcode:
procedure TRC4.Initialize(key: string);
var
  j, tausch: byte;
  s: array[byte] of byte;
  l,i: integer;
begin
  i := 0;
  j := 0;
  l := length(s);
  for i := 0 to l do
    s[i] := i;
  for i := 0 to l do
  begin
    j := (j + s[i] + key[i mod length(key)]) mod l;
    tausch := s[i];
    s[i] := s[j];
    s[j] := tausch;
  end;
end;
Aber: Bei
j := (j + s[i] + key[i mod length(key)]) mod l; bekomme ich einen
Zitat:
[Fehler] uMain.pas(156): Inkompatible Typen
- Fehler, und zwar hier:
j := (j + s[i] + key[i mod length(key)]) | mod l; Was muss ich ändern?
Danke!

mfg
fortuneNext
Tim
"Hilfe & Support konnte nicht geöffnet werden. Bitte öffnen sie Hilfe & Support, um das Problem zu beheben."
"No Keyboard found. Press F1 to continue."
  Mit Zitat antworten Zitat
Dax
(Gast)

n/a Beiträge
 
#2

Re: RC4 Problem bei Wikipedia-Pseudoquellcode

  Alt 8. Feb 2009, 23:04
Pache ein Ord() um den Zugriff auf das Key-Element.
  Mit Zitat antworten Zitat
fortuneNext

Registriert seit: 11. Aug 2007
Ort: Neuss
133 Beiträge
 
Delphi 7 Enterprise
 
#3

Re: RC4 Problem bei Wikipedia-Pseudoquellcode

  Alt 8. Feb 2009, 23:14
Natürlich Danke dir
Tim
"Hilfe & Support konnte nicht geöffnet werden. Bitte öffnen sie Hilfe & Support, um das Problem zu beheben."
"No Keyboard found. Press F1 to continue."
  Mit Zitat antworten Zitat
fortuneNext

Registriert seit: 11. Aug 2007
Ort: Neuss
133 Beiträge
 
Delphi 7 Enterprise
 
#4

Re: RC4 Problem bei Wikipedia-Pseudoquellcode

  Alt 12. Feb 2009, 16:47
Hey, ich habe nochmal eine Frage. Ich habe den Code jetzt vollständig umgesetzt:
Delphi-Quellcode:
procedure TRC4.Initialize(key: string);
var
  i: integer;
  j, swap: byte;
begin
  i := 0;
  j := 0;
  for i := 0 to length(Alg_RC4.sandbox) do
    sandbox[i] := i;
  for i := 0 to length(Alg_RC4.sandbox) do
  begin
    j := (j + Alg_RC4.sandbox[i] + ord(key[i mod length(key)])) mod length(Alg_RC4.sandbox);
    swap := Alg_RC4.sandbox[i];
    Alg_RC4.sandbox[i] := Alg_RC4.sandbox[j];
    Alg_RC4.sandbox[j] := swap;
  end;
end;

function TRC4.Crypt(text: string):string;
var
  i, x:integer;
  j, swap:byte;
begin
  i := 0;
  j := 0;
  Result := '';
  for x := 1 to length(text) do
  begin
    i := (i + 1) mod length(Alg_RC4.sandbox);
    j := (j + Alg_RC4.sandbox[i]) mod length(Alg_RC4.sandbox);
    swap := Alg_RC4.sandbox[i];
    Alg_RC4.sandbox[i] := Alg_RC4.sandbox[j];
    Alg_RC4.sandbox[j] := swap;
    if not x = length(text) then
      Result := Result + char(Alg_RC4.sandbox[(Alg_RC4.sandbox[i] + Alg_RC4.sandbox[j]) mod length(Alg_RC4.sandbox)] xor ord(text[x + 1]))
    else
      Result := Result + char(Alg_RC4.sandbox[(Alg_RC4.sandbox[i] + Alg_RC4.sandbox[j]) mod length(Alg_RC4.sandbox)]);
  end;
end;
Wobei sandbox ein array[byte] of byte; ist.

Der Algorithmus verschlüsselt zwar den String - aber nicht nach RC4. Stattdessen kommt ein verschlüsselter Code heraus, welcher jedenfalls nicht wie mit einem anderen RC4-Tool verschlüsselt aussieht und beim nochmaligen Verschlüsseln einfach sich selber weider ausgibt. Hauptproblem ist also: Die Verschlüsselten verändern sich beim nochmaligen Verschlüsseln nicht mehr, was sehr seltsam ist. Entschlüsselugn ist auch nicht möglich, obwohl normalerweise ja das 2. Verschlüsseln den Text wieder entschlüsseln müsste.

Entdeckt jemand, wo mein Fehler bei der RC4- Umsetzung liegt?

Danke!
mfg
fortuneNext
Tim
"Hilfe & Support konnte nicht geöffnet werden. Bitte öffnen sie Hilfe & Support, um das Problem zu beheben."
"No Keyboard found. Press F1 to continue."
  Mit Zitat antworten Zitat
fortuneNext

Registriert seit: 11. Aug 2007
Ort: Neuss
133 Beiträge
 
Delphi 7 Enterprise
 
#5

Re: RC4 Problem bei Wikipedia-Pseudoquellcode

  Alt 18. Feb 2009, 07:24
Ich habe die letzte Woche ziemlich lange nach dem Problem gesucht, jedoch nichts gefunden. Kann vielleicht zumindest jemand einen Denkansatz geben, wo ca. das Problem sein könnte?
Danke!
Tim
"Hilfe & Support konnte nicht geöffnet werden. Bitte öffnen sie Hilfe & Support, um das Problem zu beheben."
"No Keyboard found. Press F1 to continue."
  Mit Zitat antworten Zitat
gammatester

Registriert seit: 6. Dez 2005
999 Beiträge
 
#6

Re: RC4 Problem bei Wikipedia-Pseudoquellcode

  Alt 18. Feb 2009, 09:36
Zitat von fortuneNext:
Ich habe die letzte Woche ziemlich lange nach dem Problem gesucht, jedoch nichts gefunden. Kann vielleicht zumindest jemand einen Denkansatz geben, wo ca. das Problem sein könnte?

Delphi-Quellcode:
    if not x = length(text) then
      Result := Result + char(Alg_RC4.sandbox[(Alg_RC4.sandbox[i] + Alg_RC4.sandbox[j]) mod length(Alg_RC4.sandbox)] xor ord(text[x + 1]))
    else
      Result := Result + char(Alg_RC4.sandbox[(Alg_RC4.sandbox[i] + Alg_RC4.sandbox[j]) mod length(Alg_RC4.sandbox)]);
  end;
end;
Nur ein paar Ratschläge und Hinweise:

- Das ganze Teil ist viel zu kompliziert und unübersichtlich, normalerweise is RC4 für seine verführerische Einfachheit bekannt.

- Es sollte zu denken geben, wenn Fallunterscheidungen und Sonderbehandlungen im Code auftauchen, die im Algorithmus nicht vorhanden sind.

- Operatoren-Prioritäten sollten beachtet werden: "not x = length(text)" ist sicherlich nicht das was Du willst. Allerdings hätte eine einfache Session mit dem Debugger das auch gezeigt.

Jedenfalls läuft das ganze, wenn man folgende ändert:

Delphi-Quellcode:
function Crypt(text: string):string;
var
  i, x:integer;
  j, swap:byte;
begin
  i := 0;
  j := 0;
  Result := '';
  for x := 1 to length(text) do
  begin
    i := (i + 1) mod length(sandbox);
    j := (j + sandbox[i]) mod length(sandbox);
    swap := sandbox[i];
    sandbox[i] := sandbox[j];
    sandbox[j] := swap;
    swap := sandbox[(sandbox[i] + sandbox[j]) mod length(sandbox)];
    Result := Result + char(swap xor byte(text[x]));
  end;
end;
Das ist in meiner vereinfachten Schreibweise und muß noch Deine abgepaßt werden. Noch eine (eigentlich triviale) Frage: Vor dem Entschlüsseln wird doch wohl auch Initialize aufgerufen?

Gammatester
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.017 Beiträge
 
Delphi 12 Athens
 
#7

Re: RC4 Problem bei Wikipedia-Pseudoquellcode

  Alt 18. Feb 2009, 10:15
in Wiki ist auch ein "grober" Fehler enthalten
Für i = 0 bis LängeVon(s) wir wissen doch, daß dieses
Für i = 0 bis LängeVon(s) - 1 sein muß !

[add]
hab's auch mal schnell von Wiki aus übersetzt und komm im Großen und Ganzen auf's Selbe wie gammatester
Delphi-Quellcode:
Procedure TRC4.Initialize(Const Key: TByteDynArray);
  Var i, j: Integer;
    Temp: Byte;

  Begin
    For i := Low(sBox) to High(sBox) do sBox[i] := i;
    j := 0;
    For i := 0 to Length(sBox) - 1 do Begin
      j := (j + sBox[i] + Key[i mod Length(Key)]) mod Length(sBox);
      Temp := sBox[i];
      sBox[i] := sBox[j];
      sBox[j] := Temp;
    End;
  End;

Function TRC4.Crypt(Const Data: TByteDynArray): TByteDynArray;
  Var i, j, k: Integer;
    Temp, RandomChar: Byte;

  Begin
    SetLength(Result, Length(Data));
    i := 0;
    j := 0;
    For k := 0 to Length(Data) - 1 do Begin
      i := (i + 1) mod Length(sBox);
      j := (j + sBox[i]) mod Length(sBox);
      Temp := sBox[i];
      sBox[i] := sBox[j];
      sBox[j] := Temp;
      RandomChar := sBox[(sBox[i] + sBox[j]) mod Length(sBox)];
      Result[k] := Chr(RandomChar xor Data[k]);
    End;
  End;
@fortuneNext: du solltest beachten, das in dem Wiki-Beispiel 0-Indizierte Arrays verwendet werden, aber ein String mit dem Index 1 beginnt, weßhalb du bei Key den Index um 1 anheben mußt.
Den Index für Text hast'e dagegen schon angehoben, durch x := 1 to Length(text).

so, wenn ich das Ganze dann noch etwas Kürze und es ebenfalls auf String umstell, dann komm ich auf Folgendes:
Delphi-Quellcode:
Type TRC4 = Record
    sBox: Array[Byte] of Byte;
    Procedure Initialize(Const Key: AnsiString);
    Function Crypt (Const Data: AnsiString): AnsiString;
  End;

Procedure TRC4.Initialize(Const Key: AnsiString);
  Var i, j: Integer;
    Temp: Byte;

  Begin
    For i := Low(sBox) to High(sBox) do sBox[i] := i;
    j := 0;
    For i := 0 to High(sBox) do Begin
      j := Byte(j + sBox[i] + Ord(Key[i mod Length(Key) + 1]));
      Temp := sBox[i];
      sBox[i] := sBox[j];
      sBox[j] := Temp;
    End;
  End;

Function TRC4.Crypt(Const Data: AnsiString): AnsiString;
  Var i, j, k: Integer;
    Temp: Byte;

  Begin
    SetLength(Result, Length(Data));
    i := 0;
    j := 0;
    For k := 1 to Length(Data) do Begin
      i := Byte(i + 1);
      j := Byte(j + sBox[i]);
      Temp := sBox[i];
      sBox[i] := sBox[j];
      sBox[j] := Temp;
      Result[k] := AnsiChar(sBox[Byte(sBox[i] + sBox[j])] xor Ord(Data[k]));
    End;
  End;

Procedure TForm1.FormCreate(Sender: TObject);
  Var RC4: TRC4;
    s: String;

  Begin
    S := 'abcdefghijklmnopqrstuvwxyz';
    Label1.Caption := S;

    RC4.Initialize('1234567980');
    S := RC4.Crypt(S);
    Label2.Caption := StringReplace(S, #0, #1, [rfReplaceAll]);

    RC4.Initialize('1234567980');
    S := RC4.Crypt(S);
    Label3.Caption := S;
  End;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
fortuneNext

Registriert seit: 11. Aug 2007
Ort: Neuss
133 Beiträge
 
Delphi 7 Enterprise
 
#8

Re: RC4 Problem bei Wikipedia-Pseudoquellcode

  Alt 18. Feb 2009, 16:03
Super, jetzt klappts wieder

Danke euch vielmals!

*und wieder was dazugelernt*
Tim
"Hilfe & Support konnte nicht geöffnet werden. Bitte öffnen sie Hilfe & Support, um das Problem zu beheben."
"No Keyboard found. Press F1 to continue."
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.017 Beiträge
 
Delphi 12 Athens
 
#9

Re: RC4 Problem bei Wikipedia-Pseudoquellcode

  Alt 18. Feb 2009, 16:15
Wie schon gesagt wurde, erstmal solltest du dir sorgen machen, wenn mehr im Code steht (siehe das IF-THEN), als im Pseudocode vorkommt.

if not x = length(text) then hier ist dir auch noch ein Fehler unterlaufen (auch wenn es ja eigenltich eh unnötig war)

da NOT vorrang vor = hat, übersetzt der Compiler es so
if (not x) = length(text) then ich vermute aber mal, daß du eigentlich dieses haben wolltest:
Delphi-Quellcode:
if not (x = length(text)) then
// bzw.
if x <> length(text) then
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:25 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz