unit RegisterU;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,VMProtectSDK;

type
  TfrmRegistration = class(TForm)
    lblName: TLabel;
    edtName: TEdit;
    lblKey: TLabel;
    edtKey: TEdit;
    btnRegister: TButton;
    btnExit: TButton;
    procedure btnExitClick(Sender: TObject);
    procedure btnRegisterClick(Sender: TObject);
  private

  public
    function CalcHash(Data : pointer; DataSize : integer) : integer; register;
  end;

var
  frmRegistration: TfrmRegistration;

const
  BLACKLIST_KEY = 'WMDB-ESEW-1196-U1JF-15789821';  // this is blacklist key for patching

implementation

{$R *.dfm}

procedure TfrmRegistration.btnExitClick(Sender: TObject);
begin
  frmRegistration.Close;
end;

function TfrmRegistration.CalcHash(Data: pointer;
  DataSize: integer): integer;
asm
  push ebx
  push esi
  push edi
  mov esi, Data
  xor ebx, ebx
  or esi, esi
  jz @@Exit
  mov edx, DataSize
  or edx,edx
  jz @@Exit
  xor ecx,ecx

@@Cycle:
  xor eax,eax
  mov al,[esi]
  inc esi
  rol eax,cl
  xor ebx,eax
  inc ecx
  dec edx
  jnz @@Cycle

@@Exit:
  mov eax,ebx
  pop edi
  pop esi
  pop ebx
end;

procedure TfrmRegistration.btnRegisterClick(Sender: TObject);
var
  RegList: TStringList;
  Hash: array [0..1] of string;
  HashString: String;
begin
  VMProtectBeginUltra('Check Reg Key'); // start VM (Mutation+Virtualization)
  if Length(edtName.Text) >= 4 then
  begin
    if edtKey.Text <> '' then
    begin
      RegList := TStringList.Create;
      try
        RegList.Delimiter := '-';
        RegList.DelimitedText := edtKey.Text;
        if (UpperCase(edtName.Text[1]) = Copy(RegList.Strings[0], 3,1)) and
           (UpperCase(edtName.Text[3]) = Copy(RegList.Strings[1], 3,1))then
        begin
          Hash[0] := IntToStr(CalcHash(pointer(edtName.Text), Length(edtName.Text)));
          HashString:= RegList.Strings[0]+ '-' + RegList.Strings[1] + '-' +RegList.Strings[2] + '-' + RegList.Strings[3] + '-';
          Hash[1] :=  IntToStr(CalcHash(pointer(HashString), Length(HashString)));
          if (Hash[0] = RegList[2]) and (Hash[1] = RegList[4]) and (edtKey.Text <> BLACKLIST_KEY) then
          begin
            MessageBox(Handle, 'Success Registration!', 'Ok', MB_OK+MB_ICONINFORMATION);
            RegList.Clear;
            RegList.Add(edtName.Text);
            RegList.Add(edtKey.Text);
            RegList.SaveToFile(ExtractFilePath(Application.ExeName) + 'Key.ini');
            frmRegistration.Close;
          end
          else begin
            MessageBox(Handle, 'Wrong Key!', 'Error', MB_OK+MB_ICONERROR);
          end;
        end
        else begin
          MessageBox(Handle, 'Wrong Key!', 'Error', MB_OK+MB_ICONERROR);
        end;
      finally
        RegList.Free;
      end;
    end
    else begin
      MessageBox(Handle, 'Enter Key!', 'Error', MB_OK+MB_ICONERROR);
      edtKey.SetFocus;
    end;
  end
  else begin
    MessageBox(Handle, 'Enter minimum 4 characters!', 'Error', MB_OK+MB_ICONERROR);
    edtName.SetFocus;
  end;
  VMProtectEnd; // end marker VM  
end;

end.
