Function Jmf_AddtoFile(SourceFile,PassWord,TargetFile:String):Boolean; Var Target,Source:TFileStream; MyFileSize,PassWordSize:integer; Begin Try Source:=TFileStream.Create(SoureceFile,fmOpenRead or fmShareExclusive); Target:=TFileStream.Create(TargetFile,fmOpenWrite of fmShareExclusive); Try Target.Seek(0,soFromEnd);{往尾部添加資源} Target.CopyFrom(Source,0); MyFileSize:=Source.Size++Sizeof(MyFileSize);{計算資源大小,並寫入輔程尾部} Target.WriteBuffer(MyFilesSize,sizefo(MyFileSize)); PassWordSize:=Sizeof(PassWord)+sizeof(PassWordSize); Target.Seek(0,soFromEnd); Target.WriteBuffer(PassWord,Sizeof(PassWord)); Target.WriteBuffer(PassWordSize,sizeof(PassWordSize)); Finally Target.Free; Source.Free; End; Except Result:=False; Exit; End; Reslut:=True; End; Function Jmf_LoadFromFile(SourceFile,TargetFile:string):Boolean; Var Source:TFileStream; Target:TmemoryStream; MyFilesize,Position:integer; Begin Try Target:=TmemoryStream.Create; Source:=TFileStream(SourceFile,fmOpenRead or fmShareDenyNone); Try Source.Seek(-sizeof(Position),soFromEnd); Source.ReadBuffer(Position,sizeof(Position)); Source.Seek(-Position-sizefo(MyFileSize),soFromEnd); Source.ReadBuffer(MyFileSize,sizeof(MyFileSize));{讀出資源大小} Source.Seek(-Position-MyFileSize,soFromEnd);{定位到資源位置} Target.CopyFrom(Source,MyFileSize-sizefo(MyFileSize));{取出資源} Target.SaveToFile(TargetFile);{存放到文件} finally Target.Free; Source.Free; end; except Result:=false; Exit; end; Result:=true; end; spa Function Jmf_PassFromFile(var password:string;SourceFile:string):Boolean; Var Source:TFileStream; PassWordSize:interger; Begin Try Source:=TFileStream.Create(SourceFile,fmOpenRead or fmShareDenyNone); Try Source.Seek(0,soFromEnd);{定位到資源位置} Source.Read(PassWordSize,Sizeof(PassWordSize)); Source.Seek(-PassWordSize,soFromEnd); Source.Read(PassWord,PassWordSize-Sizeof(PassWordSize)); Finally Source.Free; End; Except Result:=false; Exit; End; Result:=true; End; orm Procedure Tfom1.unbindClick(Sender:TObject); Var S:string; pass,inpass:string; begin inpass:=password.text; S:=ChangeFileExt(Application.ExeName,'Jmf'); If Jmf_PassFromFile(pass,Application.ExeName) then If pass=password.text then Begin Jmf_LordFromFile(Application.ExeName,S);{取出文件保存在當前路徑下並命名"原文件.Jmf"} Winexec(pchar(S),SW_Show);{運行"原文件.Jmf"} Application.Terminate;{退出程序} end else Application.MessageBox('密碼錯誤,從新輸入!','密碼錯誤',MB_OK); end; 事件 Procedure Tform1.openClick(Sender:TObject); begin If OpenDialog1.Execute then position.Text:=OpenDialog1.FileName; end; 資源 procedure Tform1.secretClick(Sender:TObject); var s:string; spass:string; begin s:=ExtractFilePath(position.text); spass:=pass.Text; if ExtractRes('exefile','head',s+'head.exe') then if Jmf_AddtoFile(position.text,s+'head.exe',spass) then if DeleteFile(position.text)then if RenameFile(s+'head.exe',position.text)then Application.MessageBox('文件加密成功!!','信息',MB_OK) Else Begin If FileExists(s+'head.exe') then DeleteFile(S+'head.exe'); Application.MessageBox('文件加密失敗!!','信息',MB_OK) end; 字符串 Function ExtractRes(Restype,Resname,ResNewName:string):Boolean; Var Res:TresourceStream; begin try Res:=TresourceStream.Create(Hinstance,Resname,Pchar(ResType)); try Res.SaveToFile(ResNewName); Result:=true; Finally Res.Free; end; except Result:=False; end; get 大功告成。 原型 OK, string |