// keygenDlg.cpp : implementation file
//

#include <stdio.h>
#include <commctrl.h>
#include <windows.h>

#include "stdafx.h"
#include "keygen.h"
#include "keygenDlg.h"
#include "../../../EnigmaSDK/VC/enigma_keygen_ide.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKeygenDlg dialog

CKeygenDlg::CKeygenDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CKeygenDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CKeygenDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CKeygenDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKeygenDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CKeygenDlg, CDialog)
	//{{AFX_MSG_MAP(CKeygenDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
	ON_BN_CLICKED(IDC_CHECK_HARDWARE, OnCheckHardware)
	ON_BN_CLICKED(IDC_CHECK_EXPIRATION, OnCheckExpiration)
	ON_BN_CLICKED(IDC_BUTTON_GEN, OnButtonGen)
	ON_BN_CLICKED(IDC_BUTTON_GENFP, OnButtonGenfp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKeygenDlg message handlers

BOOL CKeygenDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CKeygenDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CKeygenDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CKeygenDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CKeygenDlg::OnButtonClose() 
{
	// Close button was pressed
	ExitProcess(0);	
}


void CKeygenDlg::OnCheckHardware() 
{
	CWnd* wnd;

	// Enable-disable Hardware Edit control
	wnd = GetDlgItem(IDC_EDIT_HARDWARE);    
	wnd->EnableWindow(IsDlgButtonChecked(IDC_CHECK_HARDWARE));
}


void CKeygenDlg::OnCheckExpiration() 
{
	CWnd* wnd;

	// Enable-disable Expiration DTPicker control
	wnd = GetDlgItem(IDC_DATETIMEPICKER_EXPIRATION);    
	wnd->EnableWindow(IsDlgButtonChecked(IDC_CHECK_EXPIRATION));	
}

void CKeygenDlg::OnButtonGen() 
{
  GenKeyParams kp;
  BYTE keybuffer[255];
  char tempbuf[255];
  SYSTEMTIME lpExpirationDate;
  DWORD dwResult;
  LPCTSTR sError;

  SetDlgItemText(IDC_EDIT_KEY, "");

  // Clear params buffer
  memset(&kp, 0, sizeof(kp));

  // Fill key buffer
  memset(&keybuffer, 0, sizeof(keybuffer));

  kp.KeyType = KEY_TYPE_LONG;
  kp.KeyFormat = KEY_FORMAT_32;
  kp.Key = (char*) keybuffer;
  kp.KeyLen = sizeof(keybuffer);

  // Fill User Info
  memset(&tempbuf, 0, sizeof(tempbuf));
  GetDlgItemText(IDC_EDIT_USERINFO, (char*)&tempbuf, sizeof(tempbuf));
  kp.RegInfo = (char*)&tempbuf;
  kp.RegInfoLen = strlen(kp.RegInfo);

  // Key expiration info
  kp.UseKeyExpiration = (BYTE)IsDlgButtonChecked(IDC_CHECK_EXPIRATION);
  SendDlgItemMessage(IDC_DATETIMEPICKER_EXPIRATION, DTM_GETSYSTEMTIME, GDT_VALID, (LPARAM)&lpExpirationDate);
  
  kp.ExpirationYear = lpExpirationDate.wYear;
  kp.ExpirationMonth = (unsigned char)lpExpirationDate.wMonth;
  kp.ExpirationDay = (unsigned char)lpExpirationDate.wDay;

  // Hardware ID
  kp.UseHardwareLocking = (BYTE)IsDlgButtonChecked(IDC_CHECK_HARDWARE);
  memset(&tempbuf, 0, sizeof(tempbuf));
  GetDlgItemText(IDC_EDIT_HARDWARE, (char*)&tempbuf, sizeof(tempbuf));
  kp.HardwareID = (char*)&tempbuf;

  // Crypt Sections
  kp.EncryptedSections[0] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION1);
  kp.EncryptedSections[1] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION2);
  kp.EncryptedSections[2] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION3);
  kp.EncryptedSections[3] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION4);
  kp.EncryptedSections[4] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION5);
  kp.EncryptedSections[5] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION6);
  kp.EncryptedSections[6] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION7);
  kp.EncryptedSections[7] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION8);
  kp.EncryptedSections[8] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION9);
  kp.EncryptedSections[9] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION10);
  kp.EncryptedSections[10] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION11);
  kp.EncryptedSections[11] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION12);
  kp.EncryptedSections[12] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION13);
  kp.EncryptedSections[13] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION14);
  kp.EncryptedSections[14] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION15);
  kp.EncryptedSections[15] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION16);

  // Secure constant (get it from project file)!
  // or use special utility
  if (kp.KeyType == KEY_TYPE_LONG)
  {
    kp.D = "3K4RHBXV93J75R72XBF68M5GTQ2PDDVCJCQ9ZZ7CXBZYJNY6ASWVB6S3X4JP9TWPBANMW2HSMPAZ7GY5WZF8TQYJ36VUQZQ7J4F6JF";
    kp.N = "7MSYMACUVPSKBB44WSEF3Q7T6ATNEDQ9D3UGQRP8SYVCXDDEX8TB78PFHWXGR4RVVTWVCSXANQWHDLWF2XTMS7PAA3396RALRP3UUK";
  }
  if (kp.KeyType == KEY_TYPE_SHORT)
  {
    kp.D = "9A2GSDM7LGTE5TLDR74QRTH8SSC76XZGD5SLHMHP";
    kp.N = "3RK8BPE4MW9NFJARVTFWMGFSWJCJMMZ7G5XK97JJM";
  }

  kp.A = "F4A190D21E05F4DF387A18CFBB70426E";
  kp.B = "C7A44A94C94D7F290037E9DACCC2CB23";

  // Generate reg. key
  dwResult = KG_GenerateRegistrationKey(&kp);

  if (dwResult == ERROR_OK)
  {		
	SetDlgItemText(IDC_EDIT_KEY, kp.Key);
  } else
  {
    switch (dwResult)
    {
      case ERROR_KEY_BUFFER_NOT_ALLOCATED:
        sError = "ERROR_KEY_BUFFER_NOT_ALLOCATED";
        break;
      case ERROR_KEY_BUFFER_LESS:
        sError = "ERROR_KEY_BUFFER_LESS";
        break;
      case ERROR_REGINFO_BUFFER_NOT_ALLOCATED:
        sError = "ERROR_REGINFO_BUFFER_NOT_ALLOCATED";
        break;
      case ERROR_REGINFO_BUFFER_EMPTY:
        sError = "ERROR_REGINFO_BUFFER_EMPTY";
        break;
      case ERROR_HARDWARE_TYPE_UNKNOWN:
        sError = "ERROR_HARDWARE_TYPE_UNKNOWN";
        break;
      case ERROR_HARDWARE_ID:
        sError = "ERROR_HARDWARE_ID";
        break;
      case ERROR_HARDWARE_BUFFER_NOT_ALLOCATED:
        sError = "ERROR_HARDWARE_BUFFER_NOT_ALLOCATED";
        break;
      case ERROR_HARDWARE_BUFFER_EMPTY:
        sError = "ERROR_HARDWARE_BUFFER_EMPTY";
        break;
      case ERROR_INVALID_CONSTANT_D:
        sError = "ERROR_INVALID_CONSTANT_D";
        break;
      case ERROR_INVALID_CONSTANT_N:
        sError = "ERROR_INVALID_CONSTANT_N";
        break;
      case ERROR_INVALID_CONSTANT_A:
        sError = "ERROR_INVALID_CONSTANT_A";
        break;
      case ERROR_INVALID_CONSTANT_B:
        sError = "ERROR_INVALID_CONSTANT_B";
        break;
      case ERROR_PROJECT_FILE_NOT_FOUND:
        sError = "ERROR_PROJECT_FILE_NOT_FOUND";
        break;
      case ERROR_PROJECT_FILE_INVALID:
        sError = "ERROR_PROJECT_FILE_INVALID";
        break;
      default:
        sError = "ERROR_UNKNOWN";
        break;
    };
	SetDlgItemText(IDC_EDIT_KEY, sError);
  };
}

