关于自定义的文件格式加密的问题!(100分)

  • 主题发起人 主题发起人 happy_boy
  • 开始时间 开始时间
H

happy_boy

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我写的用来存放用户信息的数据格式,如果有写的不何理的地方请大家指点,
希望大家给我些意见?
可是我不知道如何加密写入文件呀?
请问大家是否有好的加密算法?
unit UserInfo;

interface

uses
Windows,SysUtils,Classes,Dialogs;
type
TPurview = (tpRead,tpReadWrite);//权限

TAccessLog = record //记录登陆次数与时间
Count : LongInt;
Time : array[0..100]of TDateTime;
end;

TFileHead = record
Counter : LongInt; //记录用户个数
end;

TFileData = record
Index : LongInt;
UserName : array[0..100] of Char;//用户名
UserPwd : array[0..100] of Char;//密码
UserPuw : TPurview; //用户权限
AccessLog : TAccessLog; //登陆时间
end;
TUserInfo = class
private
FFileName : String;
FFileHandle : Integer; //文件名柄
FFileHead : TFileHead; //数据头
FFileData : TFileData; //数据下文
FLogined : Boolean; //是否登陆
FPwdChanged : Boolean; //是否已经更改密码
procedure CreateLDLFile; //建立空的数据表
function GetUserIndex(Name : String): LongInt; //得到用户在文件中的位置
procedure SaveLoginTime;
procedure SetPwdChanged(Value : Boolean);
public
constructor Create(Fn:String;Mode:Word;Name,Pwd:String);
destructor Destroy;override;
procedure Free;
procedure LoadLDLFile(Pos:LongInt); //读出文件到FFileData
procedure SaveLDLFile(New:Boolean;Pos:LongInt); //不关闭文件情况下保存文件
property FileName : String read FFileName;
property FileData : TFileData read FFileData write FFileData;
property FileHead : TFileHead read FFileHead write FFileHead;
property Logined : Boolean read FLogined write FLogined;
property PwdChanged : Boolean read FPwdChanged write SetPwdChanged;
end;


implementation

procedure TUserInfo.CreateLDLFile ;
begin
with FFileHead,FFileData do
begin
Counter := 0;
Index := 0;
UserName := '';
UserPwd := '';
UserPuw := tpRead;
AccessLog.Count := 1;
AccessLog.Time[AccessLog.Count-1]:=Now;
end;
end;

function TUserInfo.GetUserIndex(Name : String):LongInt;
var
i : LongInt;
begin
Result := 0;
if Name='' then Exit;
FileSeek(FFileHandle,0,0);
FileRead(FFileHandle,FFileHead,Sizeof(TFilehead));
for i := 1 to FFileHead.Counter do
begin
FileRead(FFileHandle,FFileData,Sizeof(TFileData));
if FFileData.UserName = Name then
begin
Result := FFileData.Index ;
Break;
end;
end;
end;

constructor TUserInfo.Create(Fn:String;Mode:Word;Name,Pwd:String);
var
Pos : LongInt;
begin
FFileName := Fn;
FLogined := False;
FPwdChanged := False;
if Mode = fmOpenReadWrite then
begin
FFileHandle := FileOpen(FFileName,fmOpenReadWrite);
Pos := GetUserIndex(Name)-1; //得到name用户的位置
LoadLDLFile(Pos); //读出内容到FFileData
if (Pwd<>'')and(FFileData.UserPwd = Pwd) then
begin
SaveLoginTime;
FLogined := True;
end else
if (Pwd<>'')and(FFileData.UserPwd <> Pwd) then
begin
ShowMessage('用户名或密码不正确,请重新输入!');
end;
end;
if Mode = fmCreate then
begin
FFileHandle := FileCreate(FFileName);
CreateLDLFile;
// FLogined := True;
end;
end;

destructor TUserInfo.Destroy;
begin
if FFileHandle<>-1 then
begin
FileClose(FFileHandle);
FFileHandle:=-1;
FLogined := False;
end;

inherited;
end;

procedure TUserInfo.Free ;
begin
if Assigned(Self) then Destroy;
end;

procedure TUserInfo.LoadLDLFile(Pos: Integer);
begin
FileSeek(FFileHandle,0,0);
FileRead(FFileHandle,FFileHead,Sizeof(TFileHead));
FileSeek(FFileHandle,(Pos)*Sizeof(TFileData),1);
FileRead(FFileHandle,FFileData,Sizeof(TFileData));
end;

procedure TUserInfo.SaveLoginTime ;
begin
FileSeek(FFileHandle,0-Sizeof(TFileData),1);
with FFileData do
begin
Inc(AccessLog.Count);
AccessLog.Time[AccessLog.Count-1] := Now; //每次登陆都记录
end;
FileWrite(FFileHandle,FFileData,Sizeof(TFileData)); //保存登陆时间
end;

procedure TUserInfo.SaveLDLFile(New: Boolean; Pos: Integer);
var
AFileData : TFileData;
Str : String;
begin
if New then //新增的用户
begin
AFileData := FFileData; //AFileData用来做临时对象保存FFileData的内容,因为GetUserIndex改变了FFileData的内容
if GetUserIndex(FFileData.UserName)<>0 then
begin
ShowMessage('该用户名已经存在请重新选择用户名!');
Exit;
end;
FFileData := AFileData;
with FFileData,FFileHead do //设置文件头与文件内容的固定项
begin
Inc(Counter);
Index := Counter;
AccessLog.Count := 1 ;
AccessLog.Time[AccessLog.Count-1] := Now;
end;
FileSeek (FFileHandle,0,0);
FileWrite(FFileHandle,FFileHead,Sizeof(TFileHead));
FileSeek (FFileHandle,0,2);
FileWrite(FFileHandle,FFileData,Sizeof(TFileData));
end else
if (not New)and(Pos<>-1) then //不是新用户
begin
FileSeek (FFileHandle,(Pos-1)*Sizeof(TFileData)+Sizeof(TFileHead),0);
FileWrite(FFileHandle,FFileData,Sizeof(TFileData));
end;
end;

procedure TUserInfo.SetPwdChanged(Value: Boolean);
begin
FPwdChanged := Value;
end;

end.
谢谢大家!
 
不错。贴下来先。
 
其实可以编写定制的文件流实现文件读写的加密。

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)+ord(mykey
[(i mod length(key))])) mod 256;
Pmy:=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)-ord(mykey
[(i mod length(key))])) mod 256;
Pbu:=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.
 
我那个文件的结构是
TFileHead
TFileData
TFileData
TFileData
TFileData
TFileData
.........
可以保存多个用户的信息,用TMyStream可以实现吗?谢谢你。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
507
import
I
后退
顶部