Bonjour,

J'essaie d'utiliser la bibliothèque DCPCrypt.
Je suis partie d'un code que j'ai trouvé ici.

J'arrive à crypter/décrypter une chaine de caractères, mais pas à décrypter un fichier, alors que dans son post, il dit que cela fonctionne.

Voici un extrait du code que j'utilise.
J'ai enlevé les fonctions qui fonctionnaient bien.

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
unit Main;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, DCPSha1, DCPmd5, DCPsha512, DCPcrypt2, DCPblockciphers, DCPblowfish,
  dcprijndael
  ;
 
type
 
  { TMain }
 
  TMain = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    OpenDialog1: TOpenDialog;
    PageControl1: TPageControl;
    SaveDialog1: TSaveDialog;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { private declarations }
    function CryptString(Original: String; Password: String; Password2: String): String;
    function DeCryptString(Original: String; Password: String; Password2: String): String;
 
    procedure CryptFile(Original: String; Encrypted:String; Password: String; Password2: String);
    procedure DeCryptFile(Encrypted: String; Sortie:String; Password: String; Password2: String);
  public
    { public declarations }
  end;
 
var
  Main1: TMain;
 
const
  PASSW1: String = 'Un1';
  PASSW2: String = 'Deux2';
 
implementation
 
{$R *.lfm}
 
 
procedure TMain.Button1Click(Sender: TObject);
// ...
 
procedure TMain.Button2Click(Sender: TObject);
// ...
 
procedure TMain.Button3Click(Sender: TObject);  // crypter un fichier
begin
  if (OpenDialog1.FileName = '') then OpenDialog1.FileName := ExtractFileDir(Application.ExeName);
  if (OpenDialog1.Execute) then
  begin
    CryptFile(OpenDialog1.FileName, OpenDialog1.FileName + '.cry', PASSW1, PASSW1);
  end;
end;
 
procedure TMain.Button4Click(Sender: TObject);  // décrypter un fichier
begin
  if (OpenDialog1.FileName = '') then OpenDialog1.FileName := ExtractFileDir(Application.ExeName);
  if (OpenDialog1.Execute) then
  begin
    if (SaveDialog1.FileName = '') then SaveDialog1.FileName := ExtractFileDir(Application.ExeName);
    if (SaveDialog1.Execute) then
    begin
        CryptFile(OpenDialog1.FileName, SaveDialog1.FileName, PASSW1, PASSW1);
    end;
  end;
end;
 
function TMain.CryptString(Original: String; Password: String; Password2: String): String;
// ....
 
function TMain.DeCryptString(Original: String; Password: String; Password2: String): String;
// ...
 
 
procedure TMain.CryptFile(Original: String; Encrypted: String; Password: String; Password2: String);
var
  BlowFish:TDCP_Blowfish;
  AES: TDCP_rijndael;
  OriginalStream:TFileStream;
  EncryptStream:TFileStream;
begin
  AES := TDCP_rijndael.Create(nil);
  AES.InitStr(Password2, TDCP_Sha1);
 
  BlowFish := TDCP_Blowfish.Create(nil);
  BlowFish.InitStr(Password, TDCP_Sha1);
 
  OriginalStream := TFileStream.Create(Original, fmOpenRead);
  EncryptStream  := TFileStream.Create(Encrypted, fmCreate);
 
  Blowfish.EncryptStream(OriginalStream, EncryptStream, OriginalStream.Size);
  BlowFish.Burn;
 
  AES.EncryptStream(EncryptStream, EncryptStream, EncryptStream.Size);
  AES.Burn;
 
  EncryptStream.Free;
  OriginalStream.Free;
  Blowfish.Free;
  AES.Free;
end;
 
procedure TMain.DeCryptFile(Encrypted: String; Sortie: String; Password: String; Password2: String);
var
  BlowFish:TDCP_Blowfish;
  AES: TDCP_rijndael;
  DecryptStream:TMemoryStream;
  EncryptStream:TFileStream;
begin
  AES := TDCP_rijndael.Create(nil);
  AES.InitStr(Password2, TDCP_Sha1);
 
  BlowFish := TDCP_Blowfish.Create(nil);
  BlowFish.InitStr(Password, TDCP_Sha1);
 
  EncryptStream := TFileStream.Create(Encrypted, fmOpenRead);
  DecryptStream := TMemoryStream.Create;
 
  AES.DecryptStream(EncryptStream, DecryptStream, EncryptStream.Size);
  AES.Burn;
 
  Blowfish.DecryptStream(DecryptStream, DecryptStream, DecryptStream.Size);
  Blowfish.Burn;
 
  EncryptStream.Free;
  DecryptStream.Position := 0;
  DecryptStream.SaveToFile(Sortie);
  DecryptStream.Free;
  AES.Free;
  BlowFish.Free;
end;
 
end.
Quelqu'un aurait-il une idée de ce qui ne vas dans ce code ?

Merci.