Svp aidez moi à traduire ce code de c++ vers delphi

fartotodz2004 Messages postés 7 Date d'inscription jeudi 25 octobre 2007 Statut Membre Dernière intervention 5 août 2009 - 28 juin 2009 à 13:32
fartotodz2004 Messages postés 7 Date d'inscription jeudi 25 octobre 2007 Statut Membre Dernière intervention 5 août 2009 - 24 juil. 2009 à 18:49
Salut à tous je pense que le titre vous a donné une idée de ce que je veux
alors ... j'ai un code de l'algo xtea et xtea bloc decrypt mais écrit en c++ mais je le veux en delphi, j'ai pu traduire la moitier mais j'ai échoué dans la deuxième
voila le code
XTEA Block
---------------------------------------------------------------------------*/
void XTEA_Decrypt(BYTE* Buf, BYTE* Key)
{
DWORD v0, v1, delta, sum, k1, k2;
int i;



v0 = *((DWORD*)(Buf+0));
v1 = *((DWORD*)(Buf+4));
delta = 0x9E3779B9;
sum = 32 * delta;
for (int i=0; i<32; i++)
 {
  k1 = *((DWORD*)(Key+((sum>>9)& 0x0C)));
  v1-= (((v0<<4)^(v0>>5)) + v0)^(sum + k1);
  sum-=delta;
  k2 = *((DWORD*)(Key+((sum & 0x03)<<2)));
  v0-= (((v1<<4)^(v1>>5)) + v1)^(sum + k2);
 }
*((DWORD*)(Buf+0)) = v0;
*((DWORD*)(Buf+4)) = v1;
}
/*---------------------------------------------------------------------------
cbc buggy xtea function
---------------------------------------------------------------------------*/
void XTEA_Bloc_Decrypt(BYTE* Buf, DWORD Size, BYTE* Key)
{
BYTE* pBlock;
BYTE* pMask;



//Last 8 bytes aligned bloc
pBlock = Buf + ((Size - 8) & 0xFFFFFFF8);
if (Size>8)
 {
  for (DWORD i = 0; i<Size/8; i++)
   {
    //Choose IV
    if (pBlock!=Buf)
     pMask = pBlock - 8;
    else
     pMask = Buf+Size-8; //Last complete



    //xor block (buggy cbc-> should be done after decrypting)
    for (int j = 0; j<8; j++)
     pBlock[j] ^= pMask[j];



    //decrypt block
    XTEA_Decrypt(pBlock, Key);



    //next block
    pBlock-=8;
    }
  //decrypt again last block
  XTEA_Decrypt(pMask, Key);
  }
}
//---------------------------------------------------------------------------




et voila ce que j'ai écrit pour la première fonction...




TYPE
XTEABUF = array[0..1] of longword;
PXTEABUF = XTEABUF^
XTEAkey = array[0..3] of longword;
PXTEAkey = XTEAkey^
.
.
.
procedure XTEA_Decrypt(Buf:PXTEABUF;Key:PXTEAkey)
VAR
v0, v1, delta, sum, k1, k2 :longword;
i:integer;
begin
v0 := Buf[0];
v1 := Buf[1];
delta := $9E3779B9;
sum := 32 * delta;
for i := 0 to 31 do
begin
  k1 := Key[((sum>>9) and $C))];
  dec(v1,(((v0 shl 4) xor (v0 shr 5)) + v0)xor(sum + k1));
  dec(sum,delta);
  k2 := Key[((sum and 3) shl 2))];
  dec(v0,(((v1 shl 4) xor (v1 shr 5)) + v1) xor (sum + k2));
end;
buf[0] = v0;
buf[1] = v1;
end;




maintenant il me manque la 2eme partie de "cbc buggy xtea function"  svp chers membres aidez moi à traduire cette fonction ? j'attends vos propositions et merci à tous en avance

8 réponses

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
29 juin 2009 à 18:56
unit XTeaCrypt;

