1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
type MsgTypes =
(
MsgCliInfo = 0,
MsgEcmCw = 1,
MsgEmmAck = 2,
MsgCardDel = 4,
MsgCmd05 = 5,
MsgKeepalive = 6,
MsgCardAdd = 7,
MsgSrvInfo = 8,
MsgCmd_0A = 10,
MsgCmd_0B = 11,
MsgCmd_0C = 12,
MsgCmd_0D = 13,
MsgCmd_0E = 14,
MsgNewCardSidinfo = 15,
MsgEcmNok1 = 254,
MsgEcmNok2 = 255,
MsgSleepsend = 128,
MsgCachePush = 129,
MsgNoHeader = 65535
);
var
FixedData,CustomData:array of Byte;
CommandTag: MsgTypes;
DataLength:Integer;
var
_keytable:array[0..255] of Byte;
_counter,_sum,_state: byte;
procedure cc_crypt_init(key:array of Byte; len : integer);
var
i, j, k : integer;
begin
for i := 0 to 255 do _keytable[i] := i;
j := 0;
for i := 0 to 255 do
begin //%
j := $ff and (j + key[i mod len] + _keytable[i]);
k := _keytable[i];
_keytable[i] := _keytable[j];
_keytable[j] := k;
end;
_state := key[0];
_counter := 0;
_sum := 0;
end;
procedure cc_encrypt(var Data:array of Byte; len : integer);
var
i : integer;
k, z: byte;
_Data:array of Byte;
begin
SetLength(_Data, len);
for i := 0 to len-1 do
begin
_counter := $ff and (_counter + 1);
_sum := _sum + (_keytable[_counter]);
k := _keytable[_counter];
_keytable[_counter] := _keytable[_sum and $FF];
_keytable[_sum and $FF] := k;
z := data[i];
_data[i] := (z xor _keytable[_keytable[_counter and $FF] + _keytable[_sum and $FF] and $FF] xor _state);
_state := $ff and (_state xor z);
end;
FillChar(Data, len, #0);
Move(_data[0], Data[0], len);
end;
procedure cc_decrypt(var Data:Array of Byte; len : integer);
var
i : integer;
k, z : byte;
_Data:array of Byte;
begin
setlength(_Data, len);
for i := 0 to len-1 do
begin
_counter := $ff and (_counter + 1);
_sum := _sum + (_keytable[_counter]);
k := _keytable[_counter];
_keytable[_counter] := _keytable[_sum and $FF];
_keytable[_sum and $FF] := k;
z := data[i];
_data[i] := (z xor _keytable[_keytable[_counter] + _keytable[_sum and $FF] and $FF] xor _state);
z := data[i];
_state := $ff and (_state xor z);
end;
FillChar(Data, len, #0);
Move(_data, Data, len);
end;
procedure cc_crypt_xor(var data:array of Byte);
var
i:Integer;
_data:array of Byte;
begin
setlength(_data, 16);
for i := 0 to 8 do
begin
_data[8 + i] := Byte(i * data[i]);
if i < 5 then _data[i] := data[i] xor Byte(cccam[i]);
end;
Move(_data, data, 8);
end;
function CheckConnectionChecksum(wszbuf:Array of Byte; dwlen:Integer):Boolean;
var
sum1,sum2,sum3,sum4:Byte;
begin
Result := False;
if (dwlen = 16) then
begin
sum1 := (wszbuf[0] + wszbuf[4] + wszbuf[8]);
sum2 := (wszbuf[1] + wszbuf[5] + wszbuf[9]);
sum3 := (wszbuf[2] + wszbuf[6] + wszbuf[10]);
sum4 := (wszbuf[3] + wszbuf[7] + wszbuf[11]);
Result := (sum1 = wszbuf[12]) and (sum2 = wszbuf[13]) and (sum3 = wszbuf[14]) and (sum4 = wszbuf[15]);
end
else Result := False;
end;
function BuildPaCkets(dwSocket:TSocket; wMsg:Byte; var Data; dataLen:Integer):Boolean;
var
Header:Array of Byte;
HeaderLen : Integer;
begin
if wMsg = Byte(MsgTypes(MsgNoHeader)) then
begin
SetLength(Header, dataLen);
HeaderLen := DataLen;
Move(Data, Header[0], HeaderLen);
end
else
begin
SetLength(Header, 4 + dataLen);
HeaderLen := 4 + DataLen;
Header[0] := 0; // flags??
Header[1] := (wMsg and $ff);
Header[2] := (Datalen shr 8);
Header[3] := (Datalen and $ff);
Move(Data, Header[4], DataLen);
end;
cc_encrypt(Header[0], HeaderLen);
Result := (send(dwSocket, Header[0], HeaderLen, 0) <> 0);
if Result then SetLength(Header, 0);
end; |
Partager