unit KeyLoader;
interface
uses
Classes, SysUtils;
type
TXTEA_KEY = array [0..15] of Byte; // Klucz XTEA ma 16 bajtow
TLOGIN_PACKET = array [0..136]of Byte; // Login packet ma 137 bajtow
TKEY_FILE = record
x_key: TXTEA_KEY;
login_packet: TLOGIN_PACKET;
server_ip: string[255]; // Adres IP Servera
end;
{Funkcje Czytajace Klucze z Pliku}
function LoadKeys(const Path: string): TKEY_FILE; // glowna funkcja do czytania
{Funkcje Konwertujace}
function DeleteSpaces(raw_str: string): string; //Usuwa spacje (jesli sa) ze string-a
function RawToLogin(raw_key: string): TLOGIN_PACKET; // konwersja string-a na tablice bajtow (na login packet)
function RawToXTea(raw_key: string): TXTEA_KEY; // jak wyzej, tylko ze na klucz xtea (16 bajtowa tablica)
implementation
function DeleteSpaces(raw_str: string): string;
var
i: Integer;
begin
Result := '';
for i:=1 to Length(raw_str) do
if (raw_str[i] <> ' ') then
Result := Result + raw_str[i];
end;
function RawToLogin(raw_key: string): TLOGIN_PACKET;
var
i,j: integer;
temp_byte: PChar;
ByteBuffer: PChar;
begin
j:=-1;
for i:=1 to Round(Length(raw_key)/2) do
begin
j:=j+2;
temp_byte := PChar(string(raw_key[j])+ string(raw_key[j+1]));
HexToBin(temp_byte,@ByteBuffer,SizeOf(temp_byte)-1);
Result[i-1] := Byte(ByteBuffer);
end;
end;
function RawToXTea(raw_key: string): TXTEA_KEY;
var
i,j: integer;
temp_byte: PChar;
ByteBuffer: PChar;
begin
j:=-1;
for i:=1 to Round(Length(raw_key)/2) do
begin
j:=j+2;
temp_byte := PChar(string(raw_key[j])+ string(raw_key[j+1]));
HexToBin(temp_byte,@ByteBuffer,SizeOf(temp_byte)-1);
Result[i-1] := Byte(ByteBuffer);
end;
end;
function LoadKeys(const Path: string): TKEY_FILE;
var
KeyFile: TextFile;
login_str, xtea_str, ip: string;
begin
Assign(KeyFile, Path);
Reset(KeyFile);
Readln(KeyFile, login_str);
Readln(KeyFile, xtea_str);
Readln(KeyFile, ip);
Close(KeyFile);
login_str := DeleteSpaces(login_str);
xtea_str := DeleteSpaces(xtea_str);
Result.x_key := RawToXTea(xtea_str);
Result.login_packet := RawToLogin(login_str);
Result.server_ip := ip;
end;
end.