interface

type
  pTeaBuffer = ^TTeaBuffer;
  TTeaBuffer = array[0..1] of LongWord;

  pTeaKey = ^TTeaKey;
  TTeaKey = array[0..3] of LongWord;

const
  SizeOfTeaBuffer = SizeOf(TTeaBuffer);
  SizeOfTeaKey    = SizeOf(TTeaKey);

function XTeaCryptStr(const Buffer, Key: AnsiString): AnsiString;
function XTeaDecryptStr(const Buffer, Key: AnsiString): AnsiString;

procedure XTeaCrypt(const Buffer, Key; const BufferSize, KeySize : integer; var Dest: array of byte);
procedure XTeaDecrypt(const Buffer, Key; const BufferSize, KeySize : integer; var Dest: array of byte);

procedure XTeaCryptOneShoot(var V: TTeaBuffer; const K: TTeaKey);
procedure XTeaDecryptOneShoot(var V: TTeaBuffer; const K: TTeaKey);

implementation

const
  _DELTA = $9E3779B9;
  _N     = 32;

type
  pArByte = ^ArByte;
  ArByte  = array[0..0] of byte;

procedure XTeaCryptOneShoot(var V: TTeaBuffer; const K: TTeaKey);
var
  I: LongWord;
  S: Int64;
begin
  S := 0;
  for I := 0 to _N-1 do
  begin
    Inc(V[0], ((V[1] shl 4) xor (V[1] shr 5)) + (V[1] xor S) + K[S and 3]);
    Inc(S, _DELTA);
    Inc(V[1], ((V[0] shl 4) xor (V[0] shr 5)) + (V[0] xor S) + K[(S shr 11) and 3]);
  end;
end;

procedure XTeaDecryptOneShoot(var V: TTeaBuffer; const K: TTeaKey);
var
  I: LongWord;
  S: Int64;
begin
  S := DELTA * Int64(_N);
  for I := 0 to _N-1 do
  begin
    Dec(V[1], ((V[0] shl 4) xor (V[0] shr 5)) + (V[0] xor S) + K[(S shr 11) and 3]);
    Dec(S, _DELTA);
    Dec(V[0], ((V[1] shl 4) xor (V[1] shr 5)) + (V[1] xor S) + K[S and 3]);
  end;
end;

procedure XTeaCrypt(const Buffer, Key; const BufferSize, KeySize : integer; var Dest: array of byte);
var
  pB, pK : pArByte;
  V: TTeaBuffer;
  K: TTeaKey;
  I, L, N: Integer;
begin
  pB := pArByte(Buffer);
  pK := pArByte(Key);

  L := KeySize;
  if L > SizeOfTeaKey then
    L := SizeOfTeaKey;

  K[0] := 0;
  K[1] := 0;
  K[2] := 0;
  K[3] := 0;
  Move(pK^[0], K[0], L);

  I := 1;
  L := BufferSize;

  if L > 0 then
    SetLength(Dest, ((L-1) div SizeOfTeaBuffer+1) * SizeOfTeaBuffer)
  else
    SetLength(Dest, 0);

  while I <= L do
  begin
    V[0] := 0;
    V[1] := 0;

    N := L - I + 1;

    if N > SizeOfTeaBuffer then
      N := SizeOfTeaBuffer;

    Move(pB^[I], V[0], N);

    XTeaCryptOneShoot(V, K);

    Move(V[0], Dest[I], SizeOfTeaBuffer);
    Inc(I, SizeOfTeaBuffer);
  end;
end;

procedure XTeaDecrypt(const Buffer, Key; const BufferSize, KeySize : integer; var Dest: array of byte);
var
  pB, pK : pArByte;
  V: TTeaBuffer;
  K: TTeaKey;
  I, L, N: Integer;
