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.
谢谢大家!
希望大家给我些意见?
可是我不知道如何加密写入文件呀?
请问大家是否有好的加密算法?
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.
谢谢大家!