void CKeygenDlg::OnButtonGenfp() 
{	
  GenKeyParams kp;
  BYTE keybuffer[255];
  char tempbuf[255];
  SYSTEMTIME lpExpirationDate;
  DWORD dwResult;
  LPCTSTR sError;

  SetDlgItemText(IDC_EDIT_KEY, "");

  // Clear params buffer
  memset(&kp, 0, sizeof(kp));

  // Fill key buffer
  memset(&keybuffer, 0, sizeof(keybuffer));

  kp.Key = (char*) keybuffer;
  kp.KeyLen = sizeof(keybuffer);

  // Fill User Info
  memset(&tempbuf, 0, sizeof(tempbuf));
  GetDlgItemText(IDC_EDIT_USERINFO, (char*)&tempbuf, sizeof(tempbuf));
  kp.RegInfo = (char*)&tempbuf;
  kp.RegInfoLen = strlen(kp.RegInfo);

  // Key expiration info
  kp.UseKeyExpiration = (BYTE)IsDlgButtonChecked(IDC_CHECK_EXPIRATION);
  SendDlgItemMessage(IDC_DATETIMEPICKER_EXPIRATION, DTM_GETSYSTEMTIME, GDT_VALID, (LPARAM)&lpExpirationDate);
  
  kp.ExpirationYear = lpExpirationDate.wYear;
  kp.ExpirationMonth = (unsigned char)lpExpirationDate.wMonth;
  kp.ExpirationDay = (unsigned char)lpExpirationDate.wDay;

  // Hardware ID
  kp.UseHardwareLocking = (BYTE)IsDlgButtonChecked(IDC_CHECK_HARDWARE);
  memset(&tempbuf, 0, sizeof(tempbuf));
  GetDlgItemText(IDC_EDIT_HARDWARE, (char*)&tempbuf, sizeof(tempbuf));
  kp.HardwareID = (char*)&tempbuf;

  // Crypt Sections
  kp.EncryptedSections[0] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION1);
  kp.EncryptedSections[1] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION2);
  kp.EncryptedSections[2] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION3);
  kp.EncryptedSections[3] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION4);
  kp.EncryptedSections[4] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION5);
  kp.EncryptedSections[5] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION6);
  kp.EncryptedSections[6] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION7);
  kp.EncryptedSections[7] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION8);
  kp.EncryptedSections[8] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION9);
  kp.EncryptedSections[9] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION10);
  kp.EncryptedSections[10] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION11);
  kp.EncryptedSections[11] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION12);
  kp.EncryptedSections[12] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION13);
  kp.EncryptedSections[13] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION14);
  kp.EncryptedSections[14] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION15);
  kp.EncryptedSections[15] = (BYTE)IsDlgButtonChecked(IDC_CHECK_SECTION16);

  // Generate reg. key
  dwResult = KG_GenerateRegistrationKeyFromProject("default.enigma", &kp);

  if (dwResult == ERROR_OK)
  {		
	SetDlgItemText(IDC_EDIT_KEY, kp.Key);
  } else
  {
    switch (dwResult)
    {
      case ERROR_KEY_BUFFER_NOT_ALLOCATED:
        sError = "ERROR_KEY_BUFFER_NOT_ALLOCATED";
        break;
      case ERROR_KEY_BUFFER_LESS:
        sError = "ERROR_KEY_BUFFER_LESS";
        break;
      case ERROR_REGINFO_BUFFER_NOT_ALLOCATED:
        sError = "ERROR_REGINFO_BUFFER_NOT_ALLOCATED";
        break;
      case ERROR_REGINFO_BUFFER_EMPTY:
        sError = "ERROR_REGINFO_BUFFER_EMPTY";
        break;
      case ERROR_HARDWARE_TYPE_UNKNOWN:
        sError = "ERROR_HARDWARE_TYPE_UNKNOWN";
        break;
      case ERROR_HARDWARE_ID:
        sError = "ERROR_HARDWARE_ID";
        break;
      case ERROR_HARDWARE_BUFFER_NOT_ALLOCATED:
        sError = "ERROR_HARDWARE_BUFFER_NOT_ALLOCATED";
        break;
      case ERROR_HARDWARE_BUFFER_EMPTY:
        sError = "ERROR_HARDWARE_BUFFER_EMPTY";
        break;
      case ERROR_INVALID_CONSTANT_D:
        sError = "ERROR_INVALID_CONSTANT_D";
        break;
      case ERROR_INVALID_CONSTANT_N:
        sError = "ERROR_INVALID_CONSTANT_N";
        break;
      case ERROR_INVALID_CONSTANT_A:
        sError = "ERROR_INVALID_CONSTANT_A";
        break;
      case ERROR_INVALID_CONSTANT_B:
        sError = "ERROR_INVALID_CONSTANT_B";
        break;
      case ERROR_PROJECT_FILE_NOT_FOUND:
        sError = "ERROR_PROJECT_FILE_NOT_FOUND";
        break;
      case ERROR_PROJECT_FILE_INVALID:
        sError = "ERROR_PROJECT_FILE_INVALID";
        break;
      default:
        sError = "ERROR_UNKNOWN";
        break;
    };
	SetDlgItemText(IDC_EDIT_KEY, sError);
  };
}