begin
  pB := pArByte(Buffer);
  pK := pArByte(Key);

  L := KeySize;

  if L > SizeOfTeaKey then
    L := SizeOfTeaKey;

  K[0] := 0;
  K[1] := 0;
  K[2] := 0;
  K[3] := 0;
  Move(pK^[0], K[0], L);

  I := 1;
  L := BufferSize;

  if L > 0 then
    SetLength(Dest, ((L-1) div SizeOfTeaBuffer+1) * SizeOfTeaBuffer)
  else
    SetLength(Dest, 0);

  while I <= L do
  begin
    V[0] := 0;
    V[1] := 0;

    N := L - I + 1;

    if N > SizeOfTeaBuffer then
      N := SizeOfTeaBuffer;

    Move(pB^[I], V[0], N);

    XTeaDecryptOneShoot(V, K);

    Move(V[0], Dest[I], SizeOfTeaBuffer);

    Inc(I, SizeOf(V))
  end;
end;

function XTeaCryptStr(const Buffer, Key: AnsiString): AnsiString;
var
  V: TTeaBuffer;
  K: TTeaKey;
  I, L, N: Integer;
begin
  L := Length(Key);
  if L > SizeOfTeaKey then
    L := SizeOfTeaKey;

  K[0] := 0;
  K[1] := 0;
  K[2] := 0;
  K[3] := 0;
  Move(Key[1], K[0], L);

  I := 1;
  L := Length(Buffer);

  if L > 0 then
    SetLength(Result, ((L-1) div SizeOfTeaBuffer+1) * SizeOfTeaBuffer)
  else
    SetLength(Result, 0);

  while I <= L do
  begin
    V[0] := 0;
    V[1] := 0;

    N := L - I + 1;

    if N > SizeOfTeaBuffer then
      N := SizeOfTeaBuffer;

    Move(Buffer[I], V[0], N);

    XTeaCryptOneShoot(V, K);

    Move(V[0], Result[I], SizeOfTeaBuffer);
    Inc(I, SizeOfTeaBuffer);
  end;
end;

function XTeaDecryptStr(const Buffer, Key: AnsiString): AnsiString;
var
  V: TTeaBuffer;
  K: TTeaKey;
  I, L, N: Integer;
begin
  L := Length(Key);

  if L > SizeOfTeaKey then
    L := SizeOfTeaKey;

  K[0] := 0;
  K[1] := 0;
  K[2] := 0;
  K[3] := 0;
  Move(Key[1], K[0], L);

  I := 1;
  L := Length(Buffer);
  if L > 0 then
    SetLength(Result, ((L-1) div SizeOfTeaBuffer+1) * SizeOfTeaBuffer)
  else
    SetLength(Result, 0);

  while I <= L do
  begin
    V[0] := 0;
    V[1] := 0;

    N := L - I + 1;

    if N > SizeOfTeaBuffer then
      N := SizeOfTeaBuffer;

    Move(Buffer[I], V[0], N);

    XTeaDecryptOneShoot(V, K);

    Move(V[0], Result[I], SizeOfTeaBuffer);

    Inc(I, SizeOfTeaBuffer)
  end;
end;

end.

<hr size="2" width="100%" />
0
DRJEROME Messages postés 436 Date d'inscription jeudi 9 janvier 2003 Statut Membre Dernière intervention 5 février 2015
29 juin 2009 à 23:31
0
fartotodz2004 Messages postés 7 Date d'inscription jeudi 25 octobre 2007 Statut Membre Dernière intervention 5 août 2009
20 juil. 2009 à 18:12
j'ai tous vu mais je cherche la traduction exacte de mon code pck ya des choz spécial dans ce code svp aidez moi à le traduire
0
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
20 juil. 2009 à 19:01
regarde mon message c'est la trad de ton truc.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
fartotodz2004 Messages postés 7 Date d'inscription jeudi 25 octobre 2007 Statut Membre Dernière intervention 5 août 2009
21 juil. 2009 à 10:23
merci foxi voila ce que je cherche XTEA_Bloc_Decrypt mais tu as ajouté XTeaDecryptStr !!! ce n'est pas présent dans mon code
0
fartotodz2004 Messages postés 7 Date d'inscription jeudi 25 octobre 2007 Statut Membre Dernière intervention 5 août 2009
21 juil. 2009 à 10:24
en plus j'ai trouvé des erreurs sur delphi 6 alors svp compile ton code et réponds moi
0
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
21 juil. 2009 à 19:32

