function
RC4(Expression, Password:
string
):
string
;
var
RB :
array
[
0..255
]
of
Integer
;
X, Y, Z:
longint
;
Key :
array
of
byte
;
ByteArray :
array
of
Word
;
Temp:
byte
;
Counter:
integer
;
begin
If
(Length(Expression) =
0
)
then
Exit;
If
(Length(Password) =
0
)
then
Exit;
If
(Length(Password) >
256
)
then
begin
Password := Copy(Password,
1
,
256
);
end
;
SetLength(Key, Length(Password));
for
Counter :=
0
to
Length(Password) -
1
do
begin
Key[Counter] := ord(Password[Counter +
1
]);
end
;
for
X :=
0
to
255
do
begin
RB[X] := X;
end
;
X :=
0
;
Y :=
0
;
Z :=
0
;
for
X :=
0
to
255
do
begin
Y := (Y + RB[X] + Key[X
mod
Length(Password)])
mod
256
;
Temp := RB[X];
RB[X] := RB[Y];
RB[Y] := Temp;
end
;
X :=
0
;
Y :=
0
;
Z :=
0
;
SetLength(ByteArray, Length(Expression));
for
Counter :=
0
to
Length(Expression) -
1
do
begin
ByteArray[Counter] := ord(Expression[Counter +
1
]);
end
;
for
X :=
0
to
Length(Expression)
do
begin
Y := (Y +
1
)
mod
256
;
Z := (Z + RB[Y])
mod
256
;
Temp := RB[Y];
RB[Y] := RB[Z];
RB[Z] := Temp;
ByteArray[X] := ByteArray[X]
xor
(RB[(RB[Y] + RB[Z])
mod
256
]);
end
;
for
Counter :=
0
to
Length(Expression) -
1
do
begin
Result := Result + chr(ord(ByteArray[Counter]));
end
;
end
;