在Delphi中預定義了Tfilestream類,通過它可以對磁盤文件進行讀寫,筆者選定Tfilestream為基類,通過對其核心的兩個讀、寫方法進行重載,編寫定制的文件流,實現對文件的讀、寫進行加密。
首先,來看一下定制文件流(Tmystream)的聲明:
type
Tmystream=class(Tfilestream)
private
fkey:string;
public
constructor create
(const filename:string;mode:Word);
function read(var buffer;count:longint):
longint;override;
function write(const buffer;count:longint):
longint;override;
property key:string read fkey write fkey ;
end;
在Tmystream的聲名中,我們對read、write兩個方法進行了重載,並添加了一個新的特性key,用以存儲對文件進行加密時所需的密碼。為實現文件讀寫的加密,在write方法中,將key的每個字符依次與buffer中的字符相加,將得到的結果寫入文件,實現加密;在read方法中,將讀出的內容依次與key的每個字符相減,實現解密。加密及解密的方法多種多樣,讀者可以通過改寫相關代碼,得到不同的加密方法。
程序清單如下:
function Tmystream.write(const buffer;
count:longint):longint;
var
Pbu,Pmy,mykey:pchar;
i,enc:integer;
begin
getmem(pmy,count); //為pmy分配內存
mykey:=pchar(key); //將key轉換為pchar指針
try
pbu:=pchar(@buffer); //將buffer轉換為pchar指針
for i:=0 to count-1 do
//將key的每個字符以此與buffer的
每個字符循環相加,結果放入pmy指向的內存區
begin
enc:=(ord(pbu[i])+ord(mykey
[(i mod length(key))])) mod 256;
Pmy[i]:=char(enc);
end;
result:=inherited write(Pmy^,count);
//將pmy指向的內容寫入文件
finally
freemem(Pmy,count);
end;
end;
function Tmystream.read(var buffer;count:longint):
longint;
var
Pbu,Pmy,mykey:pchar;
i,mycount,enc:integer;
begin
getmem(Pmy,count);//為pmy分配內存
mykey:=pchar(key);//將key轉換為pchar指針
try
mycount:=inherited read(Pmy^,count);
//將文件內容讀入pmy指向內存區
Pbu:=Pchar(@buffer);將buffer轉換為pchar指針
for i:=0 to mycount-1 do//將key的每個字符依次
與pmy的每個字符循環相減,結果放入pbu指向的變量
begin
enc:=(ord(Pmy[i])-ord(mykey
[(i mod length(key))])) mod 256;
Pbu[i]:=chr(enc);
end;
finally
freemem(P count);
end;
result:=mycount;
end;
完成定制文件流的編寫後,便可在程序中應用,實現文件的讀寫加密,例程如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls,unit2,unit3;
//unit2定義了Tmystream
//unit3定義了輸入密碼對話框form3
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Panel1: TPanel;
Panel2: TPanel;
Memo1: TMemo;
Splitter1: TSplitter;
Memo2: TMemo;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
//將選定的加密文件解開,讀入memo2
var
encstr:Tmystream;
begin
if opendialog1.Execute and (form3.showmodal=mrok)
then
begin
encstr:=Tmystream.create
(opendialog1.filename,fmopenread);
encstr.key:=form3.Edit1.Text;
try
memo2.lines.LoadFromStream(encstr);
finally
encstr.Free;
end;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
//將memo1中的內容加密,用指定文件名另存
var
encstr:Tmystream;
begin
if savedialog1.Execute and (form3.showmodal=mrok)
then
begin
encstr:=Tmystream.create(savedialog1.filename,
fmcreate);
encstr.key:=form3.Edit1.Text;
try
memo1.lines.SaveToStream(encstr);
finally
encstr.Free;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
//將指定文件讀入memo1
var
mystr:Tfilestream;
begin
if opendialog1.Execute then
begin
mystr:=Tfilestream.create
(opendialog1.filename,fmopenread);
try
memo1.lines.LoadFromStream(mystr);
finally
mystr.Free;
end;
end;
end;