Access数据库加密表头,让Access不那么脆弱

 

 

 #include <vcl.h>
#pragma hdrstop

#include "FrmAccessHeard.h"
#include   <Registry.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
const
   Byte titlestr[16] =
{0x00,0x01,0x00,0x00,0x53,0x74,0x61,0x6E,0x64,0x61,0x72,0x64,0x20,0x4A,0x65,0x74} ;//对应MDB文件的前16个字节
   Byte titlestr2[16] =
{0x48,0x4A,0x00,0x58,0x55,0x43,0x48,0x41,0x4E,0x47,0x59,0x4F,0x55,0x00,0x20,0x20} ;//更改后的MDB文件的前16个字节
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    ADOConnection1->Connected=false;
    AnsiString DBName,TempName;
    char CDBName[100];
    char CTempName[100];
    DBName=ExtractFilePath(Application->ExeName)+"Message.db";
    TempName=ExtractFilePath(Application->ExeName)+"Temp.db";
    strcpy(CDBName,DBName.c_str());
    strcpy(CTempName,TempName.c_str());
    CopyFile(CDBName,CTempName,false);
    uncrypMDB(CTempName);
    CopyFile(CTempName,CDBName,false);
    ADOConnection1->ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source='"+DBName+"';Persist Security Info=False;Jet OLEDB:Database Password=123456";
    ADOConnection1->Connected=true;
    CopyFile(CDBName,CTempName,false);
    EncrypMDB(CTempName);
    CopyFile(CTempName,CDBName,false);
    DeleteFile(CTempName);
    TRegistry   *reg = new TRegistry();
    try
    {
        reg->RootKey =HKEY_CLASSES_ROOT;
        reg->OpenKey(".ldb",true);
        reg->WriteString("","tempdb");
    }
    __finally
    {
        reg->CloseKey();
        reg->Free();
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EncrypMDB(String filename)
{
   TFileStream *F;

   if (!FileExists(filename))
       return;
   F = new TFileStream(filename,fmOpenWrite);
   try
   {
       F->Seek(0x00,soFromBeginning);
       F->Write(titlestr2,16);
   }
   __finally
   {
       F->Free();
   }

}
void __fastcall TForm1::uncrypMDB(String filename) //还原MDB前16个字节
{
  TFileStream *F;

   if (!FileExists(filename))
       return;
   F = new TFileStream(filename,fmOpenWrite);
   try
   {
       F->Seek(0x00,soFromBeginning);
       F->Write(titlestr,16);
   }
   __finally
   {
       F->Free();
   }
}

由Delphi代码翻译而来,经过些加工处理。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值