Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / MFC

Cryptest (MFC Style Crypto++ v4.2 Library GUI).

Rate me:
Please Sign up or sign in to vote.
4.62/5 (16 votes)
6 Jan 2002CPOL3 min read 237.4K   2.7K   91  
A GUI version of the Crypto++ v4.2 Library that was written by Wei Dai.
// CryptestDlg.cpp : implementation file
//
//////////////////////////////////////////////////////////////////////////////
//
// In Release mode:
//
// Take off the "_AFXDLL" and change "Multithreaded DLL" to "Multithreaded"
//
//////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Cryptest.h"
#include "CryptestDlg.h"

#include "FileStffDlg.h"
#include "Validation.h"
#include "BenchMark.h"
#include "TCPForwarding.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()

/////////////////////////////////////////////////////////////////////////////
// CCryptestDlg dialog

CCryptestDlg::CCryptestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCryptestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCryptestDlg)
	m_csPublicKey = _T("CryptestPub.key");
	m_csPrivateKey = _T("CryptestPri.key");
	m_unKeyLength = 1024;
	m_csSeed = _T("This is the seed!");
	m_csPassPhrase = _T("This is a Pass Phrase!");
	m_csPlainText = _T("This is Plain Text!");
	m_csDataEncrypted = _T("");
	m_csDataDecrypted = _T("");
	m_csMessage = _T("This is a message!");
	m_csRSADecrypted = _T("");
	m_csRSAEncrypted = _T("");
	m_csMsgFile = m_csPrivateKey;
	m_csSigFile = _T("SigFile.txt");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_nRSAType = 1;
	m_nDataType = 1;
}

void CCryptestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCryptestDlg)
	DDX_Control(pDX, EDIT_SEED, m_edtSeed);
	DDX_Control(pDX, EDIT_RSA_ENCRYPTED, m_edtRSAEncrypted);
	DDX_Control(pDX, EDIT_RSA_DECRYPTED, m_edtRSADecrypted);
	DDX_Control(pDX, EDIT_PLAIN_TEXT, m_edtPlainText);
	DDX_Control(pDX, EDIT_PASS_PHRASE, m_edtPassPhrase);
	DDX_Control(pDX, EDIT_MESSAGE, m_edtMessage);
	DDX_Control(pDX, EDIT_KEY_LENGTH, m_edtKeyLength);
	DDX_Control(pDX, EDIT_DATA_ENCRYPTED, m_edtDataEncrypted);
	DDX_Control(pDX, EDIT_DATA_DECRYPTED, m_edtDataDecrypted);
	DDX_Text(pDX, EDIT_PUBLIC_KEY, m_csPublicKey);
	DDX_Text(pDX, EDIT_PRIVATE_KEY, m_csPrivateKey);
	DDX_Text(pDX, EDIT_KEY_LENGTH, m_unKeyLength);
	DDX_Text(pDX, EDIT_SEED, m_csSeed);
	DDX_Text(pDX, EDIT_PASS_PHRASE, m_csPassPhrase);
	DDX_Text(pDX, EDIT_PLAIN_TEXT, m_csPlainText);
	DDX_Text(pDX, EDIT_DATA_ENCRYPTED, m_csDataEncrypted);
	DDX_Text(pDX, EDIT_DATA_DECRYPTED, m_csDataDecrypted);
	DDX_Text(pDX, EDIT_MESSAGE, m_csMessage);
	DDX_Text(pDX, EDIT_RSA_DECRYPTED, m_csRSADecrypted);
	DDX_Text(pDX, EDIT_RSA_ENCRYPTED, m_csRSAEncrypted);
	DDX_Text(pDX, EDIT_RSA_MSG_FILE, m_csMsgFile);
	DDX_Text(pDX, EDIT_RSA_SIG_FILE, m_csSigFile);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCryptestDlg, CDialog)
	//{{AFX_MSG_MAP(CCryptestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(BTN_FILE_STUFF, OnFileStuff)
	ON_BN_CLICKED(BTN_RSA_DOIT, OnRsaDoit)
	ON_WM_CLOSE()
	ON_BN_CLICKED(BTN_EXIT, OnExit)
	ON_BN_CLICKED(RAD_GENERATE, OnGenerate)
	ON_BN_CLICKED(RAD_RSA_DECRYPT, OnRsaDecrypt)
	ON_BN_CLICKED(RAD_RSA_ENCRYPT, OnRsaEncrypt)
	ON_BN_CLICKED(RAD_SIGN, OnSign)
	ON_BN_CLICKED(RAD_VERIFY, OnVerify)
	ON_BN_CLICKED(BTN_DATA_DOIT, OnDataDoit)
	ON_BN_CLICKED(RAD_DATA_ENCRYPT, OnDataEncrypt)
	ON_BN_CLICKED(RAD_DATA_DECRYPT, OnDataDecrypt)
	ON_BN_CLICKED(BTN_VALIDATE, OnValidate)
	ON_BN_CLICKED(BTN_BENCH_MARKS, OnBenchMarks)
	ON_BN_CLICKED(BTN_TCP_FORWARDING, OnTcpForwarding)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCryptestDlg message handlers

