Svp aidez moi .... algo xtea

aliilyas Messages postés 31 Date d'inscription lundi 29 juin 2009 Statut Membre Dernière intervention 5 février 2014 - 18 avril 2013 à 12:41
aliilyas Messages postés 31 Date d'inscription lundi 29 juin 2009 Statut Membre Dernière intervention 5 février 2014 - 22 avril 2013 à 16:21
Salut à tous
j'ai un code de l'algo xtea
Code Delphi :




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.




Comment puis-je utiliser cette procédure pour Décrypter le fichier bin ....et merci.

12 réponses

sp40 Messages postés 1276 Date d'inscription mardi 28 octobre 2003 Statut Contributeur Dernière intervention 3 juillet 2015 15
18 avril 2013 à 14:26
Salut,

Je vais peut être dire une bêtise, mais je ne pense pas que tu puisses "décrypter" directement un fichier par ces fonctions... peut-être en ouvrant ton fichier bin et en décodant caractère par caractère ou ligne par ligne ?

Et par ailleurs, dans les procedure suivantes, tu as Buffer et Key qui ne sont pas typés...

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);


Simon
0
aliilyas Messages postés 31 Date d'inscription lundi 29 juin 2009 Statut Membre Dernière intervention 5 février 2014
18 avril 2013 à 14:36
merci simonpelloquin
Comment faire pour entrer les valeurs d'une variable
array of LongWord;
dans cette
procedure ......
procedure XTea_Decrypt(const Buffer, Key; const BufferSize, KeySize : integer; var Dest: pBytes);
0
sp40 Messages postés 1276 Date d'inscription mardi 28 octobre 2003 Statut Contributeur Dernière intervention 3 juillet 2015 15
18 avril 2013 à 14:42
Aucune idée...
Tu n'as pas de doc ou d'exemple d'utilisation sur ce code ?


Simon
0
aliilyas Messages postés 31 Date d'inscription lundi 29 juin 2009 Statut Membre Dernière intervention 5 février 2014
18 avril 2013 à 14:45
J'ai utilisé ce code

var m:tmemorystream;
buf : array[0..3000000] of longword;
begin
if not opendialog1.Execute then exit;
m:=tmemorystream.Create;
m.LoadFromFile(opendialog1.FileName);
m.Position:=0;
m.Read(buf,m.Size);
m.Free;

Comment puis-je envoyer les valeurs buf a procedure XTea_Decrypt
0

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

Posez votre question
sp40 Messages postés 1276 Date d'inscription mardi 28 octobre 2003 Statut Contributeur Dernière intervention 3 juillet 2015 15
18 avril 2013 à 15:03
apparemment, ton "buf" correspond au premier paramètre de ta méthode...

Simon
0
aliilyas Messages postés 31 Date d'inscription lundi 29 juin 2009 Statut Membre Dernière intervention 5 février 2014
18 avril 2013 à 15:26
oui.
buf
Situé dans la première paramètre de la procedure ...et dans la deuxième paramètre injecter key et la taille du buf et la taille du key ....
mais le message d'erreur indiquant.
0
sp40 Messages postés 1276 Date d'inscription mardi 28 octobre 2003 Statut Contributeur Dernière intervention 3 juillet 2015 15
18 avril 2013 à 16:12
Quoi ?

Simon
0
aliilyas Messages postés 31 Date d'inscription lundi 29 juin 2009 Statut Membre Dernière intervention 5 février 2014
19 avril 2013 à 20:09
mais le message d'erreur indiquant.
0
sp40 Messages postés 1276 Date d'inscription mardi 28 octobre 2003 Statut Contributeur Dernière intervention 3 juillet 2015 15
20 avril 2013 à 13:18
Indiquant quoi ? Je ne suis pas devant ton écran et n'ai pas encore le don de deviner ce qu'il se passe sur les ordi des autres... Expliques un peu mieux les problèmes que tu rencontres si tu veux que je t'aide


Simon
0
aliilyas Messages postés 31 Date d'inscription lundi 29 juin 2009 Statut Membre Dernière intervention 5 février 2014
22 avril 2013 à 12:31
Expliquez comment cela fonctionne: les variables
dans cette
procedure XTea_Decrypt(const Buffer, Key; const BufferSize, KeySize : integer; var Dest: pBytes);
0
sp40 Messages postés 1276 Date d'inscription mardi 28 octobre 2003 Statut Contributeur Dernière intervention 3 juillet 2015 15
22 avril 2013 à 14:11
Les "variables" ce sont des paramètres pour ta fonction. Ici, tu dois fournir Buffer, Key, BufferSize et KeySize et apparemment, tu récupères le décryptage dans Dest.

Simon
0
aliilyas Messages postés 31 Date d'inscription lundi 29 juin 2009 Statut Membre Dernière intervention 5 février 2014
22 avril 2013 à 16:21
merci simon.
J'ai utilisé le type du buffer comme Array of byte ?
0
Rejoignez-nous