unit XTeaCrypt;

interface

uses Windows, SysUtils;

type
  pBytes  = ^Bytes;
  Bytes   = array of byte;

  pTeaBuffer = ^TTeaBuffer;
  TTeaBuffer = array[0..1] of LongWord;

  pTeaKey = ^TTeaKey;
  TTeaKey = array[0..3] of LongWord;

const
  SizeOfTeaBuffer = SizeOf(TTeaBuffer);
  SizeOfTeaKey    = SizeOf(TTeaKey);

procedure XTea_Bloc_Crypt(var V: TTeaBuffer; const K: TTeaKey);
procedure XTea_Bloc_Decrypt(var V: TTeaBuffer; const K: TTeaKey);

procedure XTea_Crypt(const Buffer, Key; const BufferSize, KeySize : integer; var Dest: pBytes);
procedure XTea_Decrypt(const Buffer, Key; const BufferSize, KeySize : integer; var Dest: pBytes);

function XTea_Crypt_Str(const Buffer, Key: AnsiString): AnsiString;
function XTea_Decrypt_Str(const Buffer, Key: AnsiString): AnsiString;


implementation

const
  _DELTA = $9E3779B9;
  _N     = 32;

type
  pArByte = ^ArByte;
  ArByte  = array[0..0] of byte;

procedure XTea_Bloc_Crypt(var V: TTeaBuffer; const K: TTeaKey);
var
  I: LongWord;
  S: Int64;
begin
  S := 0;
  for I := 0 to _N-1 do
  begin
    Inc(V[0], ((V[1] shl 4) xor (V[1] shr 5)) + (V[1] xor S) + K[S and 3]);
    Inc(S, _DELTA);
    Inc(V[1], ((V[0] shl 4) xor (V[0] shr 5)) + (V[0] xor S) + K[(S shr 11) and 3]);
  end;
end;

procedure XTea_Bloc_Decrypt(var V: TTeaBuffer; const K: TTeaKey);
var
  I: LongWord;
  S: Int64;
begin
  S := _DELTA * Int64(_N);
  for I := 0 to _N-1 do
  begin
    Dec(V[1], ((V[0] shl 4) xor (V[0] shr 5)) + (V[0] xor S) + K[(S shr 11) and 3]);
    Dec(S, _DELTA);
    Dec(V[0], ((V[1] shl 4) xor (V[1] shr 5)) + (V[1] xor S) + K[S and 3]);
  end;
end;

procedure XTea_Crypt(const Buffer, Key; const BufferSize, KeySize : integer; var Dest: pBytes);
var
  pB, pK : pArByte;
  V: TTeaBuffer;
  K: TTeaKey;
  I, L, N: Integer;
begin
  pB := pArByte(Buffer);
  pK := pArByte(Key);

  L := KeySize;
  if L > SizeOfTeaKey then
    L := SizeOfTeaKey;

  K[0] := 0;
  K[1] := 0;
  K[2] := 0;
  K[3] := 0;
  Move(pK^[0], K[0], L);

  I := 1;
  L := BufferSize;

  if L > 0 then
    SetLength(Dest^, ((L-1) div SizeOfTeaBuffer+1) * SizeOfTeaBuffer)
  else
    exit;

  while I <= L do
  begin
    V[0] := 0;
    V[1] := 0;

    N := L - I + 1;

    if N > SizeOfTeaBuffer then
      N := SizeOfTeaBuffer;

    Move(pB^[I], V[0], N);

    XTea_Bloc_Crypt(V, K);

    Move(V[0], Dest^[I], SizeOfTeaBuffer);
    Inc(I, SizeOfTeaBuffer);
  end;
