{-----------------------------------------------------------------------------
Procedure: LockupFile
Author: ysai
Date: 2003-6-25
Arguments: FileName:string;Lock:boolean=true
Result: integer
-----------------------------------------------------------------------------}
function LockupFile(FileName:string;Lock:boolean=true):integer;
//加密、解密数据库,针对 Access 2000
var
f:File;
bf:array[0..63] of Byte;
i:integer;
const
fpos=64;
flen=64;
//下面改为自己的密钥,我是用随机生成的,请改为自己的密钥
pw:array[0..63] of byte=
($97,$A0,$0C,$A1,$06,$59,$0A,$6D,
$91,$33,$51,$57,$D4,$A3,$94,$16,
$3D,$B2,$C7,$A0,$7C,$A3,$30,$EE,
$34,$D6,$C1,$FF,$F7,$EC,$A5,$1F,
$71,$2C,$19,$69,$E3,$25,$7D,$8B,
$D3,$95,$AB,$C9,$02,$8A,$87,$44,
$9F,$C7,$D7,$7D,$BA,$69,$56,$15,
$FB,$CB,$03,$D6,$94,$A6,$BF,$F7);
begin
result:=-1;
if not FileExists(FileName) then exit;
try
AssignFile(f,Filename);
Reset(f,1);
Seek(f,fpos);
BlockRead(f,bf,flen);
//下面的代码是判断是否被加密,你可以用二进制编辑器打开MDB文件对比,
//我是用第64,65字节作为是否加密的标记,未加密与版本相关,加密后与版本和密钥相关
if lock and (bf[0]=$2B) and (bf[1]=$EE)
or not lock and (bf[0]=$BC) and (bf[1]=$4E)
or not ((bf[0]=$2B) and (bf[1]=$EE))
and not ((bf[0]=$BC) and (bf[1]=$4E)) then
begin
result:=0;
exit;
end;
for i:=0 to flen-1 do
bf[i]:=bf[i] xor pw[i mod 64];
Seek(f,fpos);
BlockWrite(f,bf,flen);
result:=1;
finally
CloseFile(f);
end;
end;
const
//对应MDB文件的前16个字节
titlestr:array[0..15] of byte=($00,$01,$00,$00,$53,$74,$61,$6E,$64,$61,$72,$64,$20,$4A,$65,$74);
//更改后的MDB文件的前16个字节,自己随便写吧,比如写上自己公司的简称或自已的名
titlestr2:array[0..15] of byte=($48,$4A,$00,$58,$55,$43,$48,$41,$4E,$47,$59,$4F,$55,$00,$20,$20);
produce 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;
produce uncrypMDB(filename:string); //还原MDB前16个字节
var
F:TFileStream;
begin
if not fileExists(filename) then exit;
F:=TFileStream.create(filename,fmopenwrite);