Microsoft的ACCESS數(shù)據(jù)庫,是我們常用的桌面數(shù)據(jù)之一,大多中小企業(yè)的數(shù)據(jù)庫管理系統(tǒng)都可以采用它,但其安全性一直令人擔猶,試想,一套財務管理系統(tǒng),用戶直接打開數(shù)據(jù)庫去更改數(shù)據(jù),后果會如何?有些系統(tǒng)對ACCESS數(shù)據(jù)庫可能只是更改擴展名,或加個密碼,眾所周知,*ACCESS密碼的方法和工具網(wǎng)上多的是!所以這樣的加密一樣令人擔猶,下面介紹一個簡單的方法,實現(xiàn)ACCESS數(shù)據(jù)的加密,供大家參考。
用UltraEdit打開MDB文件可以看到,文件前16個字節(jié)的內(nèi)容:
00 01 00 00 53 74 61 6E 64 61 72 64 20 4A 65 74
現(xiàn)在隨便更改幾個,再用ACCESS打開,發(fā)現(xiàn)出現(xiàn)不同識別的文件格式錯誤,因為ACCESS前面保存的信息都是一些MDB文件的定義和口令,如果更改這些內(nèi)容,別人就很難看出這個數(shù)據(jù)庫的格式,無法打開它了,而且這樣不會對數(shù)據(jù)庫的內(nèi)容作更改,不會破壞原有的數(shù)據(jù)。
下面就用Delphi作個簡單的加密解程序:
用到的加密解函數(shù)如下:
const
titlestr:array[0..15] of byte=
($00,$01,$00,$00,$53,$74,$61,$6E,$64,$61,$72,$64,$20,$4A,$65,$74) ;//對應MDB文件的前16個字節(jié)
titlestr2:array[0..15] of byte=
($48,$4A,$00,$58,$55,$43,$48,$41,$4E,$47,$59,$4F,$55,$00,$20,$20) ;//更改后的MDB文件的前16個字節(jié),自己隨便寫吧,如寫上自己公司的簡稱或自已的名
produce EncrypMDB(filename:string); //用titlestr2內(nèi)容替換MDB前16個字節(jié),以便實現(xiàn)加密的作用
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;
produce uncrypMDB(filename:string); //還原MDB前16個字節(jié)
var F:TFileStream;
begin
if not fileExists(filename) then exit;
F:=TFileStream.create(filename,fmopenwrite);
try
F.seek($00,soFromBeginning);
F.Write(titlestr,16);
finally
F.free;
end;
end;
我們知道打開ACCESS數(shù)據(jù)庫后會出現(xiàn)一個鎖定文件(.ldb文件),因為我們自己也要使用數(shù)據(jù)庫,所以必須在使用時還原數(shù)據(jù)庫。
如果還原后沒有進行加密的話,用戶同樣可以復制MDB文件,然后用ACCESS或其它工具打開它,所以應該在數(shù)據(jù)打開前后都處于加密狀態(tài)才能保證數(shù)據(jù)的安全。
用Delphi采用ADO連接數(shù)據(jù)庫用以下方法可以實現(xiàn):
//還原數(shù)據(jù),以便自已使用數(shù)據(jù)庫
copyfile(pchar(APP_path+'\data\account.db'),pchar(app_path+'data\temp.db'),false); //app_path表示程序的當前目錄,account.db是個更改了擴展名的MDB文件
uncrypMDB(App_path+'data\temp.db');
copyfile(pchar(App_path+'data\temp.db'),pchar(APP_path+'\data\account.db'),false);
adoconn.connectionstring:='provider=Microsoft.Jet.OLEDB.4.0;Data Source='+App_path+'data\account.db;Persist Security Info=false'; //adocon是個TADOConnection組件
try
adoconn.connected:=true;
except
MessageBox(handle,'打開數(shù)據(jù)庫出現(xiàn)致命的錯誤!??!','錯誤',MB_OK+MB_ICONERROR);
end;
//打開后馬上對其加密
copyfile(pchar(APP_path+'\data\account.db'),pchar(app_path+'data\temp.db'),false); //app_path表示程序的當前目錄,account.db是個更改了擴展名的MDB文件
EncrypMDB(App_path+'data\temp.db');
copyfile(pchar(App_path+'data\temp.db'),pchar(APP_path+'\data\account.db'),false);
deletefile(App_path+'data\temp.db');
上面使用了兩次臨時文件,是因為數(shù)據(jù)庫打開后再對MDB進行直接的寫入會出現(xiàn)問題,而且你無法去確定多少個用戶打開了程序。
整個程序共用一個TADOConnection,只在打開數(shù)據(jù)庫連接的時候還原MDB文件,其它時間MDB文件一直都處于加密狀態(tài)!用戶復制了MDB文件一般很難知道它是什么!
打開數(shù)據(jù)庫后會有一個.ldb文件,類型會出現(xiàn)ACCESS等字樣,如果你不想讓人看出是什么的話就修改注冊表吧,如:
reg:=TRegistry.Create;
try
reg.RootKey:=HKEY_CLASSES_ROOT;
reg.OpenKey('.ldb');
reg.WriteString('','tempfile');
finally
reg.closekey;
reg.free;
end;
這樣用戶看到的文件類型是tempfile
用UltraEdit打開MDB文件可以看到,文件前16個字節(jié)的內(nèi)容:
00 01 00 00 53 74 61 6E 64 61 72 64 20 4A 65 74
現(xiàn)在隨便更改幾個,再用ACCESS打開,發(fā)現(xiàn)出現(xiàn)不同識別的文件格式錯誤,因為ACCESS前面保存的信息都是一些MDB文件的定義和口令,如果更改這些內(nèi)容,別人就很難看出這個數(shù)據(jù)庫的格式,無法打開它了,而且這樣不會對數(shù)據(jù)庫的內(nèi)容作更改,不會破壞原有的數(shù)據(jù)。
下面就用Delphi作個簡單的加密解程序:
用到的加密解函數(shù)如下:
const
titlestr:array[0..15] of byte=
($00,$01,$00,$00,$53,$74,$61,$6E,$64,$61,$72,$64,$20,$4A,$65,$74) ;//對應MDB文件的前16個字節(jié)
titlestr2:array[0..15] of byte=
($48,$4A,$00,$58,$55,$43,$48,$41,$4E,$47,$59,$4F,$55,$00,$20,$20) ;//更改后的MDB文件的前16個字節(jié),自己隨便寫吧,如寫上自己公司的簡稱或自已的名
produce EncrypMDB(filename:string); //用titlestr2內(nèi)容替換MDB前16個字節(jié),以便實現(xiàn)加密的作用
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;
produce uncrypMDB(filename:string); //還原MDB前16個字節(jié)
var F:TFileStream;
begin
if not fileExists(filename) then exit;
F:=TFileStream.create(filename,fmopenwrite);
try
F.seek($00,soFromBeginning);
F.Write(titlestr,16);
finally
F.free;
end;
end;
我們知道打開ACCESS數(shù)據(jù)庫后會出現(xiàn)一個鎖定文件(.ldb文件),因為我們自己也要使用數(shù)據(jù)庫,所以必須在使用時還原數(shù)據(jù)庫。
如果還原后沒有進行加密的話,用戶同樣可以復制MDB文件,然后用ACCESS或其它工具打開它,所以應該在數(shù)據(jù)打開前后都處于加密狀態(tài)才能保證數(shù)據(jù)的安全。
用Delphi采用ADO連接數(shù)據(jù)庫用以下方法可以實現(xiàn):
//還原數(shù)據(jù),以便自已使用數(shù)據(jù)庫
copyfile(pchar(APP_path+'\data\account.db'),pchar(app_path+'data\temp.db'),false); //app_path表示程序的當前目錄,account.db是個更改了擴展名的MDB文件
uncrypMDB(App_path+'data\temp.db');
copyfile(pchar(App_path+'data\temp.db'),pchar(APP_path+'\data\account.db'),false);
adoconn.connectionstring:='provider=Microsoft.Jet.OLEDB.4.0;Data Source='+App_path+'data\account.db;Persist Security Info=false'; //adocon是個TADOConnection組件
try
adoconn.connected:=true;
except
MessageBox(handle,'打開數(shù)據(jù)庫出現(xiàn)致命的錯誤!??!','錯誤',MB_OK+MB_ICONERROR);
end;
//打開后馬上對其加密
copyfile(pchar(APP_path+'\data\account.db'),pchar(app_path+'data\temp.db'),false); //app_path表示程序的當前目錄,account.db是個更改了擴展名的MDB文件
EncrypMDB(App_path+'data\temp.db');
copyfile(pchar(App_path+'data\temp.db'),pchar(APP_path+'\data\account.db'),false);
deletefile(App_path+'data\temp.db');
上面使用了兩次臨時文件,是因為數(shù)據(jù)庫打開后再對MDB進行直接的寫入會出現(xiàn)問題,而且你無法去確定多少個用戶打開了程序。
整個程序共用一個TADOConnection,只在打開數(shù)據(jù)庫連接的時候還原MDB文件,其它時間MDB文件一直都處于加密狀態(tài)!用戶復制了MDB文件一般很難知道它是什么!
打開數(shù)據(jù)庫后會有一個.ldb文件,類型會出現(xiàn)ACCESS等字樣,如果你不想讓人看出是什么的話就修改注冊表吧,如:
reg:=TRegistry.Create;
try
reg.RootKey:=HKEY_CLASSES_ROOT;
reg.OpenKey('.ldb');
reg.WriteString('','tempfile');
finally
reg.closekey;
reg.free;
end;
這樣用戶看到的文件類型是tempfile