BOOL CCryptestDlg::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
	
	GetDlgItem(EDIT_PRIVATE_KEY)->SetWindowText(m_csPrivateKey);
	GetDlgItem(EDIT_PUBLIC_KEY)->SetWindowText(m_csPublicKey);

	OnGenerate();
	OnDataEncrypt();

	GetDlgItem(RAD_RSA_DECRYPT)->EnableWindow(FALSE);
	GetDlgItem(RAD_DATA_DECRYPT)->EnableWindow(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCryptestDlg::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 CCryptestDlg::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 CCryptestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCryptestDlg::OnOK() 
{
	//CDialog::OnOK();
}

void CCryptestDlg::OnCancel() 
{
	//CDialog::OnCancel();
}

void CCryptestDlg::OnClose() 
{
	CDialog::OnOK();
}

void CCryptestDlg::OnExit() 
{
	OnClose();
}

void CCryptestDlg::OnFileStuff() 
{
	CFileStffDlg dlgFS;
	dlgFS.DoModal();
}

void CCryptestDlg::OnRsaDoit() 
{

	CWaitCursor wait;

	CCryptestApp* pcCryptest = (CCryptestApp*) AfxGetApp();

	UpdateData(TRUE);

	CString cs = _T("");

	switch (m_nRSAType)
	{
		case 1:
			pcCryptest->GenerateRSAKey(m_unKeyLength, m_csPrivateKey, m_csPublicKey, m_csSeed);
			break;
		case 2:
			m_csRSADecrypted = pcCryptest->RSADecryptString(m_csPrivateKey, m_csRSAEncrypted);
			GetDlgItem(EDIT_RSA_DECRYPTED)->SetWindowText(m_csRSADecrypted);
			break;
		case 3:
			m_csRSAEncrypted = pcCryptest->RSAEncryptString(m_csPublicKey, m_csSeed, m_csMessage);
			GetDlgItem(EDIT_RSA_ENCRYPTED)->SetWindowText(m_csRSAEncrypted);
			GetDlgItem(RAD_RSA_DECRYPT)->EnableWindow(TRUE);
			break;
		case 4:
			pcCryptest->RSASignFile(m_csPrivateKey, m_csMsgFile, m_csSigFile);
			GetDlgItem(RAD_VERIFY)->EnableWindow(TRUE);
			cs = _T("Signature File has been generated, go look at it if you like!\n\n");
			cs += _T("Change a character in it, then press \"Verify\", it will return \"Invalid Signature!!!!\".\n\n");
			cs += _T("When you change it back and press \"Verify\", it will return \"Valid Signature!\".");
			MessageBox(cs);
			break;
		case 5:
			if (pcCryptest->RSAVerifyFile(m_csPublicKey, m_csMsgFile, m_csSigFile))
				MessageBox(_T("Valid Signature!"));
			else
				MessageBox(_T("Invalid Signature!!!!"));

//			GetDlgItem(RAD_VERIFY)->EnableWindow(FALSE);
			break;
	}
}

void CCryptestDlg::OnGenerate() 
{
	m_nRSAType = 1;
	GetDlgItem(STATIC_RSA_INFO)->SetWindowText(_T("Generate an RSA Public and Private Key File."));
	CheckRadioButton(1006, 1010, 1006);
	GetDlgItem(BTN_RSA_DOIT)->SetWindowText(_T("Generate It"));
	GetDlgItem(EDIT_KEY_LENGTH)->EnableWindow(FALSE);
	GetDlgItem(EDIT_SEED)->EnableWindow(FALSE);
	GetDlgItem(EDIT_MESSAGE)->EnableWindow(FALSE);
	GetDlgItem(RAD_VERIFY)->EnableWindow(FALSE);
}

void CCryptestDlg::OnRsaDecrypt() 
{
	m_nRSAType = 2;
	GetDlgItem(STATIC_RSA_INFO)->SetWindowText(_T("Decrypt a string using the Private Key File."));
	CheckRadioButton(1006, 1010, 1009);
	GetDlgItem(BTN_RSA_DOIT)->SetWindowText(_T("Decrypt It"));
	GetDlgItem(EDIT_KEY_LENGTH)->EnableWindow(FALSE);
	GetDlgItem(EDIT_SEED)->EnableWindow(FALSE);
	GetDlgItem(EDIT_MESSAGE)->EnableWindow(FALSE);
	GetDlgItem(EDIT_RSA_DECRYPTED)->SetWindowText(_T(""));
	GetDlgItem(RAD_VERIFY)->EnableWindow(FALSE);
}

void CCryptestDlg::OnRsaEncrypt() 
{
	m_nRSAType = 3;
	GetDlgItem(STATIC_RSA_INFO)->SetWindowText(_T("Encrypt a string using the Public Key File."));
	CheckRadioButton(1006, 1010, 1010);
	GetDlgItem(BTN_RSA_DOIT)->SetWindowText(_T("Encrypt It"));
	GetDlgItem(EDIT_KEY_LENGTH)->EnableWindow(FALSE);
	GetDlgItem(EDIT_SEED)->EnableWindow(FALSE);
	GetDlgItem(EDIT_MESSAGE)->EnableWindow(FALSE);
	GetDlgItem(EDIT_RSA_ENCRYPTED)->SetWindowText(_T(""));
	GetDlgItem(EDIT_RSA_DECRYPTED)->SetWindowText(_T(""));
	GetDlgItem(RAD_RSA_DECRYPT)->EnableWindow(FALSE);
	GetDlgItem(RAD_VERIFY)->EnableWindow(FALSE);
}

void CCryptestDlg::OnSign() 
{
	m_nRSAType = 4;
	GetDlgItem(STATIC_RSA_INFO)->SetWindowText(_T("Sign a file using RSA."));
	CheckRadioButton(1006, 1010, 1007);
	GetDlgItem(BTN_RSA_DOIT)->SetWindowText(_T("Sign It"));
	GetDlgItem(EDIT_KEY_LENGTH)->EnableWindow(FALSE);
	GetDlgItem(EDIT_SEED)->EnableWindow(FALSE);
	GetDlgItem(EDIT_MESSAGE)->EnableWindow(FALSE);
	GetDlgItem(RAD_VERIFY)->EnableWindow(FALSE);
}

void CCryptestDlg::OnVerify() 
{
	m_nRSAType = 5;
	GetDlgItem(STATIC_RSA_INFO)->SetWindowText(_T("Verify a signature of a file using RSA."));
	CheckRadioButton(1006, 1010, 1008);
	GetDlgItem(BTN_RSA_DOIT)->SetWindowText(_T("Verify It"));
	GetDlgItem(EDIT_KEY_LENGTH)->EnableWindow(FALSE);
	GetDlgItem(EDIT_SEED)->EnableWindow(FALSE);
	GetDlgItem(EDIT_MESSAGE)->EnableWindow(FALSE);
}

void CCryptestDlg::OnDataDoit() 
{
	CCryptestApp* pcCryptest = (CCryptestApp*) AfxGetApp();

	UpdateData(TRUE);

	switch (m_nDataType)
	{
		case 1:
			m_csDataEncrypted = pcCryptest->EncryptString(m_csPlainText, m_csPassPhrase);
			GetDlgItem(EDIT_DATA_ENCRYPTED)->SetWindowText(m_csDataEncrypted);
			GetDlgItem(RAD_DATA_DECRYPT)->EnableWindow(TRUE);
			break;
		case 2:
			m_csDataDecrypted = pcCryptest->DecryptString(m_csDataEncrypted, m_csPassPhrase);
			GetDlgItem(EDIT_DATA_DECRYPTED)->SetWindowText(m_csDataDecrypted);
			break;
	}
}

void CCryptestDlg::OnDataEncrypt() 
{
	m_nDataType = 1;
	GetDlgItem(STATIC_DATA_INFO)->SetWindowText(_T("Encrypt a string using DES-EDE in CBC mode."));
	CheckRadioButton(1035, 1036, 1035);
	GetDlgItem(BTN_DATA_DOIT)->SetWindowText(_T("Encrypt It"));
	GetDlgItem(EDIT_PLAIN_TEXT)->EnableWindow(FALSE);
	GetDlgItem(EDIT_PASS_PHRASE)->EnableWindow(FALSE);
	GetDlgItem(EDIT_DATA_ENCRYPTED)->SetWindowText(_T(""));
	GetDlgItem(EDIT_DATA_DECRYPTED)->SetWindowText(_T(""));
	GetDlgItem(RAD_DATA_DECRYPT)->EnableWindow(FALSE);
}

void CCryptestDlg::OnDataDecrypt() 
{
	m_nDataType = 2;
	GetDlgItem(STATIC_DATA_INFO)->SetWindowText(_T("Decrypt a string using DES-EDE in CBC mode."));
	CheckRadioButton(1035, 1036, 1036);
	GetDlgItem(BTN_DATA_DOIT)->SetWindowText(_T("Decrypt It"));
	GetDlgItem(EDIT_PLAIN_TEXT)->EnableWindow(FALSE);
	GetDlgItem(EDIT_PASS_PHRASE)->EnableWindow(FALSE);
	GetDlgItem(EDIT_DATA_DECRYPTED)->SetWindowText(_T(""));
}

void CCryptestDlg::OnValidate() 
{
	CValidation dlgV;
	dlgV.DoModal();
}

void CCryptestDlg::OnBenchMarks() 
{
	CBenchMark dlgBM;
	dlgBM.DoModal();
}

void CCryptestDlg::OnTcpForwarding() 
{
	CTCPForwarding dlgTF;
	dlgTF.DoModal();
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager
Germany Germany
I have been programming (as a hobby) for 20+ years (Unix C, Scripting, VB, C/C++, C#). I am getting too old to talk about it and been in the Security line of work (both Military/Civilian) for 25+ years.

Comments and Discussions