编写定制的文件流实现文件读写加密
整理编辑:China ASP
在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(Pmy,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;
end.
---- 以上程序均在windows 98、Delphi4.0环境中,通过编译,运行正常。