// Datei entschlüsseln
function TFrmCipher.DecodeFile(Input, Output, Passwd:
String): Boolean;
var
SrcStream, DestStream: TStreamProgressAdapter;
TempStream: TMemoryStream;
NewHeader, OldHeader: TFileHeader;
begin
Button13.Show;
ProgressBar1.Show;
LbProgress.Show;
FrmCipher.Refresh;
result := False;
SrcStream := TStreamProgressAdapter.Create(TFileStream.Create(Input, fmOpenRead
or fmShareDenyNone),
Handle);
if Assigned(SrcStream)
then
begin
try
DestStream := TStreamProgressAdapter.Create(TFileStream.Create(Output, fmCreate),
Handle);
if Assigned(DestStream)
then
begin
try
TempStream := TMemoryStream.Create;
if Assigned(TempStream)
then
begin
try
LbProgress.Caption := '
Die Datei wird entschlüsselt... (1/3)';
FrmCipher.Refresh;
with DefCipherClass.Create('
',
nil)
do
begin
try
Mode := DefCipherMode;
HashClass := DefHashClass;
InitKey(Passwd,
nil);
// Zuerst wird SrcStream Decodiert und in TempStream geschrieben
DecodeStream(SrcStream, TempStream, SrcStream.Size);
// jetzt befindet sich am Anfang von TempStream der FileHeader
finally
Free;
end;
end;
TempStream.Seek(0,sofrombeginning);
// Der FileHeader wird gelesen
TempStream.ReadBuffer(OldHeader,SizeOf(TFileHeader));
DestStream.FMax := TempStream.Size - TempStream.Position;
LbProgress.Caption := '
Die entschlüsselte Datei wird aus dem Arbeitsspeicher in die Zieldatei geschrieben... (2/3)';
FrmCipher.Refresh;
DestStream.CopyFrom(TempStream,TempStream.Size-TempStream.Position);
DestStream.Seek(0,sofrombeginning);
LbProgress.Caption := '
Der Hash-Wert der entschlüsselten Datei wird berechnet... (3/3)';
FrmCipher.Refresh;
with DefHashClass.Create(
nil)
do
begin
try
NewHeader.HashString := CalcStream(DestStream,DestStream.Size,
nil,DefFileStringFormat);
finally
Free;
end;
end;
finally
FreeAndNil(TempStream);
end;
end else
begin
RaiseLastOSError();
exit;
end;
finally
FreeAndNil(DestStream);
end;
end else
begin
RaiseLastOSError();
exit;
end;
finally
FreeAndNil(SrcStream);
end;
end else
begin
RaiseLastOSError();
exit;
end;
If NewHeader.HashString = OldHeader.HashString
then
result := true
else
result := false;
LbProgress.Caption := '
';
LbProgress.Hide;
ProgressBar1.Hide;
Button13.Hide;
end;
// Datei verschlüsseln
function TFrmCipher.EncodeFile(Input, Output, Passwd:
String): Boolean;
var
SrcStream: TStreamProgressAdapter;
DestStream: TFileStream;
TempStream: TStreamProgressAdapter;
FileHeader : TFileHeader;
begin
Button13.Show;
ProgressBar1.Show;
LbProgress.Show;
FrmCipher.Refresh;
result := False;
SrcStream := TStreamProgressAdapter.Create(TFileStream.Create(Input, fmOpenRead
or fmShareDenyNone),
Handle);
if Assigned(SrcStream)
then
begin
try
DestStream := TFileStream.Create(Output, fmCreate);
if Assigned(DestStream)
then
begin
try
TempStream := TStreamProgressAdapter.Create(TMemoryStream.Create,
Handle);
if Assigned(TempStream)
then
begin
try
SrcStream.Seek(0,sofrombeginning);
LbProgress.Caption := '
Der Original-Hash-Wert wird berechnet... (1/3)';
FrmCipher.Refresh;;
with DefHashClass.Create(
nil)
do
begin
try
FileHeader.HashString := CalcStream(SrcStream,SrcStream.Size,
nil,DefFileStringFormat);
finally
Free;
end;
end;
TempStream.FMax := SizeOf(TFileHeader);
TempStream.Seek(0,sofrombeginning);
TempStream.
Write(FileHeader, sizeof(TFileHeader));
LbProgress.Caption := '
Die Originaldatei wird in den Arbeitsspeicher kopiert... (2/3)';
FrmCipher.Refresh;
SrcStream.Seek(0,sofrombeginning);
TempStream.FMax := TempStream.Size + SrcStream.Size;
TempStream.CopyFrom(SrcStream,SrcStream.Size);
TempStream.Seek(0,sofrombeginning);
DestStream.Seek(0,sofrombeginning);
LbProgress.Caption := '
Die Datei wird verschlüsselt... (3/3)';
FrmCipher.Update;
with DefCipherClass.Create('
',
nil)
do
begin
try
Mode := DefCipherMode;
HashClass := DefHashClass;
InitKey(Passwd,
nil);
EncodeStream(TempStream, DestStream, TempStream.Size);
finally
Free;
end;
end;
finally
FreeAndNil(TempStream);
end;
end
else
begin
RaiseLastOSError();
exit;
end;
finally
FreeAndNil(DestStream);
end;
end
else
begin
RaiseLastOSError();
exit;
end;
finally
FreeAndNil(SrcStream);
end;
end
else
begin
RaiseLastOSError();
exit;
end;
result := True;
ProgressBar1.Hide;
LbProgress.Caption := '
';
LbProgress.Hide;
Button13.Hide;
end;