procedure  tform1.EncrypMDB(filename:string);  //用titlestr2内容替换MDB前16个字节,以便实现加密的作用  
var  F:TFileStream;  
begin  
     if  not  fileExists(filename)  then  exit;  
         F:=TFileStream.create(filename,fmopenwrite);  
         try  
             F.seek($00,soFromBeginning);  
             F.Write(titlestr2,16);  
         finally  
             F.free;  
         end;  
end;  
procedure  tform1.uncrypMDB(filename:string);  //还原MDB前16个字节  
var  F:TFileStream;    //文件流  
begin  
     if  not  fileExists(filename)  then  exit;  
         F:=TFileStream.create(filename,fmopenwrite);  
         try  
             F.seek($00,soFromBeginning);  
             //Seek(Offset:  Longint;  Origin:  Word)搜索  
             F.Write(titlestr,16);  
             //Write(const  Buffer;  Count:  Longint)  
         finally  
             F.free;  
         end;  
end;  
 
procedure  TForm1.FormShow(Sender:  TObject);  
var  path:string;  
begin  
//button5.Click;  
//还原数据,以便自已使用数据库  
   {copyfile(pchar(path+'.\db\db1.mdb'),pchar(path+'.\db\temp.mdb'),false);  
   //path表示程序的当前目录    CopyFile(LPCTSTR  lpszExistingFile,  LPCTSTR  lpszNewFile,  BOOL  fFailIfExists)  
   uncrypMDB(path+'.\db\temp.mdb');  
   copyfile(pchar(path+'.\db\temp.mdb'),pchar(path+'.\db\db1.mdb'),false);    }  
   datamodule22.ADOConnection1.ConnectionString:='provider=Microsoft.Jet.OLEDB.4.0;Data  Source='+path+'.\db\db1.mdb;Persist  Security  Info=false';  
 
   try  
         datamodule22.ADOConnection1.Connected:=true;  
   except  
         MessageBox(handle,'打开数据库出现致命的错误!!!','错误',MB_OK+MB_ICONERROR);  
   end;                    
     //打开后马上对其加密  
   copyfile(pchar(path+'.\db\mdb1.mdb'),pchar(path+'.\db\temp.mdb'),false);  
     //path表示程序的当前目录  
   EncrypMDB(path+'.\db\temp.mdb');  
   copyfile(pchar(path+'.\db\temp.mdb'),pchar(path+'.\db\db1.mdb'),false);  
   deletefile(path+'.\db\temp.mdb');  
end;  
在破坏ACCESS数据库文件头的时候,为什么运行第一可以,在运行第二次的时候,提示"无法识别数据库"呢?

解决方案 »

  1.   

    是不是需要先处理数据库文件,然后再进行数据库的连接啊。
    我也不懂只是猜测,我现在也是遇到同样的问题。
    欢迎大家赐教
    http://community.csdn.net/Expert/topic/4835/4835294.xml?temp=.177746
      

  2.   

    读出来
    var
        F:TFileStream;
        passBuf:array[0..49]of byte;
        I:INTEGER;
    begin
    memo1.Clear;
       if not FileExists(Edit1.Text) then exit;
       F:=TFileStream.Create(Edit1.Text,fmOpenRead);
       try
         F.Seek($42,soFromBeginning);
         F.Read(passBuf,50);
       finally
         F.Free;
       end;
    for i:=0 to 49 do
        memo1.lines.add(VarToStr(inttohex(passBuf[i],2)));
    end;
    再写进去的问题
    将上面的read改为write,
       F:=TFileStream.Create(Edit1.Text,fmOpenwrite); 没有只读属性
    没有被其他程序占用等
    const
        passBuf:array[0..39]of byte=
          (
    $29,$77,$EC,$37,$F2,$C8,$9C,$FA,$69,$D2
    ,$28,$E6,$BC,$3A,$8A,$60,$FB,$18,$7B,$36
    ,$5A,$FE,$DF,$B1,$D8,$78,$13,$43,$60,$23
    ,$B1,$33,$9B,$ED,$79,$5B,$3D,$39,$7C,$2A
          );
    var
        F:TFileStream;
    begin
       if not FileExists(Edit1.Text) then exit;
       F:=TFileStream.Create(Edit1.Text,fmOpenWrite);
       try
         F.Seek($42,soFromBeginning);
         F.Write(passBuf,40);
       finally
         F.Free;
       end;
    end; 
    //edit1是文件路径