class procedure TCipher_RC4.GetContext(var ABufSize, AKeySize, AUserSize: Integer);
begin
ABufSize := RC4_BufSize;
AKeySize := 256;
AUserSize := 256 * 2;
end;
class function TCipher_RC4.TestVector: Pointer;
asm
MOV EAX,OFFSET @Vector
RET
@Vector: DB 02Dh,08Fh,0EEh,042h,087h,07Bh,0AEh,072h
DB 0F8h,02Bh,08Ch,0A5h,012h,014h,0A8h,07Eh
DB 07Eh,08Ch,0DBh,05Eh,096h,049h,06Ch,09Ch
DB 0EEh,05Eh,020h,06Ch,07Ah,067h,002h,05Dh
end;
procedure TCipher_RC4.Encode(Data: Pointer);
var
D: PByteArray;
B: PByte;
X,S: Byte;
begin
D := User;
B := Data;
for X := 0 to RC4_BufSize -1 do
begin
Inc(FI);
S := D[FI];
Inc(FJ, S);
D[FI] := D[FJ];
D[FJ] := S;
B^ := B^ xor D[(D[FI] + S) and $FF];
Inc(B);
end;
end;
procedure TCipher_RC4.Decode(Data: Pointer);
begin
Encode(Data);
end;
procedure TCipher_RC4.Init(const Key; Size: Integer; IVector: Pointer);
var
K: array[0..255] of Byte;
D: PByteArray;
I,J,S: Integer;
begin
InitBegin(Size);
FI := 0;
FJ := 0;
J := 0;
D := User;
for I := 0 to 255 do
begin
D[I] := I;
K[I] := PByteArray(@Key)[I mod Size];
end;
for I := 0 to 255 do
begin
J := (J + D[I] + K[I]) and $FF;
S := D[I];
D[I] := D[J];
D[J] := S;
end;
InitEnd(IVector);
{Save the Key}
Move(D[0], D[256], 256);
FSI := FI;
FSJ := FJ;
FillChar(K, SizeOf(K), 0);
end;
procedure TCipher_RC4.Done;
begin
inherited Done;
FI := FSI;
FJ := FSJ;
Move(PByteArray(User)[256], User^, 256);
end;
{-------------------------------------------------------------------------------
类 名:TRC4
方法名:Set_Key
返回值:无
参 数:无
功 能:初始化S-盒(参考RC4算法)
-------------------------------------------------------------------------------}
procedure TRC4.Set_Key;
var
J,KeyLength: Word;
I,SwapByte: Byte;
K_Box: array[Low(Byte)..High(Byte)] of Byte;
begin
I := 0;
J := 0;
KeyLength := Length(Key);
for I:= Low(Byte) to High(Byte) do begin
S_Box[I] := I;
K_Box[I] := Byte(Key[(I mod KeyLength) + 1]);
end;
for I := Low(Byte) to High(Byte) do begin
J := (J + S_Box[I] + K_Box[I]) mod (High(Byte)+1);
SwapByte := S_Box[I];
S_Box[I] := S_Box[J];
S_Box[J] := SwapByte;
end;
end;{Set_Key}
{-------------------------------------------------------------------------------
类 名:TRC4
方法名:Set_Code
返回值:无
参 数:无
功 能:计算密文或明文(参考RC4算法)
-------------------------------------------------------------------------------}
procedure TRC4.Set_Code;
var
I,J,T,SwapByte: Byte;
X: WORD;
begin
Set_Key;
I := 0;
J := 0;
for X := Low(Byte) to Length(Code)-1 do begin
I := (I + 1) mod (High(Byte)+1);
J := (J + S_Box[I]) mod (High(Byte)+1);
SwapByte := S_Box[I];
S_Box[I] := S_Box[J];
S_Box[J] := SwapByte;
T := (S_Box[I]+ S_Box[J]) mod (High(Byte)+1);
R_Box[X] := Byte(Code[X]) xor S_Box[T];
end;
end;{Set_Code}
{-------------------------------------------------------------------------------
类 名:TRC4
方法名:Get_Code
返回值:密文或明文
参 数:无
功 能:获得密文或明文
-------------------------------------------------------------------------------}
function TRC4.Get_Code():String;
var
I: Byte;
begin
Set_Code;
for I := Low(Byte) to Length(Code)-1 do begin
Result := Result + AnsiChar(R_Box[I]);
end;
end;{Get_Code}
Public Sub main()
Dim key As String
For i = 1 To 16
Randomize
key = key & Chr(Rnd * 255)
Next i
MsgBox RC4(RC4("Welcome To Plindge Studio!", key), key)
End Sub
Public Function RC4(inp As String, key As String) As String
Dim S(0 To 255) As Byte, K(0 To 255) As Byte, i As Long
Dim j As Long, temp As Byte, Y As Byte, t As Long, x As Long
Dim Outp As String
For i = 0 To 255
S(i) = i
Next
j = 1
For i = 0 To 255
If j > Len(key) Then j = 1
K(i) = Asc(Mid(key, j, 1))
j = j + 1
Next i
j = 0
For i = 0 To 255
j = (j + S(i) + K(i)) Mod 256
temp = S(i)
S(i) = S(j)
S(j) = temp
Next i
i = 0
j = 0
For x = 1 To Len(inp)
i = (i + 1) Mod 256
j = (j + S(i)) Mod 256
temp = S(i)
S(i) = S(j)
S(j) = temp
t = (S(i) + (S(j) Mod 256)) Mod 256
Y = S(t)
Outp = Outp & Chr(Asc(Mid(inp, x, 1)) Xor Y)
Next
RC4 = Outp
End Function
The RC5 encryption algorithm is a new fast symmetric block cipher
developed by Ron Rivest for RSA Laboratories. Intended as a possible
successor to the Data Encryption Standard, RC5 has a variable word
size, a variable number of rounds, and a variable-length secret key,
so the user can explicitly manipulate the trade-off between higher
speed and higher security. RC5 is suitable for hardware or software
implementations; the encryption and decryption algorithms are
exceptionally simple. A novel feature of RC5 is the heavy use of
data-dependent rotations.