end;

procedure XTea_Decrypt(const Buffer, Key; const BufferSize, KeySize : integer; var Dest: pBytes);
var
  pB, pK : pArByte;
  V: TTeaBuffer;
  K: TTeaKey;
  I, L, N: Integer;
begin
  pB := pArByte(Buffer);
  pK := pArByte(Key);

  L := KeySize;

  if L > SizeOfTeaKey then
    L := SizeOfTeaKey;

  K[0] := 0;
  K[1] := 0;
  K[2] := 0;
  K[3] := 0;
  Move(pK^[0], K[0], L);

  I := 1;
  L := BufferSize;

  if L > 0 then
    SetLength(Dest^, ((L-1) div SizeOfTeaBuffer+1) * SizeOfTeaBuffer)
  else
    exit;

  while I <= L do
  begin
    V[0] := 0;
    V[1] := 0;

    N := L - I + 1;

    if N > SizeOfTeaBuffer then
      N := SizeOfTeaBuffer;

    Move(pB^[I], V[0], N);

    XTea_Bloc_Decrypt(V, K);

    Move(V[0], Dest^[I], SizeOfTeaBuffer);

    Inc(I, SizeOf(V))
  end;
end;


function XTea_Crypt_Str(const Buffer, Key: AnsiString): AnsiString;
var
  V: TTeaBuffer;
  K: TTeaKey;
  I, L, N: Integer;
begin
  L := Length(Key);
  if L > SizeOfTeaKey then
    L := SizeOfTeaKey;

  K[0] := 0;
  K[1] := 0;
  K[2] := 0;
  K[3] := 0;
  Move(Key[1], K[0], L);

  I := 1;
  L := Length(Buffer);

  if L > 0 then
    SetLength(Result, ((L-1) div SizeOfTeaBuffer+1) * SizeOfTeaBuffer)
  else
    SetLength(Result, 0);

  while I <= L do
  begin
    V[0] := 0;
    V[1] := 0;

    N := L - I + 1;

    if N > SizeOfTeaBuffer then
      N := SizeOfTeaBuffer;

    Move(Buffer[I], V[0], N);

    XTea_Bloc_Crypt(V, K);

    Move(V[0], Result[I], SizeOfTeaBuffer);
    Inc(I, SizeOfTeaBuffer);
  end;
end;

function XTea_Decrypt_Str(const Buffer, Key: AnsiString): AnsiString;
var
  V: TTeaBuffer;
  K: TTeaKey;
  I, L, N: Integer;
begin
  L := Length(Key);

  if L > SizeOfTeaKey then
    L := SizeOfTeaKey;

  K[0] := 0;
  K[1] := 0;
  K[2] := 0;
  K[3] := 0;
  Move(Key[1], K[0], L);

  I := 1;
  L := Length(Buffer);
  if L > 0 then
    SetLength(Result, ((L-1) div SizeOfTeaBuffer+1) * SizeOfTeaBuffer)
  else
    SetLength(Result, 0);

  while I <= L do
  begin
    V[0] := 0;
    V[1] := 0;

    N := L - I + 1;

    if N > SizeOfTeaBuffer then
      N := SizeOfTeaBuffer;

    Move(Buffer[I], V[0], N);

    XTea_Bloc_Decrypt(V, K);

    Move(V[0], Result[I], SizeOfTeaBuffer);

    Inc(I, SizeOfTeaBuffer)
  end;
end;

end.
0
fartotodz2004 Messages postés 7 Date d'inscription jeudi 25 octobre 2007 Statut Membre Dernière intervention 5 août 2009
24 juil. 2009 à 18:49
merci f0xi je vais tester l'unité et te répondre
0
Rejoignez-nous