1

我使用 TurboPower onGuard 进行许可,并且有一个选项可以指定和生成特定于机器的密钥以进行许可验证。使用以下代码,我创建基于 machineID 的机器修饰符

const
    Ckey :TKey = ($56,$1B,$B0,$48,$2D,$AF,$F4,$E5,$30,$6C,$E5,$3B,$A7,$8E,$1A,$14);

procedure TForm1.FormCreate(Sender: TObject);
var
  InfoSet : TEsMachineInfoSet;
  MachineID : Longint;
begin
  { initialize the machine information set }
  InfoSet := [midUser, midSystem, midNetwork, midDrives];
  { create the machine ID and display in hex }
  try
  MachineID := CreateMachineID(InfoSet);
  Edit1.Text := '$' + BufferToHex(MachineID, SizeOf(MachineID));
  except on E:Exception do
   ShowMessage(E.Message);
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
   modifier :LongInt;
   key :TKey;
   releasecode,unlockcode :TCode;
   inexpirydate: TdateTime;
begin
  modifier := GenerateMachineModifierPrim ;
  ApplyModifierToKeyPrim(Modifier, CKey, SizeOf(CKey));

  //HexToBuffer(Ckey, key, SizeOf(TKey));
  InitRegCode(Ckey, edit1.Text, inExpiryDate, releaseCode);
  Edit2.text :=  BufferToHex(unlockCode, SizeOf(releaseCode));
end;    

另一方面,为了验证序列号和发布代码,我在客户端使用以下代码来验证应用程序,我放置了 SerialNumberCode 组件来实现它。

 Ckey :TKey = ($56,$1B,$B0,$48,$2D,$AF,$F4,$E5,$30,$6C,$E5,$3B,$A7,$8E,$1A,$14);

procedure TForm1.OgSerialNumberCode1GetKey(Sender: TObject; var Key: TKey);
begin
  Key := Ckey;
end;

procedure TForm1.OgSerialNumberCode1GetModifier(Sender: TObject;
  var Value: LongInt);
begin
 Value := GenerateMachineModifierPrim;

end;

function TForm1.GetSNData(var S: string): integer;
var
  TC  : TCode;
  SNC : string;
  modifier :LOngInt;
begin
  try
    modifier := GenerateMachineModifierPrim;
    ApplyModifierToKeyPrim(Modifier, CKey, SizeOf(CKey));

    IniSNVal := StrToInt(InputBox('Serial number', 'Enter serial Value ', '12345678'));
    SNC :=  (InputBox('release code', 'Enter code ', ''));
    {Check that Release Code was entered correctly}
    HexToBuffer(SNC, TC, SizeOf(TCode));
    if not (IsSerialNumberCodeValid(CKey, TC)) then begin
      S := 'Release code not entered correctly';
      Result := mrCancel;
    end else begin
      IniFile := TIniFile.Create(TheDir + 'test.INI');
      try
        IniFile.WriteInteger('Codes', 'SN', IniSNVal);
        IniFile.WriteString('Codes', 'SNCode', SNC);
      finally
        IniFile.Free;
      end;
    end;
  finally
  end;

end;

procedure TForm1.OgSerialNumberCode1GetCode(Sender: TObject; var Code: TCode);
var
  S1  : string;
  L   : integer;
begin
     if not (FileExists(TheDir + 'test.INI')) then
    Exit;

  {open Ini File}
  IniFile := TIniFile.Create(TheDir + 'test.INI');
  try
    {try to read release code}
    S1 := IniFile.ReadString('Codes', 'SNCode', '');
    IniSNVal := IniFile.ReadInteger('Codes', 'SN', 0);

    {convert retrieved string to a code}
    HexToBuffer(S1, Code, SizeOf(Code));
  finally
    IniFile.Free;
  end;

end;

procedure TForm1.OgSerialNumberCode1Checked(Sender: TObject; Status: TCodeStatus);
var
  S,
  C1,
  C2  : string;
  TC  : TCode;
  LI  : longint;
begin
  case Status of
    ogValidCode   : begin
                      {check if retrieved Serial Number matches Code}
                      LI := OgSerialNumberCode1.GetValue;

                      if (LI <> IniSNVal) then begin
                        Status := ogInvalidCode;
                        S := 'The serial number has been changed';
                      end else begin
                          ShowMessage('Serial #: ' + IntToStr(IniSNVal));
                        Exit;
                      end;
                    end;

    ogInvalidCode : begin
                      {if INI file doesn't exist, presume this is first run}
                      if not (FileExists(TheDir + 'test.INI')) then
                      begin
                        if not (GetSNData(S) = mrCancel) then begin
                          {Check the SN/ReleaseCode}
                          OgSerialNumberCode1.CheckCode(True);
                          {must Exit since line above began a recursive call}
                          Exit;
                        end;
                      end else
                        S := 'Invalid Code';
                    end;

    ogCodeExpired : S := 'Evaluation period expired';
  end;

  ShowMessage(S);
  Application.Terminate;    
end;

但在客户端机器从不接受验证并抛出未正确输入的发布代码。可能我不了解 onguard 文件夹中 machineID 示例的用法。另请注意,客户端的密钥和发布代码的生成是相同的,因此不应有任何不匹配。

4

0 回答 0