Delphi 버전 RC4 암호화 알고리즘
7298 단어 Delphi
{===============================================================================
: Rc4_StrEncrypt()
: RC4 Based string encryption
: i_Encrypt —— 1 , 0 (integer );
s_EncryptText —— ( ) (string );
s_EncryptPassword —— ( ) (string );
i_EncryptLevel —— ( :1--10;integer ))
:
QQ: 93331961
=============================================================================== }
unit Rc4_Unit;
interface
uses
SysUtils;
function Rc4_StrEncrypt(i_Encrypt:integer; s_EncryptText,
s_EncryptPassword:string; i_EncryptLevel:integer = 1):string;
implementation
function Rc4_StrEncrypt(i_Encrypt:integer; s_EncryptText,
s_EncryptPassword:string; i_EncryptLevel:integer = 1):string;
var
v_EncryptModified, v_EncryptCipher, v_EncryptCipherBy:string;
i_EncryptCountA, i_EncryptCountB, i_EncryptCountC, i_EncryptCountD,
i_EncryptCountE, i_EncryptCountF, i_EncryptCountG, i_EncryptCountH,
v_EncryptSwap:integer;
av_EncryptBox:array[0..256, 0..2] of integer;
begin
if (i_Encrypt <> 0) and (i_Encrypt <> 1) then
begin
result := '';
end
else if (s_EncryptText = '') or (s_EncryptPassword = '') then
begin
result := '';
end
else
begin
if (i_EncryptLevel <= 0) or (Int(i_EncryptLevel) <> i_EncryptLevel) then
i_EncryptLevel := 1;
if Int(i_EncryptLevel) > 10 then i_EncryptLevel := 10;
if i_Encrypt = 1 then
begin
for i_EncryptCountF := 0 to i_EncryptLevel do
begin
i_EncryptCountG := 0;
i_EncryptCountH := 0;
v_EncryptModified := '';
for i_EncryptCountG := 1 to Length(s_EncryptText) do
begin
if i_EncryptCountH = Length(s_EncryptPassword) then
begin
i_EncryptCountH := 1;
end
else
begin
inc(i_EncryptCountH);
end;
v_EncryptModified := v_EncryptModified +
Chr(Ord(s_EncryptText[i_EncryptCountG]) xor
Ord(s_EncryptPassword[i_EncryptCountH]) xor 255);
end;
s_EncryptText := v_EncryptModified;
i_EncryptCountA := 0;
i_EncryptCountB := 0;
i_EncryptCountC := 0;
i_EncryptCountD := 0;
i_EncryptCountE := 0;
v_EncryptCipherBy := '';
v_EncryptCipher := '';
v_EncryptSwap := 0;
for i_EncryptCountA := 0 to 255 do
begin
av_EncryptBox[i_EncryptCountA, 1] :=
Ord(s_EncryptPassword[i_EncryptCountA mod Length(s_EncryptPassword) +
1]);
av_EncryptBox[i_EncryptCountA, 0] := i_EncryptCountA;
end;
for i_EncryptCountA := 0 to 255 do
begin
i_EncryptCountB := (i_EncryptCountB + av_EncryptBox[i_EncryptCountA][0]
+ av_EncryptBox[i_EncryptCountA][1]) mod 256;
v_EncryptSwap := av_EncryptBox[i_EncryptCountA][0];
av_EncryptBox[i_EncryptCountA][0] :=
av_EncryptBox[i_EncryptCountB][0];
av_EncryptBox[i_EncryptCountB][0] := v_EncryptSwap;
end;
for i_EncryptCountA := 1 to Length(s_EncryptText) do
begin
i_EncryptCountC := (i_EncryptCountC + 1) mod 256;
i_EncryptCountD := (i_EncryptCountD +
av_EncryptBox[i_EncryptCountC][0]) mod 256;
i_EncryptCountE := av_EncryptBox[(av_EncryptBox[i_EncryptCountC][0] +
av_EncryptBox[i_EncryptCountD][0]) mod 256][0];
v_EncryptCipherBy := inttostr(Ord(s_EncryptText[i_EncryptCountA]) xor
i_EncryptCountE);
v_EncryptCipher := v_EncryptCipher +
IntToHex(strtoint(v_EncryptCipherBy), 2);
end;
s_EncryptText := v_EncryptCipher;
end;
end
else
begin
for i_EncryptCountF := 0 to i_EncryptLevel do
begin
i_EncryptCountB := 0;
i_EncryptCountC := 0;
i_EncryptCountD := 0;
i_EncryptCountE := 0;
v_EncryptCipherBy := '';
v_EncryptCipher := '';
v_EncryptSwap := 0;
for i_EncryptCountA := 0 to 255 do
begin
av_EncryptBox[i_EncryptCountA, 1] :=
Ord(s_EncryptPassword[i_EncryptCountA mod Length(s_EncryptPassword) +
1]);
av_EncryptBox[i_EncryptCountA, 0] := i_EncryptCountA;
end;
for i_EncryptCountA := 0 to 255 do
begin
i_EncryptCountB := (i_EncryptCountB + av_EncryptBox[i_EncryptCountA, 0]
+ av_EncryptBox[i_EncryptCountA, 1]) mod 256;
v_EncryptSwap := av_EncryptBox[i_EncryptCountA, 0];
av_EncryptBox[i_EncryptCountA, 0] := av_EncryptBox[i_EncryptCountB,
0];
av_EncryptBox[i_EncryptCountB, 0] := v_EncryptSwap;
end;
for i_EncryptCountA := 1 to Length(s_EncryptText) do
begin
if (i_EncryptCountA mod 2) <> 0 then
begin
i_EncryptCountC := ((i_EncryptCountC + 1) mod 256);
i_EncryptCountD := ((i_EncryptCountD +
av_EncryptBox[i_EncryptCountC, 0]) mod 256);
i_EncryptCountE := av_EncryptBox[((av_EncryptBox[i_EncryptCountC, 0]
+ av_EncryptBox[i_EncryptCountD, 0]) mod 256), 0];
v_EncryptCipherBy := inttostr(StrToInt64('$' +
s_EncryptText[i_EncryptCountA] + s_EncryptText[i_EncryptCountA + 1])
xor i_EncryptCountE);
v_EncryptCipher := v_EncryptCipher +
Chr(strtoint(v_EncryptCipherBy));
end;
end;
s_EncryptText := v_EncryptCipher;
i_EncryptCountG := 0;
i_EncryptCountH := 0;
v_EncryptModified := '';
for i_EncryptCountG := 1 to Length(s_EncryptText) do
begin
if i_EncryptCountH = Length(s_EncryptPassword) then
begin
i_EncryptCountH := 1;
end
else
begin
i_EncryptCountH := i_EncryptCountH + 1;
end;
v_EncryptModified := v_EncryptModified +
Chr((Ord(s_EncryptText[i_EncryptCountG]) xor
Ord(s_EncryptPassword[i_EncryptCountH]) xor 255));
end;
s_EncryptText := v_EncryptModified;
end;
end;
result := s_EncryptText;
end;
end;
end.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Delphi] TStringBuilder그리고 꼭 사용해야만 할까? 그림처럼 Heap 영역에 "Hello" 공간을 생성하고 포인팅을 한다. "Hello World" 공간을 새로 생성한 후 포인팅을 하게 된다. 결국 "Hello" 라는 String 객체가 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.