unit UnitMainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls,Math;
type
TMainForm =
class(TForm)
ButtonEncrypt: TButton;
OpenDialogEncrypt: TOpenDialog;
Memo1: TMemo;
ButtonDecrypt: TButton;
ProgressBar1: TProgressBar;
function GenerateKey: Integer;
procedure ButtonEncryptClick(Sender: TObject);
procedure HandleFile(done:Longint; Filename:
String; Mode: Boolean);
procedure ButtonDecryptClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
function Encode(what:
string; key: Integer):
String;
var
i: integer;
begin
Result := what;
for i := 1
to length(Result)
do
Result[i] := Char((Ord(Result[i]) + Key)
mod 256);
end;
function Decode(what:
string; key: Integer):
String;
var
i: integer;
begin
Result := what;
for i := 1
to length(Result)
do
Result[i] := Char((Ord(Result[i]) - Key)
mod 256);
end;
function TMainForm.GenerateKey: Integer;
begin
randomize;
Result:= random(79)+20;
end;
procedure TMainForm.ButtonDecryptClick(Sender: TObject);
begin
if MainForm.OpenDialogEncrypt.Execute
then
begin
HandleFile(0, OpenDialogEncrypt.FileName, true);
end;
end;
procedure TMainForm.ButtonEncryptClick(Sender: TObject);
begin
if MainForm.OpenDialogEncrypt.Execute
then
begin
HandleFile(0, OpenDialogEncrypt.Filename, false);
end;
end;
procedure TMainForm.HandleFile(done:Longint; Filename:
String; Mode: Boolean);
const
BufferSize = 1024;
var
Stream, WStream: TFileStream;
Buffer:
String;
adone, datasize: LongInt;
Key, i: integer;
begin
Stream :=
nil;
// Falls eine Exception auftritt
WStream :=
nil;
// ist Free auf nil-Pointer zulässig
try
// SetLength(Buffer, BufferSize);
Key := GenerateKey;
Stream := TFileStream.Create(Filename, fmOpenRead);
if Mode = true
then
begin
SetLength(Buffer,2);
WStream := TFileStream.Create(Copy(Filename,1, Pos('
.backup', Filename)), fmOpenWrite
or fmCreate);
Stream.Position := 0;
Stream.ReadBuffer(Buffer[1], 1);
Stream.ReadBuffer(Buffer[2], 1);
Key := StrToInt(Buffer[1]+Buffer[2]);
adone := Stream.Size-2;
Buffer := '
';
end else if Mode = false
then
begin
Buffer := IntToStr(Key);
WStream := TFileStream.Create(Filename+'
.backup', fmOpenWrite
or fmCreate);
WStream.
Write(Buffer[1], 1);
// kompletten Key als Zeichenfolge speichern
WStream.
Write(Buffer[2], 1);
adone := Stream.Size;
end;
case Key
of // Key könnte eigentlich im Bereich 0..255 liegen
20..99: ;
// durch die Art der Speicherung auf 2stellig beschränkt
else
ShowMessage(Format('
Key %d liegt nicht im gültigen Bereich', [Key]));
exit;
end;
ShowMessage(IntToStr(Key));
ProgressBar1.Max := adone;
ProgressBar1.Position := 0;
ProgressBar1.Min := 0;
while (adone > 0)
do
begin
// Stream.Seek(done, soBeginning);
datasize := Min(adone, BufferSize);
SetLength(Buffer, datasize);
Stream.
Read(Buffer[1], datasize);
Dec(adone, datasize);
ShowMessage(Buffer);
if Mode = true
then
Buffer:= Decode(Buffer, Key)
else if Mode = false
then
Buffer:= Encode(Buffer, Key);
WStream.
Write(Buffer[1], datasize);
ProgressBar1.Position := ProgressBar1.Max - adone;
end;
finally
Stream.Free;
WStream.Free;
end;
end;
end.