Thema: Prism RC4 und Delphi.Net

Einzelnen Beitrag anzeigen

skyware

Registriert seit: 11. Dez 2002
Ort: Wetzikon, Zürich
151 Beiträge
 
Delphi 6 Enterprise
 
#9

Re: RC4 und Delphi.Net

  Alt 17. Okt 2004, 14:07
Hallo zusammen,

danke für Eure Unterstützung, leider hab ich immer noch 2 Problemchen
die ich seit gestern nicht lösen konnte, bin am verzweifeln
Die Codezeilen die mir noch Probleme bereichen sind mit "//todo" markiert.
Hab scho diverses versucht mit Byte casten, mit Convert.GetBase64FromString etc.,
hat alles nichts geholfen
Please help

ps: Lars, unsafe Code möcht ich eigentlich vermeiden

Delphi-Quellcode:
unit HTFunctions_Crypt_RC4;

interface

uses
  {$IFDEF CLR}Borland.Vcl.SysUtils{$ELSE}SysUtils{$ENDIF};


function RC4Encode(const AString, AKey: string): string;
function RC4Decode(const AString, AKey: string): string;


implementation

type
  TRC4Context = record
    FD : array[Byte] of Byte;
    FI,
    FJ : Byte;
  end;

{ RC4Init:
-------------------------------------------------------------------------------}

procedure RC4Init(var ARC4: TRC4Context; const AKey: String);
var
  xR,
  xS,
  xT,
  xK : Byte;
  xU,
  xL : Integer;
begin
  xL := Length(AKey);
  with ARC4 do
  begin
    FI := 0;
    FJ := 0;
    for xS := 0 to 255 do FD[xS] := xS;
    xR := 0;
    xU := 0;
    for xS := 0 to 255 do
    begin
      {$IFDEF CLR}
      if xU < xL then xK := Byte(AKey[xU+1]) else xK := 0;
      {$ELSE}
      if xU < xL then xK := PByteArray(AKey)[xU] else xK := 0;
      {$ENDIF}
      Inc(xU);
      if xU >= xL then xU := 0;

      Inc(xR, FD[xS] + xK);
      xT := FD[xS];
      FD[xS] := FD[xR];
      FD[xR] := xT;
    end;
  end;
end;

{ RC4Code:
-------------------------------------------------------------------------------}

procedure RC4Code(var ARC4: TRC4Context; const ASource; var Dest; ACount: Integer);
var
  xS : Integer;
  xT : Byte;
begin
  with ARC4 do
    for xS := 0 to ACount -1 do
    begin
      Inc(FI);
      xT := FD[FI];
      Inc(FJ, xT);
      FD[FI] := FD[FJ];
      FD[FJ] := xT;
      Inc(xT, FD[FI]);
      {$IFDEF CLR}
      //todo
      TByteArray(Dest)[xS] := TByteArray(ASource)[xS] xor FD[xT];
      {$ELSE}
      TByteArray(Dest)[xS] := TByteArray(ASource)[xS] xor FD[xT];
      {$ENDIF}
    end;
end;

{ RC4Done:
-------------------------------------------------------------------------------}

procedure RC4Done(var ARC4: TRC4Context);
begin
  //todo
  FillChar(ARC4, SizeOf(ARC4), 0);
end;

{ RC4Encode:
-------------------------------------------------------------------------------}

function RC4Encode(const AString, AKey: string): string;
var
  xRC4 : TRC4Context;
begin
  SetLength(Result, Length(AString));
  RC4Init(xRC4, AKey);
  RC4Code(xRC4, AString[1], Result[1], Length(AString));
  Rc4Done(xRC4);
end;

{ RC4Decode:
-------------------------------------------------------------------------------}

function RC4Decode(const AString, AKey: string): string;
var
  xRC4 : TRC4Context;
begin
  SetLength(Result, Length(AString));

  RC4Init(xRC4, AKey);
  RC4Code(xRC4, AString[1], Result[1], Length(AString));
  Rc4Done(xRC4);
end;


end.
  Mit Zitat antworten Zitat