Bonjour,

Je m'entraîne pour la réalisation d'un encodeur de chaînes de caractères.
J'ai trouvé un code source intéressant sur internet ; en l'imitant, il est possible de crypter/décrypter mais impossible d'avoir les résultats attendus.

L'encodeur original est normalement censé transformer les chaînes de caractères sous la forme de :#AB#BC#01#02 sauf que j'obtiens des résultats sous la forme de caractères, chiffres, et caractères spéciaux (Ex : FqÙQvø‹;`).

Les résultats de l'encodeur original XTEa Encoder for Delphi (http://www.irnis.net/soft/xted/)

Exemple :

Mes résultats, légèrement différents :



Le problème c'est que si je reste sous la forme 'FqÙQvø‹', l'encryption et la décryption ne marchent pas pour tous les mots (notamment lorsque l'on change la clé d'encryption).

Voici le code que j'utilise :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{$APPTYPE CONSOLE}
program demo;
uses
  XTea;  //Ce dont à besoin
Var
  Encoded : String;
  Mot : String;
const
  Password = 'egewa54';   //Clé d'encryption
begin
  Writeln('Entrez votre mot');
  read(Mot);
  ReadLn;
  Writeln('-----------------------------');
  Writeln('Votre chaine crypte avec la fonction XTeaCryptStr est :');
  Encoded := XTeaCryptStr(Mot, Password);  //cryptage
  Writeln(Encoded);  //à ce moment ce n'est pas encrypté dans la forme que je veux
  Writeln('-----------------------------');
  Writeln('Votre chaine decrypte avec la fonction XTeaDecryptStr est :');
  Writeln(XTeaDecryptStr(Encoded, Password));    //decryptage, ça marche quand même
  Writeln('==============================');
  Writeln('');
  Writeln('En utilisant un password au hasard par exemple "Ceci est un test" dans le GUI "XTea Encoder for Delphi", le mot encrypte devient');   //J'utilises le GUI pour encrypté un mot
  Writeln('');
  Writeln('#232#038#058#077#057#027#003#251#033#202#179#179#253#147#194#206;'); //La phrase "Ceci est un test" à été encrypté dans le GUI
  Encoded := #232#038#058#077#057#027#003#251#033#202#179#179#253#147#194#206;
  Writeln('La fonction XTeDecryptStr peut quand meme decrypte le mot');
  Writeln('Preuve :');
  Writeln(XTeaDecryptStr(Encoded, Password));
  Writeln('');
  Writeln('Bizarre ou pas ?');
   ReadLn;
end.
Et voici le code du projet XTea Encoder for Delphi (il n'y a rien d'autres) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
unit xtea;
 
interface
 
type
  TTeaMsgBlock = array[0..1] of LongWord;
  TTeaKeyBlock = array[0..3] of LongWord;
 
procedure XTeaCrypt(var V: TTeaMsgBlock; const K: TTeaKeyBlock);
procedure XTeaDecrypt(var V: TTeaMsgBlock; const K: TTeaKeyBlock);
function XTeaCryptStr(const Msg, Pwd: string): string;
function XTeaDecryptStr(const Msg, Pwd: string): string;
 
implementation
{ ========================================================================= }
const
  DELTA = $9e3779b9;
  N = 32;
 
procedure XTeaCrypt(var V: TTeaMsgBlock; const K: TTeaKeyBlock);
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 XTeaDecrypt(var V: TTeaMsgBlock; const K: TTeaKeyBlock);
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;
 
function XTeaCryptStr(const Msg, Pwd: string): string;
var
  V: TTeaMsgBlock;
  K: TTeaKeyBlock;
  I, L, N: Integer;
begin
  L := Length(Pwd); if L > SizeOf(K) then L := SizeOf(K);
  K[0] := 0; K[1] := 0; K[2] := 0; K[3] := 0; Move(Pwd[1], K[0], L);
 
  I := 1; L := Length(Msg);
  if L > 0 then SetLength(Result, ((L - 1) div SizeOf(V) + 1) * SizeOf(V))
           else SetLength(Result, 0);
  while I <= L do begin
    V[0] := 0; V[1] := 0;
    N := L - I + 1; if N > SizeOf(V) then N := SizeOf(V);
    Move(Msg[I], V[0], N);
    XTeaCrypt(V, K);
    Move(V[0], Result[I], SizeOf(V));
    Inc(I, SizeOf(V))
  end;
end;
 
function XTeaDecryptStr(const Msg, Pwd: string): string;
var
  V: TTeaMsgBlock;
  K: TTeaKeyBlock;
  I, L, N: Integer;
begin
  L := Length(Pwd); if L > SizeOf(K) then L := SizeOf(K);
  K[0] := 0; K[1] := 0; K[2] := 0; K[3] := 0; Move(Pwd[1], K[0], L);
 
  I := 1; L := Length(Msg);
  if L > 0 then SetLength(Result, ((L - 1) div SizeOf(V) + 1) * SizeOf(V))
           else SetLength(Result, 0);
  while I <= L do begin
    V[0] := 0; V[1] := 0;
    N := L - I + 1; if N > SizeOf(V) then N := SizeOf(V);
    Move(Msg[I], V[0], N);
    XTeaDecrypt(V, K);
    Move(V[0], Result[I], SizeOf(V));
    Inc(I, SizeOf(V))    
  end;
end;
{ ========================================================================= }
end.
Merci à vous pour votre future aide ...

Cordialement

PS : Sources : http://www.irnis.net/soft/xted/