lock_main.pas
上传用户:zhkydz
上传日期:2013-04-26
资源大小:44k
文件大小:14k
源码类别:

破解

开发平台:

Asm

  1. unit lock_main;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   TFlatButtonUnit, StdCtrls, TFlatEditUnit, ComCtrls, ShellAPI,
  6.   TFlatCheckBoxUnit, TFlatHintUnit, ExtCtrls;
  7. type
  8.   TForm1 = class(TForm)
  9.     Button_Go: TFlatButton;
  10.     Button_Exit: TFlatButton;
  11.     OpenDialog1: TOpenDialog;
  12.     Edit_Pass: TFlatEdit;
  13.     ProgressBar1: TProgressBar;
  14.     StaticText1: TStaticText;
  15.     Button_OpenFile: TFlatButton;
  16.     StaticText2: TStaticText;
  17.     Edit_Pass1: TFlatEdit;
  18.     Button_About: TFlatButton;
  19.     StaticText_Pass1: TStaticText;
  20.     Edit_FileName: TFlatEdit;
  21.     CheckBox_BackUp: TFlatCheckBox;
  22.     FlatHint1: TFlatHint;
  23.     Button_UnGo: TFlatButton;
  24.     Panel1: TPanel;
  25.     Label_Msg: TLabel;
  26.     Button_Directory: TFlatButton;
  27.     procedure Button_GoClick(Sender: TObject);
  28.     procedure Button_ExitClick(Sender: TObject);
  29.     procedure Button_OpenFileClick(Sender: TObject);
  30.     procedure Button_AboutClick(Sender: TObject);
  31.     procedure Button_UnGoClick(Sender: TObject);
  32.     procedure FormCreate(Sender: TObject);
  33.     procedure Button_DirectoryClick(Sender: TObject);
  34.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  35.     procedure Edit_FileNameChange(Sender: TObject);
  36.   private
  37.     CheckTimes:Byte;
  38.     procedure ResetForm;
  39.     procedure BusyForm;
  40.     procedure DoLock;
  41.     procedure DoUnLock;
  42.     procedure CopyLockedFile(var FromFile, ToFile: String);
  43.     procedure GetPassDialogFile(FileName: String);
  44.     procedure CheckOpFile(FileName: String);
  45.     procedure WMDropFiles(var Msg:TWMDropFiles); message WM_DROPFILES;
  46.     { Private declarations }
  47.   public
  48.     { Public declarations }
  49.   end;
  50. const
  51.   Error_FileNotExists='对不起,选的文件不存在,不能继续。';
  52.   Error_NoPass='对不起,密码不能为空,请输入密码。';
  53.   Error_PassNotSame='两次密码不一致,请检查并重新输入。';
  54.   Error_FileLocked='文件[%s]已经加密,不能继续。';
  55.   Error_FileNotLocked='文件[%s]没有加密,不能继续。';
  56.   Error_Thread='对不起,建立多线程错误,不能继续。';
  57.   Error_PassWrong='密码错误,还有%d次操作机会。';
  58.   Error_FileType='文件[%s]类型不是EXE,加密后可能出错。';
  59.   Error_FileAttribute='文件[%s]属性只读,不能继续。';
  60.   Msg_DoLock='文件[%s]没有加密,可以加密。';
  61.   Msg_DoUnLock='文件[%s]已经加密,可以解密。';
  62.   Msg_Over='密码3次错误,程序将自动退出。';
  63.   Msg_BeginLock='开始加密[%s]文件,请稍后!';
  64.   Msg_BeginUnLock='开始解密[%s]文件,请稍后!';
  65.   Msg_EndLock='文件[%s]加密完成,谢谢使用。';
  66.   Msg_EndUnLock='文件[%s]解密完成,谢谢使用。';
  67.   Msg_BeginBackUpFile='正在备份[%s]文件,请稍后!';
  68. var
  69.   Form1: TForm1;
  70. implementation
  71. uses lock_about;
  72. {$R *.DFM}
  73. {$R 1.RES}
  74. procedure TForm1.ResetForm;
  75. begin
  76.   CheckTimes:=1;
  77.   ProgressBar1.Position:=0;
  78.   Label_Msg.Caption:='信息';
  79.   Edit_Pass.Enabled:=True;
  80.   Edit_Pass1.Enabled:=True;
  81.   Edit_Pass.Text:=#0;
  82.   Edit_Pass1.Text:=#0;
  83.   Button_Go.Enabled:=True;
  84.   Button_UnGo.Enabled:=True;
  85.   Button_Exit.Enabled:=True;
  86.   Edit_FileName.Enabled:=True;
  87.   Button_OpenFile.Enabled:=True;
  88.   Button_Directory.Enabled:=True;
  89.   CheckBox_BackUp.Enabled:=True;
  90.   StaticText_Pass1.Enabled:=True;
  91. end;
  92. procedure TForm1.BusyForm;
  93. begin
  94.   Edit_Pass.Enabled:=False;
  95.   Edit_Pass1.Enabled:=False;
  96.   Button_Go.Enabled:=False;
  97.   Button_UnGo.Enabled:=False;
  98.   Button_Exit.Enabled:=False;
  99.   Edit_FileName.Enabled:=False;
  100.   Button_OpenFile.Enabled:=False;
  101.   Button_Directory.Enabled:=False;
  102. end;
  103. procedure TForm1.CopyLockedFile(var FromFile,ToFile:String);
  104. var
  105.   OpStruc:TSHFileOpStruct;
  106.   FromBuf,ToBuf:packed array[0..2047]of char;
  107. begin
  108.   fillchar(frombuf,sizeof(frombuf),0);
  109.   fillchar(tobuf,sizeof(tobuf),0);
  110.   StrpCopy(frombuf,fromfile);
  111.   StrpCopy(tobuf,tofile);
  112.   with OpStruc do
  113.   begin
  114.     wnd:=handle;
  115.     wFunc:=FO_COPY;
  116.     pfrom:=@frombuf;
  117.     pto:=@tobuf;
  118.     fFlags:=FOF_SILENT or FOF_NOCONFIRMATION;
  119.     fAnyOperationsAborted:=false;
  120.     hNameMappings:=nil;
  121.     lpszProgressTitle:=nil;
  122.   end;
  123.   ShFileOperation(OpStruc);
  124. end;
  125. procedure TForm1.GetPassDialogFile(FileName:String);
  126. var
  127.   ExeRes:TResourceStream;
  128. begin
  129.   ExeRes:=TResourceStream.Create(Hinstance,'PassDialogFile','EXEFILE');
  130.   ExeRes.SavetoFile(FileName);
  131.   ExeRes.Free;
  132. end;
  133. function LockFile(P:pointer):Longint;stdcall;
  134.   function jjm(S:String):String;
  135.   var
  136.     i:Byte;
  137.   begin
  138.     for i:=1 to Length(S) do
  139.       S[i]:=Char(ord(S[i]) xor (i+3));
  140.     jjm:=S;
  141.   end;
  142. var
  143.   FsName,FtName,FbName:String;
  144.   iTargetFile,iSourceFile:Integer;
  145.   GetFlag,Flag:String[7];
  146.   MyBuf:packed array[0..2047]of Char;
  147.   NumRead,NumWritten:Integer;
  148.   LockedFile:record
  149.     Name:ShortString;
  150.     Size:Integer;
  151.     PassWord:String[15];
  152.     Encrypted:Boolean;
  153.   end;
  154.   Successed:Boolean;
  155. begin
  156.   Flag:='@@#%#@@';
  157.   Successed:=False;
  158.   with Form1 do
  159.   begin
  160.     BusyForm;
  161.     FsName:=Edit_FileName.Text;
  162.     FbName:=FsName+'.BAK';
  163.     try
  164.       iSourceFile:=FileOpen(FsName,fmOpenRead or fmShareDenyNone);
  165.       { get flag from file which will be encrypted }
  166.       FileSeek(iSourceFile,-SizeOf(Flag),soFromEnd);
  167.       FileRead(iSourceFile,GetFlag,SizeOf(GetFlag));
  168.       if GetFlag<>Flag then
  169.       begin
  170.         { check whether backup file }
  171.         if CheckBox_BackUp.Checked then
  172.         begin
  173.           Label_Msg.Caption:=Format(Msg_BeginBackUpFile,[ExtractFileName(FsName)]);
  174.           CopyLockedFile(FsName,FbName);
  175.         end;
  176.         { begin lock }
  177.         with LockedFile do
  178.         begin
  179.           Name:=ExtractFileName(FsName);
  180.           Size:=FileSeek(iSourceFile,0,soFromEnd)+SizeOf(LockedFile);
  181.           PassWord:=jjm(Edit_Pass.Text);
  182.           Encrypted:=False;
  183.         end;
  184.         { get passdialogfile to currect directory }
  185.         FtName:=ExtractFilePath(FsName)+'_'+LockedFile.Name;
  186.         GetPassDialogFile(FtName);
  187.         { set progressbar }
  188.         ProgressBar1.Max:=LockedFile.Size div SizeOf(MyBuf);
  189.         ProgressBar1.Position:=0;
  190.         Label_Msg.Caption:=Format(Msg_BeginLock,[LockedFile.Name]);
  191.         { begin copy }
  192.         iTargetFile:=FileOpen(FtName,fmOpenReadWrite);
  193.         FileSeek(iSourceFile,0,soFromBeginning);
  194.         FileSeek(iTargetFile,0,soFromEnd);
  195.         repeat
  196.           ProgressBar1.Position:=ProgressBar1.Position+1;
  197.           NumRead:=FileRead(iSourceFile,MyBuf,SizeOf(MyBuf));
  198.           NumWritten:=FileWrite(iTargetFile,MyBuf,NumRead);
  199.         until (NumRead=0) or (NumWritten<>NumRead);
  200.         FileWrite(iTargetFile,LockedFile,SizeOf(LockedFile));
  201.         FileWrite(iTargetFile,Flag,SizeOf(Flag));
  202.         Successed:=True;
  203.         { end copy }
  204.         Label_Msg.Caption:=Format(Msg_EndLock,[LockedFile.Name]);
  205.       end else
  206.       begin
  207.         Label_Msg.Caption:=Format(Error_FileLocked,[ExtractFileName(FsName)]);
  208.       end;
  209.     finally
  210.       FileClose(iSourceFile);
  211.       FileClose(iTargetFile);
  212.     end;
  213.     if Successed then
  214.     begin
  215.       DeleteFile(FsName);
  216.       RenameFile(FtName,FsName);
  217.     end;
  218.     Sleep(1000);
  219.     CheckOpFile(Edit_FileName.Text);
  220.   end;
  221. end;
  222. function UnLockFile(P:pointer):Longint;stdcall;
  223.   function jjm(S:String):String;
  224.   var
  225.     i:Byte;
  226.   begin
  227.     for i:=1 to Length(S) do
  228.       S[i]:=Char(ord(S[i]) xor (i+3));
  229.     jjm:=S;
  230.   end;
  231. var
  232.   FsName,FtName:String;
  233.   iSourceFile,iTargetFile:Integer;
  234.   NumRead,NumWritten:Integer;
  235.   MyBuf:packed array[0..2047]of Char;
  236.   GetFlag,Flag:String[7];
  237.   LockedFile:record
  238.     Name:ShortString;
  239.     Size:Integer;
  240.     PassWord:String[15];
  241.     Encrypted:Boolean;
  242.   end;
  243.   FileStart,FileEnd:Integer;
  244.   Successed:Boolean;
  245. begin
  246.   Flag:='@@#%#@@';
  247.   Successed:=False;
  248.   with Form1 do
  249.   begin
  250.     BusyForm;
  251.     FsName:=Edit_FileName.Text;
  252.     try
  253.       iSourceFile:=FileOpen(FsName,fmOpenRead or fmShareDenyNone);
  254.       FileSeek(iSourceFile,-SizeOf(Flag),soFromEnd);
  255.       FileRead(iSourceFile,GetFlag,SizeOf(GetFlag));
  256.       if GetFlag=Flag then
  257.       begin
  258.         FileSeek(iSourceFile,-Sizeof(LockedFile)-SizeOf(Flag),soFromEnd);
  259.         FileRead(iSourceFile,LockedFile,SizeOf(LockedFile));
  260.         if LockedFile.PassWord=jjm(Edit_Pass.Text) then
  261.         begin
  262.           { set progressbar }
  263.           ProgressBar1.Max:=LockedFile.Size div SizeOf(MyBuf);
  264.           ProgressBar1.Position:=0;
  265.           Label_Msg.Caption:=Format(Msg_BeginUnLock,[LockedFile.Name]);
  266.           FileStart:=LockedFile.Size+SizeOf(Flag);
  267.           FileEnd:=SizeOf(LockedFile)+SizeOf(Flag);
  268.           try
  269.             FtName:=ExtractFilePath(FsName)+'_'+LockedFile.Name;
  270.             iTargetFile:=FileCreate(FtName);
  271.             FileSeek(iSourceFile,-FileStart,soFromEnd);
  272.             repeat
  273.               ProgressBar1.Position:=ProgressBar1.Position+1;
  274.               NumRead:=FileRead(iSourceFile,MyBuf,SizeOf(MyBuf));
  275.               if NumRead=SizeOf(MyBuf)then
  276.                 NumWritten:=FileWrite(iTargetFile,MyBuf,NumRead) else
  277.                 NumWritten:=FileWrite(iTargetFile,MyBuf,NumRead-FileEnd);
  278.             until (NumRead=0) or (NumWritten<>NumRead);
  279.             Successed:=True;
  280.             Label_Msg.Caption:=Format(Msg_EndUnLock,[LockedFile.Name]);
  281.           finally
  282.             FileClose(iTargetFile);
  283.           end;
  284.         end else
  285.         begin
  286.           inc(CheckTimes);
  287.           Label_Msg.Caption:=Format(Error_PassWrong,[4-CheckTimes]);
  288.         end;
  289.       end else Label_Msg.Caption:=Format(Error_FileNotLocked,[ExtractFileName(FsName)]);
  290.     finally
  291.       FileClose(iSourceFile);
  292.     end;
  293.     if Successed then
  294.     begin
  295.       DeleteFile(FsName);
  296.       RenameFile(FtName,FsName);
  297.     end;
  298.     Sleep(1000);
  299.     CheckOpFile(Edit_FileName.Text);
  300.   end;
  301. end;
  302. procedure TForm1.Button_GoClick(Sender: TObject);
  303. var
  304.   hThread:Thandle;
  305.   ThreadID:DWord;
  306. begin
  307.   if not FileExists(Edit_FileName.Text) then
  308.   begin
  309.     Label_Msg.Caption:=Error_FileNotExists;
  310.     exit;
  311.   end;
  312.   if Edit_Pass.Text='' then
  313.   begin
  314.     Label_Msg.Caption:=Error_NoPass;
  315.     exit;
  316.   end;
  317.   if Edit_Pass.Text<>Edit_Pass1.Text then
  318.   begin
  319.     Label_Msg.Caption:=Error_PassNotSame;
  320.     exit;
  321.   end;
  322.   hThread:=CreateThread(nil,0,@LockFile,nil,0,ThreadID);
  323.   if hThread=0 then Label_Msg.Caption:=Error_Thread;
  324. end;
  325. procedure TForm1.Button_ExitClick(Sender: TObject);
  326. begin
  327.   Close;
  328. end;
  329. procedure TForm1.Button_OpenFileClick(Sender: TObject);
  330. begin
  331.   if OpenDialog1.Execute then
  332.   begin
  333.     Edit_FileName.Text:=OpenDialog1.FileName;
  334.     CheckOpFile(Edit_FileName.Text);
  335.   end;
  336. end;
  337. procedure TForm1.Button_AboutClick(Sender: TObject);
  338. begin
  339.   Application.CreateForm(Tfrm_about, frm_about);
  340.   frm_about.ShowModal;
  341. end;
  342. procedure TForm1.Button_UnGoClick(Sender: TObject);
  343. var
  344.   hThread:THandle;
  345.   ThreadID:DWord;
  346. begin
  347.   if not FileExists(Edit_FileName.Text) then
  348.   begin
  349.     Label_Msg.Caption:=Error_FileNotExists;
  350.     exit;
  351.   end;
  352.   if Edit_Pass.Text='' then
  353.   begin
  354.     Label_Msg.Caption:=Error_NoPass;
  355.     exit;
  356.   end;
  357.   if CheckTimes>=3 then
  358.   begin
  359.     Close;
  360.   end else
  361.   begin
  362.     hThread:=CreateThread(nil,0,@UnLockFile,nil,0,ThreadID);
  363.     if hThread=0 then Label_Msg.Caption:=Error_Thread;
  364.   end;
  365. end;
  366. procedure TForm1.FormCreate(Sender: TObject);
  367. begin
  368.   { enable dragfile }
  369.   DragAcceptFiles(Handle, True);
  370.   CheckTimes:=1;
  371. end;
  372. procedure TForm1.Button_DirectoryClick(Sender: TObject);
  373. var
  374.   SDirectory:String;
  375. begin
  376.   if not FileExists(Edit_FileName.Text) then
  377.   begin
  378.     Label_Msg.Caption:=Error_FileNotExists;
  379.     exit;
  380.   end;
  381.   SDirectory:=ExtractFilePath(Edit_FileName.Text);
  382.   ShellExecute(Handle,nil,PChar(SDirectory),nil,nil,SW_SHOWNORMAL);
  383. end;
  384. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  385. begin
  386.   if not Button_Exit.Enabled then CanClose:=False;
  387. end;
  388. procedure TForm1.DoLock;
  389. var
  390.   DoFileName:String;
  391. begin
  392.   DoFileName:=ExtractFileName(Edit_FileName.Text);
  393.   Label_Msg.Caption:=Format(Msg_DoLock,[DoFileName]);
  394.   Button_UnGo.Enabled:=False;
  395. end;
  396. procedure TForm1.DoUnLock;
  397. var
  398.   DoFileName:String;
  399. begin
  400.   DoFileName:=ExtractFileName(Edit_FileName.Text);
  401.   Label_Msg.Caption:=Format(Msg_DoUnLock,[DoFileName]);
  402.   Button_Go.Enabled:=False;
  403.   Edit_Pass1.Enabled:=False;
  404.   CheckBox_BackUp.Enabled:=False;
  405.   StaticText_Pass1.Enabled:=False;
  406. end;
  407. procedure TForm1.CheckOpFile(FileName:String);
  408. var
  409.   iOpFile:Integer;
  410.   GetFlag,Flag:String[7];
  411.   LockedFile:record
  412.     Name:ShortString;
  413.     Size:Integer;
  414.     PassWord:String[15];
  415.     Encrypted:Boolean;
  416.   end;
  417.   FileExt:String;
  418.   FileAttr:Integer;
  419. begin
  420.   ResetForm;
  421.   Flag:='@@#%#@@';
  422.   FileExt:=ExtractFileExt(FileName);
  423.   if StrUpper(PChar(FileExt))<>'.EXE' then
  424.   begin
  425.     label_Msg.Caption:=Format(Error_FileType,[ExtractFileName(FileName)]);
  426.     Sleep(1000);
  427.   end;
  428.   FileAttr:=FileGetAttr(FileName);
  429.   if FileAttr and faReadOnly>0 then
  430.   begin
  431.     label_Msg.Caption:=Format(Error_FileAttribute,[ExtractFileName(FileName)]);
  432.     Label_Msg.Hint:=Label_Msg.Caption;
  433.     exit;
  434.   end;
  435.   try
  436.     iOpFile:=FileOpen(FileName,fmOpenRead);
  437.     FileSeek(iOpFile,-SizeOf(Flag),soFromEnd);
  438.     FileRead(iOpFile,GetFlag,SizeOf(GetFlag));
  439.     if GetFlag=Flag then
  440.     begin
  441.       DoUnLock;
  442.     end else
  443.     begin
  444.       DoLock;
  445.     end;
  446.     Label_Msg.Hint:=Label_Msg.Caption;
  447.   finally
  448.     FileClose(iOpFile);
  449.   end;
  450. end;
  451. procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);
  452. var
  453.   CFileName: array[0..MAX_PATH] of Char;
  454. begin
  455.   try
  456.     if DragQueryFile(Msg.Drop,0,CFileName,MAX_PATH)>0 then
  457.     begin
  458.       Edit_FileName.Text:=CFileName;
  459.       CheckOpFile(CFileName);
  460.     end;
  461.   finally
  462.     DragFinish(Msg.Drop);
  463.   end;
  464. end;
  465. procedure TForm1.Edit_FileNameChange(Sender: TObject);
  466. begin
  467.   Edit_FileName.Hint:=Edit_FileName.Text;
  468. end;
  469. end.