type
TEnDeclass = class
private
Okey:array [1..3,1..4]of dword;//变换之后的3组KEY
keytab:array[0..47]of byte;
keytable:array[0..11] of Dword;
keysy:array[0..11]of Dword;
zhongzhi1:Dword; //种子
table1:array[0..255] of word; //表1 加密表
table2:array[0..255] of word; //表2
Buff:array[0..35]of dword;
procedure Zz_Key(Key_A,Key_B:Dword);
function Sf1(Meax:Dword):word;
procedure Sf2(Len,Mjs:Dword;var Tab1,tab2:array of word);
procedure Table_Star(Key:Dword);
{ Private declarations }
public
{ Public declarations }
procedure key_key(var A,b:DWORD);
procedure EnCode(Key_A,Key_B:Dword;var Bbuff:array of byte;Len:Word);
procedure DeCode(Key_A,Key_B:Dword;var Buff:array of Byte;Len:Word);
end;
implementation
procedure TEnDeclass.key_key(var A,b:DWORD);
//获得初始KEY的种子 A,B
var
int:integer;
begin
A:=$641378C6;//文件大小获得的数据
a:=0;
b:=0;
//01 42 C6 78 13 64
for int:=0 to 5 do
b:=b+keystar[int];
a:=GetTickCount;
end;
procedure TEnDeclass.Zz_Key(Key_A,Key_B:Dword);
//种子-->KEY
var
a,b:dword; //A,B两个KEY种子
int:word; //循环计数
str:string;//中间变量
key,key1,key2,key3:string;
Ikey,c,d:Dword;
begin
key1:=lowercase(inttohex(key_a,8)+inttohex(key_b,8));
key2:=uppercase(inttohex(key_a,8)+inttohex(key_b,8));
str:='00000000'+inttostr(key_a);
if length(str)>16 then
begin
key3:=copy(str,length(str)-8,9);
str:='0000'+inttostr(key_b);
key3:=key3+copy(str,1,7);
end
else
begin
key3:=copy(str,length(str)-7,8);
str:='00000000'+inttostr(key_b);
key3:=key3+copy(str,length(str)-7,8);
end;
//KEY由3部分组成
key:=key1+key2+key3;
//显示初始的KEY
str:='';
//开始对KEY进行变换
//用A对KEY1进行变换
a:=Key_A;
for int:=4 downto 1 do
begin
str:=copy(key1,int*4-3,4);
key:='$'+inttohex(ord(str[4]),2)+inttohex(ord(str[3]),2)+
inttohex(ord(str[2]),2)+inttohex(ord(str[1]),2);
Ikey:=strtoint(key);
Ikey:=Ikey xor a;
a:=a+1;
Okey[1,int]:=Ikey;
end;
//用B对KEY2进行变换
B:=Key_B;
for int:=4 downto 1 do
begin
str:=copy(key2,int*4-3,4);
key:='$'+inttohex(ord(str[4]),2)+inttohex(ord(str[3]),2)+
inttohex(ord(str[2]),2)+inttohex(ord(str[1]),2);
Ikey:=strtoint(key);
Ikey:=Ikey xor B;
B:=B+1;
Okey[2,int]:=Ikey;
end;
//用A+B对KEY3进行变换
a:=Key_A+Key_B;
for int:=4 downto 1 do
begin
str:=copy(key3,int*4-3,4);
key:='$'+inttohex(ord(str[4]),2)+inttohex(ord(str[3]),2)+
inttohex(ord(str[2]),2)+inttohex(ord(str[1]),2);
Ikey:=strtoint(key);
Ikey:=Ikey xor a;
a:=a+1;
Okey[3,int]:=Ikey;
end;
move(Okey,keytab,12*sizeof(Dword));
move(Okey,keytable,12*sizeof(Dword));
for int:=0 to 11 do
keysy[int]:=int;
int:=0;
Repeat
a:=keytab[int] and $b;
b:=keytab[int+1] and $b;
if a<>b then
begin
c:=keysy[a];
d:=keysy;
keysy[a]:=d;
keysy:=c;
end;
int:=int+2;
Until int>=$30;
end;
function TEnDeclass.Sf1(Meax:Dword):word;
//生成初始表的数据
var
Mecx:Dword;
begin
Mecx:=zhongzhi1;
Mecx:=Mecx * $343FD + $269EC3;
Mecx:=Mecx and $ffffffff;
zhongzhi1:=Mecx;
Mecx:=Mecx shr $10;
Mecx:=Mecx and $7fff;
sf1:=Mecx;
end;
procedure TEnDeclass.Sf2(Len,Mjs:Dword;var Tab1,tab2:array of word);
//排序并记录交换顺序
var
Mebx,Mebp:Dword;
Mesp20,MDx,MDi,Meax:word;
begin
Mebx:=0;
Mebp:=len;
Mesp20:=0;
while Mesp20=0 do
begin
if Mebx>Mebp then break;
Mebp:=len;
Mesp20:=1;
Meax:=1;
Repeat
Mdx:=tab1[Meax-1];
Mdi:=tab1[Meax];
if Mdx>Mdi then
begin
tab1[Meax-1]:=Mdi;
tab1[Meax]:=Mdx;
Mdi:=tab2[Meax-1];
Mdx:=tab2[Meax];
tab2[Meax]:=Mdi;
tab2[Meax-1]:=Mdx;
Mesp20:=0;
end;
Meax:=Meax+1;
Mebp:=Mebp-1;
Until Mebp=0;
end;
end;
procedure TEnDeclass.Table_Star(Key:Dword);
//生成加密&解密用的表
var
int,Mesi:word;
Mecx:Dword;
label label1;
begin
zhongzhi1:=Key;
Mesi:=0;
Repeat
table2[Mesi]:=Mesi;
int:=Sf1(zhongzhi1);
Mecx:=0;
if Mesi<>0 then
begin
Repeat
//Medx:=Mecx;
//Medx:=Medx and $ffff;
if int=table1[Mecx] then
begin
int:=Sf1(zhongzhi1);
Mecx:=0;
goto label1;
end;
Mecx:=Mecx+1;
label1:
Until Mecx>Mesi;
end;
table1[Mesi]:=int;
Mesi:=Mesi+1;
Until Mesi>=$100;
//到这里就是00405FDE
Sf2($ff,0,Table1,table2);
for int:=0 to 255 do
table1[int]:=int;
Sf2($ff,0,Table2,table1);
//现在的TABLE1就是转换之后的TABLE了可以直接用来加密和解密了
end;
Meax:=buff[Mesi+1];//1
Meax:=Meax xor Mebx;
Medx:=buff[Mesi]; //0
buff[Mesi+1]:=Medx;
buff[Mesi]:=Meax;
Mesp:=Mesp+3;
int:=int-1;
Until int=0;
Meax:=buff[Mesi];
Medx:=buff[Mesi+1];
buff[Mesi]:=Medx;
buff[Mesi+1]:=Meax;
Mesi:=Mesi+2;
Mebp:=Mebp-1;
Until Mebp=0;
//生成用于加密的数据表 TABLE1
Table_Star(Key_A);
//加密处理数据
move(buff,bbuff,len);
for int:=0 to len-1 do
begin
Bbuff[int]:=table1[Bbuff[int]];
end;
end;
procedure TEnDeclass.DeCode(Key_A,Key_B:Dword;var Buff:array of Byte;Len:Word);
var
int,int2:word;
begin
Table_Star(Key_A);
for int:=0 to len-1 do
begin
for int2:=0 to 255 do
begin
if buff[int]=table1[int2] then buff[int]:=int2;
end;
end;
end;