Delphi实现U盘加密狗[原创]

效果,只有当指定U盘插入才能打开该程序。该U盘的硬件序列号作为解密的密匙。代码如下:

procedure TForm1.GetDiskKey;
const
     drivers:array[0..25] of string=('A:\','B:\','C:\','D:\','E:\','F:\','G:\','H:\','I:\','J:\','K:\','L:\','M:\','N:\','O:\','P:\','Q:\','R:\','S:\','T:\','U:\','V:\','W:\','X:\','Y:\','Z:\');
var  i,sresult:integer;
     isKey:boolean;
begin
  isKey := false;
for I := 0 to 25 do
  begin
  sresult:=getdrivetype(pchar(drivers[i]));
  if (sresult=drive_removable) then
  begin
    if  (GetDiskID(PWidechar(drivers[i]))='876986194') then
    begin
    isKey := true;
    break;
    end;
  end;
  end;
if not isKey then   Application.Terminate;
end;

其中包含有GetDiskID函数的单元

使用方法,直接在FormCreate或OnCanResize或OnActivate事件中加上如下代码:

Form1.GetDiskKey