Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
Quote:
Originally Posted by
=Battle=
Who knows explain in detail for what mu_encdec and mu_packet
mu_packet contents a class that gives to packet location (unsigned char) commons functions that are usually used (get header, get headersize...).
mu_encdec contents functions to decrypt encrypt these packets.
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
An issue has been found by sircid in MuPacketEncSize function. It incorrectly calculates destination buffer size if pkt->contentSize() %8 ==0, because in the code there wasn't taken in account one byte for enc_key. Here is a fix:
inline unsigned short MuPacketEncSpace( muPacket* pkt) {
unsigned short contentSize =pkt->contentSize() +1;
return((( contentSize /8) +(((contentSize %8) >0) ? 1 : 0)) *11) +pkt->hdrSize();
}
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
Quote:
Originally Posted by
Evolver
An issue has been found by sircid in MuPacketEncSize function. It incorrectly calculates destination buffer size if pkt->contentSize() %8 ==0, because in the code there wasn't taken in account one byte for enc_key. Here is a fix:
inline unsigned short MuPacketEncSpace( muPacket* pkt) {
unsigned short contentSize =pkt->contentSize() +1;
return((( contentSize /8) +(((contentSize %8) >0) ? 1 : 0)) *11) +pkt->hdrSize();
}
Now everything works 100% =)!
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
Hey guys, when I need to use MuPacketEncSpace() and MuPacketDecSpace() functions?
Until now, I was able to encrypt and decrypt without them. o_0
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
Quote:
Originally Posted by
thiforums
Hey guys, when I need to use MuPacketEncSpace() and MuPacketDecSpace() functions?
Until now, I was able to encrypt and decrypt without them. o_0
You need it coz they give you size of bytes that decrypted/encrypted packet would use. Which size do you set when you create an array :S?
1 Attachment(s)
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
here is delphi version
Code:
unit MUOnlineEncrypt;
interface
uses windows,
sysutils,
classes;
var
//Encryptacion
resultadoDesencriptado:TByteArray; //Desencryptado
resultadoEncriptado:TByteArray; //Encryptado
DecryptKeysServer:Array[0..11]of LongWord; //Llave para Desencriptacion en el Servidor y Encriptacion en el Cliente
EncryptKeysServer:Array[0..11]of LongWord; //Llave para Encriptacion en el Servidor y Desencriptacion en el Cliente
EncLen:Integer;
TempEnc:Array[0..3]of LongWord;
TempByte:PByteArray;
TempWord:Array[0..3]of Word;
XorByte:Byte;
TempDest,TempSrc:PByteArray;
TempPtr:PByteArray;
//PROCEDIMIENTOS PARA MUONLINE
procedure LoadKeys(ARchivo:String;var Where:Array of LongWord); //Carga las llaves desde Los archvios Enc1.dat y Dec2.dat
procedure DesencriptarC1(Coded:PByteArray;inicio,size:Integer);
procedure DesencriptarC2(Coded:PByteArray;inicio,size:Integer);
function DesencriptarC3(Coded:PByteArray;inicio,size:Integer;ToServer:Boolean):Integer;
procedure EncriptarC1(Data:PByteArray;inicio,size:Integer);
procedure EncriptarC2(Data:PByteArray;inicio,size:Integer);
function EncriptarC3(Data:PByteArray;inicio,size:Integer;ToServer:Boolean):Integer;
function coef2(a,b:integer):integer;
function coef1(a,b:integer):integer;
//EXAMPLES
const TestLen=$18;
const Temp6: array[0..TestLen-1] of Byte =
// ($C3,$0D,$06,$8A,$36,$37,$0B,$7C,$76,$6D,$35,$05,$30); //ServerToClient
// ($C3,$18,$70,$27,$2D,$95,$0A,$F9,$35,$E1,$08,$BD,$88,$5D,$FC,$2C,$57,$5C,$F5,$A1,$E0,$D1,$42,$77); //ServerToClient
// ($C3,$18,$D2,$39,$20,$BF,$91,$F2,$13,$3C,$C1,$A0,$95,$5D,$FC,$2C,$57,$5C,$F5,$A1,$E0,$D1,$4B,$77); //ServerToClient MOVE CDA+9+S+L (46CCE5) TO HAND (00)
($C3,$18,$8B,$90,$78,$F0,$13,$6A,$50,$31,$2D,$8C,$B9,
$3C,$82,$5D,$25,$D9,$05,$C3,$8B,$24,$57,$69); //ClientToServer NOT WORKING YET!! T_T!!
// ($C3,$0D,$3C,$82,$5D,$25,$D9,$05,$C3,$8B,$24,$57,$69); //ClientToServer NOT WORKING YET!! T_T!!
// ($C3,$18,$8B,$90,$78,$F0,$13,$6A,$50,$31,$2D,$0C,$39,
// $B4,$F0,$04,$94,$56,$07,$30,$A1,$D8,$7B,$4E); //ClientToServer NOT WORKING YET!! T_T!!
const KeyByteC1C2: array[0..31] of Byte =(231, **** 58, 137, 188, 178, 159, ****
35, 168, 254, 182, 73, 93, 57, 93,
138, 203, *** 141, 234, 125, 43, 95,
195, 177, 233, 131, 41, 81, 2*** 86);
const KeyByteC3C4: array[0..15] of Byte =($9B,$A7,$08,$3F,$87,$C2,$5C,$E2,
$B9,$7A,$D2,$93,$BF,$A7,$DE,$20);
//FsGate 1
//7D299AF0C73940FFCCAA1CE39E332A1C
//FsGate 2
//98 25 87 94 3F DE C2 1B B3 A2 0E F8 5B 92 1A A1
//GameServer
Const GameServerKey: Array[0..28] of Byte=($76,$FB,$E3,$9B,$5D,$D8,$B3,$3A,$25,$54,$95,$D0,$9C,$D6,$11,$95,
$A8,$61,$6D,$0B,$B9,$25,$6B,$0B,$74,$B1,$6B,$0B,$F8);
function Test():integer;
implementation
function Test():integer;
var
i:integer;
len:integer;
Str:String;
A,B:String;
P:PByte;
Pc,Pd:PByte;
begin
LoadKeys('Enc1.dat',DecryptKeysServer);
LoadKeys('Dec2.dat',EncryptKeysServer);
A:='';
SetLength(B,TestLen*2);
for i:=0 to TestLen-1 do begin
A:=A+chr(Temp6[i]);
end;
for i:=0 to TestLen*2-1 do begin
B[i+1]:=chr(0);
end;
Str:='';
WriteLn('Mensaje Original');
for i:=0 to TestLen-1 do begin
if ((i mod 16)=0)then begin
WriteLn(Str);
Str:='';
end;
Str:=Str+IntToHex(Temp6[i],2)+' ';
end;
WriteLn(Str);
Str:='';
WriteLn('Llave de Encriptacion en el Servidor');
for i:=0 to 11 do begin
if ((i mod 16)=0)then begin
WriteLn(Str);
Str:='';
end;
Str:=Str+IntToHex(EncryptKeysServer[i],6)+' ';
end;
WriteLn(Str);
Str:='';
WriteLn('Llave de Desencriptacion en el Servidor');
for i:=0 to 11 do begin
if ((i mod 16)=0)then begin
WriteLn(Str);
Str:='';
end;
Str:=Str+IntToHex(DecryptKeysServer[i],6)+' ';
end;
WriteLn(Str);
len:=DesencriptarC3(PByteArray(A),0,TestLen,true);
Str:='';
WriteLn('Mensaje Desencriptado');
for i:=0 to TestLen-1 do begin
if ((i mod 16)=0)then begin
WriteLn(Str);
Str:='';
end;
Str:=Str+IntToHex(ResultadoDesencriptado[i],2)+' ';
end;
WriteLn(Str);
len:=EncriptarC3(Addr(ResultadoDesencriptado[0]),0,len,true);
Str:='';
WriteLn('Mensaje Encriptado');
for i:=0 to len-1 do begin
if ((i mod 16)=0)then begin
WriteLn(Str);
Str:='';
end;
Str:=Str+IntToHex(ResultadoEncriptado[i],2)+' ';
end;
WriteLn(Str);
result:=0;
end;
{---------------------------------------------------------------------
Teorema de Bezout: si c=mcd(a,b) entonces existen enteros
alfa y beta tales que c= alfa*a + beta*b
----------------------------------------------------------------------}
function coef1(a,b:integer):integer;
begin
if (a mod b=0) then result:=0
else result:=coef2(b,a mod b);
end;
function coef2(a,b:integer):integer;
begin
if (a mod b=0) then result:=1
else result:=coef1(b,a mod b)-a div b*coef2(b,a mod b);
end;
{----------------------------------------------------------------------
Calculo del inverso modulo un entero.
Suponiendo que 1=mcd(m,n) entonces existen enteros a y b tales que
1=m.a+n.b y por tanto a es un inverso de a modulo n
Devuelve el inverso de m modulo n o 0 si no es inversible.
----------------------------------------------------------------------}
function invmod(m,n:integer):integer;
var a,b:integer;
begin
a:=coef1(m,n);
b:=coef2(m,n);
if(m*a+n*b <> 1)then begin
result:=0;
exit;
end;
while(a<0)do Inc(a,n);
result:=a;
end;
procedure LoadKeys(ARchivo:String;var Where:Array of LongWord);
var
Buff:Array[0..15]of Byte;
hFile:Cardinal;
BytesRead:LongWord;
i:integer;
begin
hFile:=CreateFile(PAnsiChar(ARchivo),GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
SetFilePointer(hFile,6,0,FILE_BEGIN);
ReadFile(hFile,Buff,16,bytesRead,0);
for i:=0 to 3 do Where[i]:=((KeyByteC3C4[4*i+3]shl 24)+(KeyByteC3C4[4*i+2]shl 16)+(KeyByteC3C4[4*i+1]shl 8)+KeyByteC3C4[4*i]) xor ((Buff[4*i+3]shl 24)+(Buff[4*i+2]shl 16)+(Buff[4*i+1]shl 8)+Buff[4*i]);
ReadFile(hFile,Buff,16,bytesRead,0);
for i:=0 to 3 do Where[i+4]:=((KeyByteC3C4[4*i+3]shl 24)+(KeyByteC3C4[4*i+2]shl 16)+(KeyByteC3C4[4*i+1]shl 8)+KeyByteC3C4[4*i]) xor ((Buff[4*i+3]shl 24)+(Buff[4*i+2]shl 16)+(Buff[4*i+1]shl 8)+Buff[4*i]);
ReadFile(hFile,Buff,16,bytesRead,0);
for i:=0 to 3 do Where[i+8]:=((KeyByteC3C4[4*i+3]shl 24)+(KeyByteC3C4[4*i+2]shl 16)+(KeyByteC3C4[4*i+1]shl 8)+KeyByteC3C4[4*i]) xor ((Buff[4*i+3]shl 24)+(Buff[4*i+2]shl 16)+(Buff[4*i+1]shl 8)+Buff[4*i]);
CloseHandle(hFile);
end;
//DESPLAZA EL BUFFER, USADO PARA ENCRIPTACION OK
procedure ShiftBuffer(Buff:PByteArray;Len:Integer;ShiftLen:integer);
var
i:integer;
begin
if(ShiftLen<>0)then begin
if(ShiftLen>0)then begin
if(Len>1)then begin
i:=Len-1;
while(i>0)do begin
Buff[i]:=(Buff[i-1]shl(8-ShiftLen))or(Buff[i]shr(ShiftLen));
i:=i-1;
end;
end;
Buff[0]:=Buff[0]shr ShiftLen;
exit;
end;
ShiftLen:=-ShiftLen;
if(Len>1)then
for i:=0 to Len-2 do Buff[i]:=(Buff[i+1]shr(8-ShiftLen))or(Buff[i]shl ShiftLen);
Buff[Len-1]:=Buff[Len-1]shl ShiftLen;
end;
end;
//ENCRIPTACION DEL BUFFER
function HashBuffer(Dest:PByteArray;Param10:integer;Src:PByteArray;Param18:integer;Param1c:integer):Integer;
var
BuffLen:integer;
EAX,ESI,EDI,CycleCount,i:integer;
Temp:TByteArray;
begin
BuffLen:=((Param1c+Param18-1)shr 3)-(Param18 shr 3)+2;
Temp[BuffLen-1]:=0;
for i:=0 to BuffLen-2 do Temp[i]:=Src[(Param18 shr 3)+i];
EAX:=(Param1c+Param18)and $80000007;
if(EAX<0)then EAX:=((EAX-1)or-8)+1;
if(EAX<>0)then Temp[BuffLen-2]:=Temp[BuffLen-2]and(($FF)shl LOBYTE(8-EAX));
ESI:=Param18 and $80000007;
if(ESI<0)then ESI:=((ESI-1)or-8)+1;
EDI:=Param10 and $80000007;
if(EDI<0)then EDI:=((EDI-1)or-8)+1;
ShiftBuffer(Addr(Temp),BuffLen-1,-ESI);
ShiftBuffer(Addr(Temp),BuffLen,EDI);
TempPtr:=Addr(Dest[(Param10 shr 3)]);
CycleCount:=BuffLen-1;
if(EDI>ESI)then
Inc(CycleCount);
if(CycleCount<>0)then begin
for i:=0 to CycleCount-1 do begin
TempPtr[i]:=TempPtr[i] or Temp[i];
end;
end;
result:=Param10+Param1c;
end;
function DecC3Bytes(Dest:PByteArray;Src:PByteArray;DecryptKeys:Array of LongWord):integer;
var
i,j:integer;
TempDec:Array[0..3] of LongWord;
Temp,Temp1:LongWord;
XorByte:Byte;
PTempDec:PByteArray;
begin
for i:=0 to 15 do Dest[i]:=0;
j:=0;
for i:=0 to 3 do TempDec[i]:=0;
PTempDec:=Addr(TempDec[0]);
for i:=0 to 3 do begin
HashBuffer(Addr(PTempDec[4*i]),0,Src,j,16);
Inc(j,16);
HashBuffer(Addr(PTempDec[4*i]),22,Src,j,2);
Inc(j,2);
end;
for i:=0 to 2 do TempDec[2-i]:=TempDec[2-i] xor DecryptKeys[8+2-i] xor(TempDec[2-i+1]and $FFFF);
Temp:=0;
for i:=0 to 3 do begin
Temp1:=((DecryptKeys[4+i]*(TempDec[i]))mod(DecryptKeys[i]))xor DecryptKeys[i+8] xor Temp;
Temp:=TempDec[i]and $FFFF;
Dest[2*i]:=LoWord(Temp1) and $FF;
Dest[2*i+1]:=LoWord(Temp1) shr 8;
end;
TempDec[0]:=0;
PTempDec:=Addr(TempDec[0]);
HashBuffer(PTempDec,0,Src,j,16);
PTempDec:=Addr(TempDec[0]);
PTempDec[0]:=PTempDec[1] xor PTempDec[0] xor $3D;
XorByte:=$F8;
for i:=0 to 7 do XorByte:=XorByte xor Dest[i];
if(XorByte<>PTempDec[1])then begin
result:=-1;
//WriteLn('Error al Desencriptar DecC3Bytes');
end else result:=PTempDec[0];
end;
function DecC3Buffer(Dest:PByteArray;Src:PByteArray;Len:Integer;DecryptKeys:Array of LongWord):integer;
var
DecLen:integer;
i:integer;
begin
result:=0;
if(Dest=nil) then exit;
DecLen:=0;
if(Len>0)then begin
i:=0;
while(DecLen<Len)do begin
if(DecC3Bytes(Addr(Dest[8*i]),Addr(Src[11*i]),DecryptKeys)<0)then begin
//exit;
end;
Inc(DecLen,11);
Inc(i);
result:=DecLen;
end;
end;
end;
procedure EncC3Bytes(Dest:PByteArray;Src:PByteArray;const Len:integer;EncryptKeys:Array of LongWord);
var
i,j:integer;
Temp,Temp2:LongWord;
inverse:integer;
begin
Temp:=0;
for i:=0 to 3 do begin
Temp2:=Src[2*i+1]shl 8 or Src[2*i];
inverse:=invmod(EncryptKeys[i+4],EncryptKeys[i])mod EncryptKeys[i];
TempEnc[i]:=((EncryptKeys[i+8]xor Temp2 xor Temp)*inverse)mod EncryptKeys[i];
Temp:=TempEnc[i]and$FFFF;
end;
Temp:=TempEnc[3]and$FFFF;
for i:=0 to 2 do begin
Temp2:=Temp;
Temp:=TempEnc[2-i]and$FFFF;
TempEnc[2-i]:=TempEnc[2-i]xor EncryptKeys[8+2-i]xor Temp2;
end;
j:=0;
for i:=0 to 10 do Dest[i]:=0;
TempByte:=Addr(TempEnc[0]);
for i:=0 to 3 do begin
j:=HashBuffer(Dest,j,Addr(TempByte[4*i]),0,16);
j:=HashBuffer(Dest,j,Addr(TempByte[4*i]),22,2);
end;
XorByte:=$F8;
for i:=0 to 7 do XorByte:=XorByte xor Src[i];
TempByte:=Addr(TempEnc[0]);
TempByte[1]:=XorByte;
TempByte[0]:=XorByte xor Len xor $3D;
HashBuffer(Dest,j,TempByte,0,16);
end;
function EncC3Buffer(Dest:PByteArray;Src:PByteArray;const Len:integer;EncryptKeys:Array of LongWord):integer;
var
i:integer;
begin
result:=0;
if(Dest=nil)then exit;
EncLen:=Len;
if(Len>0)then begin
i:=0;
While(EncLen>0)do begin
if(EncLen>7)then
EncC3Bytes(Addr(Dest[11*i]),Addr(Src[8*i]),8,EncryptKeys)
else
EncC3Bytes(Addr(Dest[11*i]),Addr(Src[8*i]),EncLen,EncryptKeys);
Dec(EncLen,8);
Inc(i);
end;
result:=Len;
end;
end;
procedure DesencriptarC1(Coded:PByteArray;inicio,size:Integer);
var
i:integer;
begin
for i:=0 to size-1 do begin
//Se copian los primeros 3 bytes
if(i<=2)then resultadoDesencriptado[i]:=coded[i+inicio]
else resultadoDesencriptado[i]:=coded[i+inicio] xor ( coded[i-1+inicio] xor keybyteC1C2[i mod 32] );
end;
end;
function DesencriptarC3(Coded:PByteArray;inicio,size:Integer;ToServer:Boolean):integer;
var
DecodedLen:Integer;
i:integer;
begin
resultadoDesencriptado[0]:=$C3;
resultadoDesencriptado[1]:=size;
if(ToServer)then begin
for i:=0 to size-1 do begin
ResultadoDesencriptado[i]:=0;
ResultadoEncriptado[i]:=0;
end;
DecodedLen:=DecC3Buffer(Addr(resultadoDesencriptado[2]),Addr(Coded[2+inicio]),Size-2,DecryptKeysServer);
Inc(DecodedLen,2);
for i:=0 to DecodedLen-1 do ResultadoEncriptado[i]:=ResultadoDesencriptado[i];
DesencriptarC1(Addr(resultadoencriptado[0]),0,DecodedLen);
end else begin
DecC3Buffer(Addr(resultadoDesencriptado[2]),Addr(Coded[2+inicio]),Size-2,EncryptKeysServer);
end;
result:=size;
ResultadoDesencriptado[0]:=$C3;
ResultadoDesencriptado[1]:=size;
end;
procedure DesencriptarC2(Coded:PByteArray;inicio,size:Integer);
var
i:integer;
begin
for i:=0 to size-1 do begin
//Se copian los primeros 3 bytes
if(i<=3)then resultadoDesencriptado[i]:=coded[i+inicio]
else resultadoDesencriptado[i]:=coded[i+inicio] xor ( coded[i-1+inicio] xor keybyteC1C2[i mod 32] );
end;
end;
procedure EncriptarC1(Data:PByteArray;inicio,size:Integer);
var
i:integer;
begin
for i:=0 to size-1 do begin
//Se copian los primeros 3 bytes
if(i<=2)then resultadoencriptado[i]:=Data[i+inicio]
else resultadoencriptado[i]:=Data[i+inicio] xor ( resultadoencriptado[i-1+inicio] xor keybyteC1C2[i mod 32] );
end;
end;
//SE ENCRIPTA 2 VECES
function EncriptarC3(Data:PByteArray;inicio,size:Integer;ToServer:Boolean):integer;
var
Temp:TByteArray;
EncodeLen:Integer;
i:integer;
begin
Temp[0]:=$C3;
if(ToServer)then begin
EncriptarC1(Data,inicio,size);
for i:=0 to size-1 do Temp[i]:=resultadoEncriptado[i];
EncodeLen:=EncC3Buffer(Addr(resultadoEncriptado[2]),Addr(Temp[2]),Size-2,DecryptKeysServer);
Inc(EncodeLen,2);
end else begin
EncodeLen:=EncC3Buffer(Addr(resultadoEncriptado[2]),Addr(Data[2+inicio]),Size-2,EncryptKeysServer);
Inc(EncodeLen,2);
end;
result:=EncodeLen;
ResultadoEncriptado[0]:=$C3;
ResultadoEncriptado[1]:=EncodeLen;
end;
procedure EncriptarC2(Data:PByteArray;inicio,size:Integer);
var
i:integer;
begin
for i:=0 to size-1 do begin
//Se copian los primeros 3 bytes
if(i<=3)then resultadoencriptado[i]:=Data[i+inicio]
else resultadoencriptado[i]:=Data[i+inicio] xor ( resultadoencriptado[i-1+inicio] xor keybyteC1C2[i mod 32] );
end;
end;
end.
i duuno who made this so credits gous to google =D
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
I was declaring my desinationt buffer as:
Code:
unsigned char enc_pkt[8192];
I hope the correct way should be:
Code:
unsigned char enc_pkt[MuPacketEncSpace(dec_pkt)];
Right?
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
Quote:
Originally Posted by
thiforums
I was declaring my desinationt buffer as:
Code:
unsigned char enc_pkt[8192];
I hope the correct way should be:
Code:
unsigned char enc_pkt[MuPacketEncSpace(dec_pkt)];
Right?
Most packets doesn't use more than 100bytes, so you have about 8kbytes of unused memory.
In other hand, don't know if that packet could exist ;), but theoretically a C4 packet could take up to 65535 bytes (0xFFFF). How do you decrypt it if you only have 8192 bytes?
Your code is wrong, it won't compile, coz [MuPacketEncSpace(dec_pkt) isn't a constant. So you must create a pointer and reserve memory dynamically. Use this form:
Code:
unsigned char* enc_pkt = new unsigned char[MuPacketEncSpace(dec_pkt)];
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
i see sircid u moved from vb to c++ =D
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
Quote:
Originally Posted by
mauka
i see sircid u moved from vb to c++ =D
yep =)! now I have realized that VB is a shit >.<!
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
Quote:
Originally Posted by
mauka
here is delphi version
Code:
unit MUOnlineEncrypt;
interface
uses windows,
sysutils,
classes;
var
//Encryptacion
resultadoDesencriptado:TByteArray; //Desencryptado
resultadoEncriptado:TByteArray; //Encryptado
DecryptKeysServer:Array[0..11]of LongWord; //Llave para Desencriptacion en el Servidor y Encriptacion en el Cliente
EncryptKeysServer:Array[0..11]of LongWord; //Llave para Encriptacion en el Servidor y Desencriptacion en el Cliente
EncLen:Integer;
TempEnc:Array[0..3]of LongWord;
TempByte:PByteArray;
TempWord:Array[0..3]of Word;
XorByte:Byte;
TempDest,TempSrc:PByteArray;
TempPtr:PByteArray;
//PROCEDIMIENTOS PARA MUONLINE
procedure LoadKeys(ARchivo:String;var Where:Array of LongWord); //Carga las llaves desde Los archvios Enc1.dat y Dec2.dat
procedure DesencriptarC1(Coded:PByteArray;inicio,size:Integer);
procedure DesencriptarC2(Coded:PByteArray;inicio,size:Integer);
function DesencriptarC3(Coded:PByteArray;inicio,size:Integer;ToServer:Boolean):Integer;
procedure EncriptarC1(Data:PByteArray;inicio,size:Integer);
procedure EncriptarC2(Data:PByteArray;inicio,size:Integer);
function EncriptarC3(Data:PByteArray;inicio,size:Integer;ToServer:Boolean):Integer;
function coef2(a,b:integer):integer;
function coef1(a,b:integer):integer;
//EXAMPLES
const TestLen=$18;
const Temp6: array[0..TestLen-1] of Byte =
// ($C3,$0D,$06,$8A,$36,$37,$0B,$7C,$76,$6D,$35,$05,$30); //ServerToClient
// ($C3,$18,$70,$27,$2D,$95,$0A,$F9,$35,$E1,$08,$BD,$88,$5D,$FC,$2C,$57,$5C,$F5,$A1,$E0,$D1,$42,$77); //ServerToClient
// ($C3,$18,$D2,$39,$20,$BF,$91,$F2,$13,$3C,$C1,$A0,$95,$5D,$FC,$2C,$57,$5C,$F5,$A1,$E0,$D1,$4B,$77); //ServerToClient MOVE CDA+9+S+L (46CCE5) TO HAND (00)
($C3,$18,$8B,$90,$78,$F0,$13,$6A,$50,$31,$2D,$8C,$B9,
$3C,$82,$5D,$25,$D9,$05,$C3,$8B,$24,$57,$69); //ClientToServer NOT WORKING YET!! T_T!!
// ($C3,$0D,$3C,$82,$5D,$25,$D9,$05,$C3,$8B,$24,$57,$69); //ClientToServer NOT WORKING YET!! T_T!!
// ($C3,$18,$8B,$90,$78,$F0,$13,$6A,$50,$31,$2D,$0C,$39,
// $B4,$F0,$04,$94,$56,$07,$30,$A1,$D8,$7B,$4E); //ClientToServer NOT WORKING YET!! T_T!!
const KeyByteC1C2: array[0..31] of Byte =(231, **** 58, 137, 188, 178, 159, ****
35, 168, 254, 182, 73, 93, 57, 93,
138, 203, *** 141, 234, 125, 43, 95,
195, 177, 233, 131, 41, 81, 2*** 86);
const KeyByteC3C4: array[0..15] of Byte =($9B,$A7,$08,$3F,$87,$C2,$5C,$E2,
$B9,$7A,$D2,$93,$BF,$A7,$DE,$20);
//FsGate 1
//7D299AF0C73940FFCCAA1CE39E332A1C
//FsGate 2
//98 25 87 94 3F DE C2 1B B3 A2 0E F8 5B 92 1A A1
//GameServer
Const GameServerKey: Array[0..28] of Byte=($76,$FB,$E3,$9B,$5D,$D8,$B3,$3A,$25,$54,$95,$D0,$9C,$D6,$11,$95,
$A8,$61,$6D,$0B,$B9,$25,$6B,$0B,$74,$B1,$6B,$0B,$F8);
function Test():integer;
implementation
function Test():integer;
var
i:integer;
len:integer;
Str:String;
A,B:String;
P:PByte;
Pc,Pd:PByte;
begin
LoadKeys('Enc1.dat',DecryptKeysServer);
LoadKeys('Dec2.dat',EncryptKeysServer);
A:='';
SetLength(B,TestLen*2);
for i:=0 to TestLen-1 do begin
A:=A+chr(Temp6[i]);
end;
for i:=0 to TestLen*2-1 do begin
B[i+1]:=chr(0);
end;
Str:='';
WriteLn('Mensaje Original');
for i:=0 to TestLen-1 do begin
if ((i mod 16)=0)then begin
WriteLn(Str);
Str:='';
end;
Str:=Str+IntToHex(Temp6[i],2)+' ';
end;
WriteLn(Str);
Str:='';
WriteLn('Llave de Encriptacion en el Servidor');
for i:=0 to 11 do begin
if ((i mod 16)=0)then begin
WriteLn(Str);
Str:='';
end;
Str:=Str+IntToHex(EncryptKeysServer[i],6)+' ';
end;
WriteLn(Str);
Str:='';
WriteLn('Llave de Desencriptacion en el Servidor');
for i:=0 to 11 do begin
if ((i mod 16)=0)then begin
WriteLn(Str);
Str:='';
end;
Str:=Str+IntToHex(DecryptKeysServer[i],6)+' ';
end;
WriteLn(Str);
len:=DesencriptarC3(PByteArray(A),0,TestLen,true);
Str:='';
WriteLn('Mensaje Desencriptado');
for i:=0 to TestLen-1 do begin
if ((i mod 16)=0)then begin
WriteLn(Str);
Str:='';
end;
Str:=Str+IntToHex(ResultadoDesencriptado[i],2)+' ';
end;
WriteLn(Str);
len:=EncriptarC3(Addr(ResultadoDesencriptado[0]),0,len,true);
Str:='';
WriteLn('Mensaje Encriptado');
for i:=0 to len-1 do begin
if ((i mod 16)=0)then begin
WriteLn(Str);
Str:='';
end;
Str:=Str+IntToHex(ResultadoEncriptado[i],2)+' ';
end;
WriteLn(Str);
result:=0;
end;
{---------------------------------------------------------------------
Teorema de Bezout: si c=mcd(a,b) entonces existen enteros
alfa y beta tales que c= alfa*a + beta*b
----------------------------------------------------------------------}
function coef1(a,b:integer):integer;
begin
if (a mod b=0) then result:=0
else result:=coef2(b,a mod b);
end;
function coef2(a,b:integer):integer;
begin
if (a mod b=0) then result:=1
else result:=coef1(b,a mod b)-a div b*coef2(b,a mod b);
end;
{----------------------------------------------------------------------
Calculo del inverso modulo un entero.
Suponiendo que 1=mcd(m,n) entonces existen enteros a y b tales que
1=m.a+n.b y por tanto a es un inverso de a modulo n
Devuelve el inverso de m modulo n o 0 si no es inversible.
----------------------------------------------------------------------}
function invmod(m,n:integer):integer;
var a,b:integer;
begin
a:=coef1(m,n);
b:=coef2(m,n);
if(m*a+n*b <> 1)then begin
result:=0;
exit;
end;
while(a<0)do Inc(a,n);
result:=a;
end;
procedure LoadKeys(ARchivo:String;var Where:Array of LongWord);
var
Buff:Array[0..15]of Byte;
hFile:Cardinal;
BytesRead:LongWord;
i:integer;
begin
hFile:=CreateFile(PAnsiChar(ARchivo),GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
SetFilePointer(hFile,6,0,FILE_BEGIN);
ReadFile(hFile,Buff,16,bytesRead,0);
for i:=0 to 3 do Where[i]:=((KeyByteC3C4[4*i+3]shl 24)+(KeyByteC3C4[4*i+2]shl 16)+(KeyByteC3C4[4*i+1]shl 8)+KeyByteC3C4[4*i]) xor ((Buff[4*i+3]shl 24)+(Buff[4*i+2]shl 16)+(Buff[4*i+1]shl 8)+Buff[4*i]);
ReadFile(hFile,Buff,16,bytesRead,0);
for i:=0 to 3 do Where[i+4]:=((KeyByteC3C4[4*i+3]shl 24)+(KeyByteC3C4[4*i+2]shl 16)+(KeyByteC3C4[4*i+1]shl 8)+KeyByteC3C4[4*i]) xor ((Buff[4*i+3]shl 24)+(Buff[4*i+2]shl 16)+(Buff[4*i+1]shl 8)+Buff[4*i]);
ReadFile(hFile,Buff,16,bytesRead,0);
for i:=0 to 3 do Where[i+8]:=((KeyByteC3C4[4*i+3]shl 24)+(KeyByteC3C4[4*i+2]shl 16)+(KeyByteC3C4[4*i+1]shl 8)+KeyByteC3C4[4*i]) xor ((Buff[4*i+3]shl 24)+(Buff[4*i+2]shl 16)+(Buff[4*i+1]shl 8)+Buff[4*i]);
CloseHandle(hFile);
end;
//DESPLAZA EL BUFFER, USADO PARA ENCRIPTACION OK
procedure ShiftBuffer(Buff:PByteArray;Len:Integer;ShiftLen:integer);
var
i:integer;
begin
if(ShiftLen<>0)then begin
if(ShiftLen>0)then begin
if(Len>1)then begin
i:=Len-1;
while(i>0)do begin
Buff[i]:=(Buff[i-1]shl(8-ShiftLen))or(Buff[i]shr(ShiftLen));
i:=i-1;
end;
end;
Buff[0]:=Buff[0]shr ShiftLen;
exit;
end;
ShiftLen:=-ShiftLen;
if(Len>1)then
for i:=0 to Len-2 do Buff[i]:=(Buff[i+1]shr(8-ShiftLen))or(Buff[i]shl ShiftLen);
Buff[Len-1]:=Buff[Len-1]shl ShiftLen;
end;
end;
//ENCRIPTACION DEL BUFFER
function HashBuffer(Dest:PByteArray;Param10:integer;Src:PByteArray;Param18:integer;Param1c:integer):Integer;
var
BuffLen:integer;
EAX,ESI,EDI,CycleCount,i:integer;
Temp:TByteArray;
begin
BuffLen:=((Param1c+Param18-1)shr 3)-(Param18 shr 3)+2;
Temp[BuffLen-1]:=0;
for i:=0 to BuffLen-2 do Temp[i]:=Src[(Param18 shr 3)+i];
EAX:=(Param1c+Param18)and $80000007;
if(EAX<0)then EAX:=((EAX-1)or-8)+1;
if(EAX<>0)then Temp[BuffLen-2]:=Temp[BuffLen-2]and(($FF)shl LOBYTE(8-EAX));
ESI:=Param18 and $80000007;
if(ESI<0)then ESI:=((ESI-1)or-8)+1;
EDI:=Param10 and $80000007;
if(EDI<0)then EDI:=((EDI-1)or-8)+1;
ShiftBuffer(Addr(Temp),BuffLen-1,-ESI);
ShiftBuffer(Addr(Temp),BuffLen,EDI);
TempPtr:=Addr(Dest[(Param10 shr 3)]);
CycleCount:=BuffLen-1;
if(EDI>ESI)then
Inc(CycleCount);
if(CycleCount<>0)then begin
for i:=0 to CycleCount-1 do begin
TempPtr[i]:=TempPtr[i] or Temp[i];
end;
end;
result:=Param10+Param1c;
end;
function DecC3Bytes(Dest:PByteArray;Src:PByteArray;DecryptKeys:Array of LongWord):integer;
var
i,j:integer;
TempDec:Array[0..3] of LongWord;
Temp,Temp1:LongWord;
XorByte:Byte;
PTempDec:PByteArray;
begin
for i:=0 to 15 do Dest[i]:=0;
j:=0;
for i:=0 to 3 do TempDec[i]:=0;
PTempDec:=Addr(TempDec[0]);
for i:=0 to 3 do begin
HashBuffer(Addr(PTempDec[4*i]),0,Src,j,16);
Inc(j,16);
HashBuffer(Addr(PTempDec[4*i]),22,Src,j,2);
Inc(j,2);
end;
for i:=0 to 2 do TempDec[2-i]:=TempDec[2-i] xor DecryptKeys[8+2-i] xor(TempDec[2-i+1]and $FFFF);
Temp:=0;
for i:=0 to 3 do begin
Temp1:=((DecryptKeys[4+i]*(TempDec[i]))mod(DecryptKeys[i]))xor DecryptKeys[i+8] xor Temp;
Temp:=TempDec[i]and $FFFF;
Dest[2*i]:=LoWord(Temp1) and $FF;
Dest[2*i+1]:=LoWord(Temp1) shr 8;
end;
TempDec[0]:=0;
PTempDec:=Addr(TempDec[0]);
HashBuffer(PTempDec,0,Src,j,16);
PTempDec:=Addr(TempDec[0]);
PTempDec[0]:=PTempDec[1] xor PTempDec[0] xor $3D;
XorByte:=$F8;
for i:=0 to 7 do XorByte:=XorByte xor Dest[i];
if(XorByte<>PTempDec[1])then begin
result:=-1;
//WriteLn('Error al Desencriptar DecC3Bytes');
end else result:=PTempDec[0];
end;
function DecC3Buffer(Dest:PByteArray;Src:PByteArray;Len:Integer;DecryptKeys:Array of LongWord):integer;
var
DecLen:integer;
i:integer;
begin
result:=0;
if(Dest=nil) then exit;
DecLen:=0;
if(Len>0)then begin
i:=0;
while(DecLen<Len)do begin
if(DecC3Bytes(Addr(Dest[8*i]),Addr(Src[11*i]),DecryptKeys)<0)then begin
//exit;
end;
Inc(DecLen,11);
Inc(i);
result:=DecLen;
end;
end;
end;
procedure EncC3Bytes(Dest:PByteArray;Src:PByteArray;const Len:integer;EncryptKeys:Array of LongWord);
var
i,j:integer;
Temp,Temp2:LongWord;
inverse:integer;
begin
Temp:=0;
for i:=0 to 3 do begin
Temp2:=Src[2*i+1]shl 8 or Src[2*i];
inverse:=invmod(EncryptKeys[i+4],EncryptKeys[i])mod EncryptKeys[i];
TempEnc[i]:=((EncryptKeys[i+8]xor Temp2 xor Temp)*inverse)mod EncryptKeys[i];
Temp:=TempEnc[i]and$FFFF;
end;
Temp:=TempEnc[3]and$FFFF;
for i:=0 to 2 do begin
Temp2:=Temp;
Temp:=TempEnc[2-i]and$FFFF;
TempEnc[2-i]:=TempEnc[2-i]xor EncryptKeys[8+2-i]xor Temp2;
end;
j:=0;
for i:=0 to 10 do Dest[i]:=0;
TempByte:=Addr(TempEnc[0]);
for i:=0 to 3 do begin
j:=HashBuffer(Dest,j,Addr(TempByte[4*i]),0,16);
j:=HashBuffer(Dest,j,Addr(TempByte[4*i]),22,2);
end;
XorByte:=$F8;
for i:=0 to 7 do XorByte:=XorByte xor Src[i];
TempByte:=Addr(TempEnc[0]);
TempByte[1]:=XorByte;
TempByte[0]:=XorByte xor Len xor $3D;
HashBuffer(Dest,j,TempByte,0,16);
end;
function EncC3Buffer(Dest:PByteArray;Src:PByteArray;const Len:integer;EncryptKeys:Array of LongWord):integer;
var
i:integer;
begin
result:=0;
if(Dest=nil)then exit;
EncLen:=Len;
if(Len>0)then begin
i:=0;
While(EncLen>0)do begin
if(EncLen>7)then
EncC3Bytes(Addr(Dest[11*i]),Addr(Src[8*i]),8,EncryptKeys)
else
EncC3Bytes(Addr(Dest[11*i]),Addr(Src[8*i]),EncLen,EncryptKeys);
Dec(EncLen,8);
Inc(i);
end;
result:=Len;
end;
end;
procedure DesencriptarC1(Coded:PByteArray;inicio,size:Integer);
var
i:integer;
begin
for i:=0 to size-1 do begin
//Se copian los primeros 3 bytes
if(i<=2)then resultadoDesencriptado[i]:=coded[i+inicio]
else resultadoDesencriptado[i]:=coded[i+inicio] xor ( coded[i-1+inicio] xor keybyteC1C2[i mod 32] );
end;
end;
function DesencriptarC3(Coded:PByteArray;inicio,size:Integer;ToServer:Boolean):integer;
var
DecodedLen:Integer;
i:integer;
begin
resultadoDesencriptado[0]:=$C3;
resultadoDesencriptado[1]:=size;
if(ToServer)then begin
for i:=0 to size-1 do begin
ResultadoDesencriptado[i]:=0;
ResultadoEncriptado[i]:=0;
end;
DecodedLen:=DecC3Buffer(Addr(resultadoDesencriptado[2]),Addr(Coded[2+inicio]),Size-2,DecryptKeysServer);
Inc(DecodedLen,2);
for i:=0 to DecodedLen-1 do ResultadoEncriptado[i]:=ResultadoDesencriptado[i];
DesencriptarC1(Addr(resultadoencriptado[0]),0,DecodedLen);
end else begin
DecC3Buffer(Addr(resultadoDesencriptado[2]),Addr(Coded[2+inicio]),Size-2,EncryptKeysServer);
end;
result:=size;
ResultadoDesencriptado[0]:=$C3;
ResultadoDesencriptado[1]:=size;
end;
procedure DesencriptarC2(Coded:PByteArray;inicio,size:Integer);
var
i:integer;
begin
for i:=0 to size-1 do begin
//Se copian los primeros 3 bytes
if(i<=3)then resultadoDesencriptado[i]:=coded[i+inicio]
else resultadoDesencriptado[i]:=coded[i+inicio] xor ( coded[i-1+inicio] xor keybyteC1C2[i mod 32] );
end;
end;
procedure EncriptarC1(Data:PByteArray;inicio,size:Integer);
var
i:integer;
begin
for i:=0 to size-1 do begin
//Se copian los primeros 3 bytes
if(i<=2)then resultadoencriptado[i]:=Data[i+inicio]
else resultadoencriptado[i]:=Data[i+inicio] xor ( resultadoencriptado[i-1+inicio] xor keybyteC1C2[i mod 32] );
end;
end;
//SE ENCRIPTA 2 VECES
function EncriptarC3(Data:PByteArray;inicio,size:Integer;ToServer:Boolean):integer;
var
Temp:TByteArray;
EncodeLen:Integer;
i:integer;
begin
Temp[0]:=$C3;
if(ToServer)then begin
EncriptarC1(Data,inicio,size);
for i:=0 to size-1 do Temp[i]:=resultadoEncriptado[i];
EncodeLen:=EncC3Buffer(Addr(resultadoEncriptado[2]),Addr(Temp[2]),Size-2,DecryptKeysServer);
Inc(EncodeLen,2);
end else begin
EncodeLen:=EncC3Buffer(Addr(resultadoEncriptado[2]),Addr(Data[2+inicio]),Size-2,EncryptKeysServer);
Inc(EncodeLen,2);
end;
result:=EncodeLen;
ResultadoEncriptado[0]:=$C3;
ResultadoEncriptado[1]:=EncodeLen;
end;
procedure EncriptarC2(Data:PByteArray;inicio,size:Integer);
var
i:integer;
begin
for i:=0 to size-1 do begin
//Se copian los primeros 3 bytes
if(i<=3)then resultadoencriptado[i]:=Data[i+inicio]
else resultadoencriptado[i]:=Data[i+inicio] xor ( resultadoencriptado[i-1+inicio] xor keybyteC1C2[i mod 32] );
end;
end;
end.
i duuno who made this so credits gous to google =D
does this code works? can som1 aprove it..
i try to decrypt c3 then encrypt it back and its rtrn wrong...
:grr:
c1&c2 works fine.. but c3 nope.. meybe i do somthing wrong
PS.
seems even test code retourn encrypted c3 wrong:grr::thumbdown:
Edited:
nwm! i got fully working enc/dec c1/c2/c3/c4 delphi unit
source posted above does not work..
Re: [Release] C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
Quote:
Originally Posted by
sircid
yep =)! now I have realized that VB is a shit >.<!
good for you:)
and dont forget foxes the client count c3/c4 packages and use it as 1'st byte of new one included within ecnryption ...
Re: C1/C2/C3/C4 Packet Encryption/Decryption source code (C++)
Is that so called old or new encryption ?