调用RC4的函数失败

pmp2001 2008-11-05 04:48:58
我找了一个RC4的代码,但是用起来有问题
请大家帮助看看

procedure TForm1.Button2Click(Sender: TObject);
var
rc4:TRC4;
ss0,ss1:ansistring;
begin
Rc4:=TRC4.create;
ss0:=rc4.Encrypt(pansichar(edit2.text),'andy');

ss1:=rc4.Decrypt(Pansichar(ss0),'andy') ;
end;


{*******************************************************}
{ }
{ RC4 encryption unit }
{ October 2006, Codius }
{ Thanks to Michael Puff, shapeless }
{ }
{*******************************************************}
unit UnRC4;

interface

type
PByteArray = ^TByteArray;
TByteArray = Array [0..32767] Of Byte;

TRC4 = class
private
D : array[Byte] of Byte;
I,J : Byte;
procedure Init(const Key: string);
procedure Done;
procedure Code(Source, Dest: pChar; Count: Integer);
public
function Encrypt(S: pChar; const Password: string): AnsiString;
function Decrypt(S: pChar; const Password: string): AnsiString;
end;

implementation

{ TRC4.Encrypt
This function will return the text(S) encrypted with the chosen password. }
function TRC4.Encrypt(S: pChar; const Password: string): AnsiString;
begin
SetLength(Result, Length(S));
Init(Password);
Code(pChar(S), pChar(Result), Length(S));
Done;
end;

{ TRC4.Decrypt
This function will return the text(S) decrypted with the chosen password. }
function TRC4.Decrypt(S: pChar; const Password: string): AnsiString;
begin
SetLength(Result, Length(S));
Init(Password);
Code(pChar(S), pChar(Result), Length(S));
Done;
end;

{ TRC4.Init
This routine will prepare the encryption/decryption. }
procedure TRC4.Init(const Key: string);
var
R, S, T, K : Byte;
U,L : Integer;
DummyArray : array [0..1599] of Char;
begin
{$R-}
{$Q-}
L := Length(Key);
I := 0;
J := 0;
R := 0;
U := 0;
for S := 0 to 255 do
D[S] := S;
for S := 0 to 255 do
begin
if (U < L) then
K := PByteArray(Key)[u]
else
K := 0;
Inc(U);
if (U >= L) then
U := 0;
Inc(R, D[S] + K);
T := D[S];
D[S] := D[R];
D[R] := T;
end;
Code(@DummyArray, @DummyArray, 1600);
end;

{ TRC4.Done
This routine will clean the variables used when encrypting/decrypting. }
procedure TRC4.Done;
begin
FillChar(D, sizeOf(D), 0);
FillChar(I, sizeOf(I), 0);
FillChar(J, sizeOf(J), 0);
end;

{ TRC4.Code
This routine will encrypt the text. }
procedure TRC4.Code(Source, Dest: pChar; Count: Integer);
var
S : Integer;
T : Byte;
begin
for S := 0 to (Count - 1) do
begin
Inc(I);
T := D[i];
Inc(J, T);
D[i] := D[J];
D[J] := T;
Inc(T, D[i]);
Byte(Dest[S]) := Byte(Source[S]) xor D[T];
end;
end;

end.
...全文
73 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
僵哥 2008-11-05
  • 打赏
  • 举报
回复
Encrypt和Decrypt的第一个参数都改为AnsiString而不是PChar

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