Click here to Skip to main content
15,886,664 members
Articles / Desktop Programming / MFC

Exile 1.8 - The Password Manager

Rate me:
Please Sign up or sign in to vote.
4.57/5 (51 votes)
6 Mar 20058 min read 255.3K   7.4K   111  
Yet another password manager.
// PasswordGeneratorDialog.cpp : implementation file
//

#include "stdafx.h"
#include "exile.h"
#include "security.h"
#include "PasswordGeneratorDialog.h"
#include "Registry.h"
#include "messagebox.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPasswordGeneratorDialog dialog


CPasswordGeneratorDialog::CPasswordGeneratorDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CPasswordGeneratorDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPasswordGeneratorDialog)
	m_bDecimalDigits = FALSE;
	m_bLowercase = FALSE;
	m_bPunctuation = FALSE;
	m_bSpecial = FALSE;
	m_bUppercase = FALSE;
	m_nPasswordLength = 0;
	m_strPassword = _T("");
	//}}AFX_DATA_INIT
}


void CPasswordGeneratorDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPasswordGeneratorDialog)
	DDX_Check(pDX, IDC_DECIMALDIGITS, m_bDecimalDigits);
	DDX_Check(pDX, IDC_LOWERCASE, m_bLowercase);
	DDX_Check(pDX, IDC_PUNCTUATIONMARKS, m_bPunctuation);
	DDX_Check(pDX, IDC_SPECIALCHARACTERS, m_bSpecial);
	DDX_Check(pDX, IDC_UPPERCASE, m_bUppercase);
	DDX_Text(pDX, IDC_PASSWORDLENGTH, m_nPasswordLength);
	DDX_Text(pDX, IDC_PASSWORD, m_strPassword);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPasswordGeneratorDialog, CDialog)
	//{{AFX_MSG_MAP(CPasswordGeneratorDialog)
	ON_BN_CLICKED(IDC_PASSWORD_TITLE, OnPasswordTitle)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPasswordGeneratorDialog message handlers

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

	// Loading settings
	CRegistry reg;
	CString strKey;

	reg.SetRootKey(HKEY_CURRENT_USER);
	strKey.LoadString(IDS_REGKEY);
	strKey += _T("\\Password Generator");

	BOOL bKey = reg.SetKey(strKey, FALSE);

	m_bUppercase = bKey ? reg.ReadBool("English Upper-case", TRUE) : TRUE;
	m_bLowercase = bKey ? reg.ReadBool("English Lower-case", TRUE) : TRUE;
	m_bDecimalDigits = bKey ? reg.ReadBool("Decimal Digits", TRUE) : TRUE;
	m_bPunctuation = bKey ? reg.ReadBool("Punctuation", FALSE) : FALSE;
	m_bSpecial = bKey ? reg.ReadBool("Special Characters", FALSE) : FALSE;

	m_nPasswordLength = bKey ? reg.ReadInt("Password Length", 8) : 8;

	UpdateData(FALSE);
	
	m_ctrlPassword.SubclassDlgItem(IDC_PASSWORD_TITLE, this);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPasswordGeneratorDialog::OnPasswordTitle() 
{
	// Preparing data to generate password
	UpdateData();

	if(!(m_bUppercase || m_bLowercase || m_bDecimalDigits || m_bPunctuation || m_bSpecial))
	{
		MessageBoxEx(*this, IDS_SELECTCHARSET, IDS_TITLE);
		return;
	} // if

	if((4 >= m_nPasswordLength) || (1025 <= m_nPasswordLength))
	{
		MessageBoxEx(*this, IDS_ENTERPASSWORDLENGTH, IDS_TITLE);
		return;
	} // if

	int nLength = 0, nCharSet = 0;
#ifdef _UNICODE
	nLength = wcslen(g_szEnglishUppercase) + wcslen(g_szEnglishLowercase) + wcslen(g_szDigits) +
		wcslen(g_szPunctuation) + wcslen(g_szSpecial);
#else
	nLength = strlen(g_szEnglishUppercase) + strlen(g_szEnglishLowercase) + strlen(g_szDigits) +
		strlen(g_szPunctuation) + strlen(g_szSpecial);
#endif // _UNICODE

	TCHAR *pszCharset = new TCHAR[nLength + 1];
	pszCharset[0] = 0;

#ifdef _UNICODE
	if(m_bUppercase) 
	{
		wcscat(pszCharset, g_szEnglishUppercase);
		nCharSet += wcslen(g_szEnglishUppercase);
	} // if

	if(m_bLowercase)
	{
		wcscat(pszCharset, g_szEnglishLowercase);
		nCharSet += wcslen(g_szEnglishLowercase);
	} // if

	if(m_bDecimalDigits) 
	{
		wcscat(pszCharset, g_szDigits);
		nCharSet += wcslen(g_szDigits);
	} // if

	if(m_bPunctuation) 
	{
		wcscat(pszCharset, g_szPunctuation);
		nCharSet += wcslen(g_szPunctuation);
	} // if
	
	if(m_bSpecial) 
	{
		wcscat(pszCharset, g_szSpecial);
		nCharSet += wcslen(g_szSpecial);
	} // if
#else
	if(m_bUppercase) 
	{
		strcat(pszCharset, g_szEnglishUppercase);
		nCharSet += strlen(g_szEnglishUppercase);
	} // if

	if(m_bLowercase)
	{
		strcat(pszCharset, g_szEnglishLowercase);
		nCharSet += strlen(g_szEnglishLowercase);
	} // if

	if(m_bDecimalDigits) 
	{
		strcat(pszCharset, g_szDigits);
		nCharSet += strlen(g_szDigits);
	} // if

	if(m_bPunctuation) 
	{
		strcat(pszCharset, g_szPunctuation);
		nCharSet += strlen(g_szPunctuation);
	} // if
	
	if(m_bSpecial) 
	{
		strcat(pszCharset, g_szSpecial);
		nCharSet += strlen(g_szSpecial);
	} // if
#endif // _UNICODE	

	BeginWaitCursor();	
	
	// Data
	DWORD *pdwData = new DWORD[m_nPasswordLength * 2];
	POINT ptOld, ptCur;

	::GetCursorPos(&ptOld);

	for(int n = 0; n < m_nPasswordLength; ++n)
	{
		do 
		{
			::GetCursorPos(&ptCur);
		} 
		while((ptOld.x == ptCur.x) || (ptOld.y == ptCur.y));

		pdwData[2 * n] = ptCur.x;
		pdwData[2 * n + 1] = ptCur.y;

		ptOld = ptCur;
	} // for

	TCHAR *pszPassword = new TCHAR[m_nPasswordLength + 1];

	GeneratePassword(pszCharset, nCharSet, pdwData, m_nPasswordLength * 2, m_nPasswordLength, pszPassword);

	// Freeing
	FreeBuffer(pdwData, sizeof(DWORD) * m_nPasswordLength);
	FreeBuffer(pszCharset, sizeof(TCHAR) * (nLength + 1));

	pszPassword[m_nPasswordLength] = _T(0);

	SetDlgItemText(IDC_PASSWORD, pszPassword);

	GetDlgItem(IDC_PASSWORD)->EnableWindow(TRUE);

	FreeBuffer(pszPassword, sizeof(TCHAR) * m_nPasswordLength);

	EndWaitCursor();
}

void CPasswordGeneratorDialog::OnDestroy() 
{
	BeginWaitCursor();

	UpdateData();

	// Saving settings
	BeginWaitCursor();
	CRegistry reg;
	CString strKey;

	reg.SetRootKey(HKEY_CURRENT_USER);
	strKey.LoadString(IDS_REGKEY);

	strKey += _T("\\Password Generator");

	if(reg.CreateKey(strKey))
	{
		reg.WriteBool("English Upper-case", m_bUppercase);
		reg.WriteBool("English Lower-case", m_bLowercase);
		reg.WriteBool("Decimal Digits", m_bDecimalDigits);
		reg.WriteBool("Punctuation", m_bPunctuation);
		reg.WriteBool("Special Characters", m_bSpecial);

		reg.WriteInt("Password Length", m_nPasswordLength);
	} // if

	EndWaitCursor();

	CDialog::OnDestroy();	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
I'll think about it later on...

Comments and Discussions