xTea

Announcements and discussion regarding any projects related to Cyan Worlds' Plasma Engine including (but not limited to) CyanWorlds.com Engine, Drizzle, OfflineKI, PyPRP, and libHSPlasma.

xTea

Postby Stucuk » Wed Jul 14, 2010 5:42 pm

Iv been trying to convert libHSPlasma's xTea Decryption procedure from C++ into Delphi and its not working. Code looks identical. Im trying to decrypt a URU Live age(As far as i understand they still use xTea). Anyone see anything wrong?

libhsplasma:
Code: Select all
void plEncryptedStream::TeaDecipher(unsigned int* buf) {
    unsigned int second = buf[1], first = buf[0], key = 0xC6EF3720;

    for (int i=0; i<32; i++) {
        second -= (((first >> 5) ^ (first << 4)) + first)
                ^ (fEKey[(key >> 11) & 3] + key);
        key += 0x61C88647;
        first -= (((second >> 5) ^ (second << 4)) + second)
               ^ (fEKey[key & 3] + key);
    }
    buf[0] = first;
    buf[1] = second;
}


Mine:
Code: Select all
procedure xTeaDecrypt(Input : TTeaArray; var Result : TTeaArray);
var
 First,
 Second : Cardinal;
 I      : Integer;
 Key    : Cardinal;
begin
 First  := Input[0];
 Second := Input[1];
 Key    := $C6EF3720;

 for I := 0 to 31 do
 begin
 Second := Second - (
                    (((First Shr 5) Xor (First Shl 4)) + First)
                    Xor (URUKey[(Key Shr 11) and 3] + Key)
                   );
   Inc(Key,$61C88647);
 First := First - (
                   (((Second Shr 5) Xor (Second Shl 4)) + Second)
                   Xor (URUKey[Key and 3] + Key)
                 );
 end;

 Result[0] := First;
 Result[1] := Second;
end;


Whole unit:
Code: Select all
unit xTea;

interface

type
TTeaArray = Array [0..1] of Cardinal;

const
URUKey : Array [0..3] of Cardinal = ($6c0a5452, $03827d0f, $3a170b92, $16db7fc2);

procedure xTeaDecrypt(Input : TTeaArray; var Result : TTeaArray);

function LoadxTeaFile(Filename : String) : String;

implementation

uses Windows, Math, SysUtils;

function TeaArrayToStr(Input : TTeaArray) : String;
var
 Output : Array [0..7] of Char;
begin
 CopyMemory(@Output[0],@Input[0],8);
 Result := Output[0] + Output[1] + Output[2] + Output[3] + Output[4] + Output[5] + Output[6] + Output[7];
end;

procedure xTeaDecrypt(Input : TTeaArray; var Result : TTeaArray);
var
 First,
 Second : Cardinal;
 I      : Integer;
 Key    : Cardinal;
begin
 First  := Input[0];
 Second := Input[1];
 Key    := $C6EF3720;

 for I := 0 to 31 do
 begin
 Second := Second - ((((First Shr 5) Xor (First Shl 4)) + First) Xor (URUKey[(Key Shr 11) and 3] + Key));
   Inc(Key,$61C88647);
 First := First - ((((Second Shr 5) Xor (Second Shl 4)) + Second) Xor (URUKey[Key and 3] + Key));
 end;

 Result[0] := First;
 Result[1] := Second;
end;

function LoadxTeaFile(Filename : String) : String;
var
 H      : Integer;
 Size   : Integer;
 Input,
 Output : TTeaArray;
begin
 Result := '';
 H := FileOpen(Filename,fmOpenRead);
  Size := FileSeek(H,0,2);
  FileSeek(H,12,0);

  if Size > 8 then
  Repeat
   FileRead(H,Input[0],8);
   xTeaDecrypt(Input,Output);
   Result := Result + TeaArrayToStr(Output);
   Dec(Size,8);
  Until Size < 8;

 FileClose(H);
end;
Image
-Stu
Stucuk
 
Posts: 45
Joined: Wed Jul 14, 2010 5:29 pm

Re: xTea

Postby Stucuk » Thu Jul 15, 2010 8:41 am

Found the problem. Needed to start from the 16th byte instead of 12th. Tho i get rubbish at the end of the age, is there ment to be something at the end?.

Code: Select all
StartDateTime=0946713600
DayLength=24.000000
MaxCapacity=10
LingerTime=180
SequencePrefix=38
ReleaseVersion=0
Page=ahnySphere01,1,1
Page=ahnySphere02,2,1
Page=ahnySphere03,3,1
Page=ahnySphere04,4,1
Page=ahnySphereCtrl,31
Page=EngineerHut,11,1
Page=FemaleSwimDockExit,17
Page=FemaleValveWheelCCW,18
Page=FemaleValveWheelCW,19
Page=FemaleVogChairExit,12
Page=FemaleVogChairRide,13
Page=Hub,10,1
Page=MaintRoom01,30,1
Page=MaintRoom02,6,1
Page=MaintRoom03,7,1
Page=MaintRoom04,8,1
Page=MaleSwimDockExit,20
Page=MaleValveWheelCCW,21
Page=MaleValveWheelCW,22
Page=MaleVogChairExit,14
Page=MaleVogChairRide,15
Page=QuabIdle01,24
Page=QuabIdle02,25
Page=QuabIdle03,26
Page=QuabRun01,27
Page=QuabRun02,28
Page=QuabRun03,29
Page=Sphere01BuildingInterior,5,1
Page=Vortex,9,1
Page=YeeshaSketchBahro,16
’Âï¥6
’Âï¥6
’Âï¥


Code: Select all
StartDateTime=0946713600
DayLength=24.000000
MaxCapacity=10
LingerTime=180
SequencePrefix=25
ReleaseVersion=0
Page=YeeshaCave,2
ave,2
ave,2
Image
-Stu
Stucuk
 
Posts: 45
Joined: Wed Jul 14, 2010 5:29 pm

Re: xTea

Postby Paradox » Thu Jul 15, 2010 10:23 pm

XTEA requires that the encrypted data be a certain block size, so there is probably some padding data at the end.
Paradox
 
Posts: 1294
Joined: Fri Sep 28, 2007 6:48 pm
Location: Canada

Re: xTea

Postby ddb174 » Fri Jul 16, 2010 12:42 am

Yeah, just take the filesize which is stored at the start, and only keep that many bytes, and throw out the rest. Drizzle does the same thing.
ddb174
 
Posts: 928
Joined: Thu Apr 10, 2008 7:28 pm


Return to Plasma Development

Who is online

Users browsing this forum: No registered users and 19 guests