Bonjour a tous,

j'ai une error quand je décrypt du text si le text a été crypter en 1 passe j'ai pas d'error par contre si il a été crypter en plusieur passe j'ai une Error

voila a chaque cryptage de texte j'insert la clé de cryptage + le nombre de passe a la fin de la chaîne crypter < la clé $ le nombre de passe >

pour le décryptage je récupére les positions de ma balise je copier la clé dans un variable + le nombre de passe dans un autre variable.


Code :

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
91
92
93
94
95
96
97
98
99
100
// Function cryptage
function CryptSecurity(const Text,KeyCrypt: string; Pass: String): string;
var
I, J: Integer;
ChCrypt : String;
begin
ChCrypt := '';
J := 1;
for I := 1 to Length(Text) do
 begin
  ChCrypt := ChCrypt + IntToHex(Ord(Text[I]) xor Ord(KeyCrypt[J]),2);
  Inc(J);
  if J > Length(KeyCrypt) then
   J := 1; // On insert la clé de cryptage + le nombre de passe
   Result :=  ChCrypt +'<' + KeyCrypt + '$' + Pass + '>';
    end;
 end;
 
 {Function de décryptage}
function DecryptSecurity( const Text,KeyCrypt : string): string;
var
I, J: Integer;
begin
Result := '';
J := 1;
I := 1;
repeat
 Result := Result + Chr(StrToInt('$' + Copy(Text, I, 2)) xor Ord(KeyCrypt[J]));
 Inc(I, 2);
 Inc(J);
 if J > Length(KeyCrypt) then J := 1;
until I > Length(Text);
end;
 
// Générateur de clé
Function KeyRandom(Nbr:Integer): String ;
const
  ChAlpha = '013456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
begin
  Randomize;
  repeat
   Result := Result + ChAlpha[Random(Length(ChAlpha)) + 1];
  until Length(Result) = Nbr; // Longueur de la clé
end;
 
 
procedure TForm1.BtKeyClick(Sender: TObject);
begin 
  GKey.Text := KeyRandom(LengthKey.Value);
end;
 
// Cryptage
procedure TForm1.BtCryptClick(Sender: TObject);
var
 NbPass : Integer;
begin
NbPass := 0;
Gauge.MaxValue := Pass.Value;
repeat
 begin
  Zone.Text := CryptSecurity(Zone.Text,GKey.Text,Inttostr(Pass.Value));
  Inc(NbPass);
  Zone.Refresh;
  Gauge.Progress := Gauge.Progress +1;
 end;
until NbPass = Pass.Value;
Gauge.Progress := 0;
end;
 
// Décryptage
procedure TForm1.BtDecryptClick(Sender: TObject);
var
 PosSub1,PosSub2,PosSub3,Pass : Integer;
 KeyHexo,NbPass : String;
begin
  Pass := 0;
  NbPass := '';
  //Zone.Lines.BeginUpdate;
  PosSub1 := pos('<',Zone.Text);
  PosSub2 := Pos('$',Zone.Text);
  PosSub3 := pos('>',Zone.Text);
  // On récupére la clé de cryptage
  KeyHexo := Copy(Zone.Text,PosSub1 +1,(PosSub2 -1) - PosSub1);
  // On récupére le nombre de passe
  NbPass  := Copy(Zone.Text,(PosSub2 +1),1);
  Zone.SelStart := PosSub1 -1;
  Zone.SelLength := (PosSub3 +1) - PosSub1;
  Zone.SelText := #0;
  //Zone.Lines.EndUpdate;
  Gauge.MaxValue := Strtoint(NbPass);
repeat
 begin // On décrypte
  Zone.Text := DeCryptSecurity(Zone.Text,KeyHexo);
  Inc(Pass);
  Zone.Refresh;
  Gauge.Progress := Gauge.Progress +1;
 end;
until Pass = Strtoint(NbPass);
Gauge.Progress := 0;
end;

Merci de votre réponse.