⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 learn_mfcdlg.cpp

📁 aes cbc mode source code
💻 CPP
字号:
// Learn_MFCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Learn_MFC.h"
#include "Learn_MFCDlg.h"
#include "aes.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()

/////////////////////////////////////////////////////////////////////////////
// CLearn_MFCDlg dialog

CLearn_MFCDlg::CLearn_MFCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLearn_MFCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLearn_MFCDlg)
	m_input_txt = _T("");
	m_encrypted_txt = _T("");
	m_decrypted_txt = _T("");
	m_input_type = -1;
	m_key = _T("");
	m_init_vector = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLearn_MFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLearn_MFCDlg)
	DDX_Control(pDX, IDC_BUTTON_CLR_KEY, m_clr_key);
	DDX_Control(pDX, IDC_BUTTON_ENCRYPT, m_encrypt);
	DDX_Control(pDX, IDC_BUTTON_DECRYPT, m_decrypt);
	DDX_Control(pDX, IDC_BUTTON_CLEAR_TEXT, m_clr_txt);
	DDX_Text(pDX, IDC_EDIT_IN_TXT, m_input_txt);
	DDX_Text(pDX, IDC_EDIT_EN_TXT, m_encrypted_txt);
	DDX_Text(pDX, IDC_EDIT_DE_TXT, m_decrypted_txt);
	DDX_Text(pDX, IDC_EDIT_KEY, m_key);
	DDV_MaxChars(pDX, m_key, 32);
	DDX_Text(pDX, IDC_INIT_VECTOR, m_init_vector);
	DDV_MaxChars(pDX, m_init_vector, 32);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLearn_MFCDlg, CDialog)
	//{{AFX_MSG_MAP(CLearn_MFCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CLEAR_TEXT, OnButtonClearText)
//	ON_BN_CLICKED(IDC_RADIO_CLR_TXT, OnRadioClrTxt)
	ON_BN_CLICKED(IDC_RADIO_CIP_TXT, OnRadioCipTxt)
	ON_EN_CHANGE(IDC_EDIT_EN_TXT, OnChangeEditEnTxt)
	ON_EN_CHANGE(IDC_EDIT_IN_TXT, OnChangeEditInTxt)
	ON_EN_CHANGE(IDC_EDIT_DE_TXT, OnChangeEditDeTxt)
	ON_EN_CHANGE(IDC_EDIT_KEY, OnChangeEditKey)
	ON_BN_CLICKED(ID_QUIT, OnQuit)
	ON_BN_CLICKED(IDC_BUTTON_CLR_KEY, OnButtonClrKey)
	ON_BN_CLICKED(IDC_BUTTON_ENCRYPT, OnButtonEncrypt)
	ON_BN_CLICKED(IDC_BUTTON_DECRYPT, OnButtonDecrypt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLearn_MFCDlg message handlers

BOOL CLearn_MFCDlg::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 -- KV
	m_input_txt = "Enter your input text here and select input text type";
	m_key = "FACA37E0B0C85373DF706E73F7C9AF86";
	m_init_vector = "52D0C29FF8793A519BD6A8289FC80E6A";
	//m_input_type = 0;
	// Update the dialog with the values -- KV
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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



void CLearn_MFCDlg::Char2Hex(const unsigned char ch, char *szHex)
{
	unsigned char byte[2];
	byte[0] = ch/16;
	byte[1] = ch%16;
	for(int i=0; i<2; i++)
	{
		if(byte[i] >= 0 && byte[i] <= 9)
			szHex[i] = '0' + byte[i];
		else
			szHex[i] = 'A' + byte[i] - 10;
	}
	szHex[2] = 0;
}

void CLearn_MFCDlg::Hex2Char(const char *szHex, unsigned char &rch)
{
	rch = 0;
	for(int i=0; i<2; i++)
	{
		if(*(szHex + i) >='0' && *(szHex + i) <= '9')
			rch = (rch << 4) + (*(szHex + i) - '0');
		else if(*(szHex + i) >='A' && *(szHex + i) <= 'F')
			rch = (rch << 4) + (*(szHex + i) - 'A' + 10);
		else if(*(szHex + i) >='a' && *(szHex + i) <= 'f')
			rch = (rch << 4) + (*(szHex + i) - 'a' + 10);
		else
		{
			MessageBox("Illegal input.  Allows only 0-9 or a-f or A-F",
					"Warning",
					MB_OK);
			break;
		}
	}
}



void CLearn_MFCDlg::CharStr2HexStr(const char *pucCharStr, char *pszHexStr, int iSize)
{
	int i;
	char szHex[3];
	pszHexStr[0] = 0;
	for(i=0; i<iSize; i++)
	{
		Char2Hex(pucCharStr[i], szHex);
		strcat(pszHexStr, szHex);
	}
}

void CLearn_MFCDlg::HexStr2CharStr(const char *pszHexStr, char *pucCharStr, int iSize)
{
	int i;
	unsigned char ch;
	for(i=0; i<iSize/2; i++)
	{
		Hex2Char(pszHexStr+2*i, ch);
		pucCharStr[i] = ch;
	}
//	pucCharStr = 0;
}


void CLearn_MFCDlg::OnButtonClearText() 
{
	// TODO: Add your control notification handler code here -- KV
	// Clear input values
	m_input_txt = "";
	m_decrypted_txt = "";
	m_encrypted_txt = "";
	UpdateData(FALSE);
	
}

void CLearn_MFCDlg::OnRadioClrTxt() 
{
	// TODO: Add your control notification handler code here
	m_input_type = 0;
	UpdateData(FALSE);
}

void CLearn_MFCDlg::OnRadioCipTxt() 
{
	// TODO: Add your control notification handler code here -- KV
	m_input_type = 1;
	UpdateData(FALSE);
}

void CLearn_MFCDlg::OnChangeEditEnTxt() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here 
	
}

void CLearn_MFCDlg::OnChangeEditInTxt() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	//m_input_txt += m_input_txt;
	//UpdateData(FALSE);
}

void CLearn_MFCDlg::OnChangeEditDeTxt() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CLearn_MFCDlg::OnChangeEditKey() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here

}

void CLearn_MFCDlg::OnQuit() 
{
	// TODO: Add your control notification handler code here
	// Exit the program -- KV
	OnOK();

}

void CLearn_MFCDlg::OnButtonClrKey() 
{
	// TODO: Add your control notification handler code here -- KV
	m_key = "";
	UpdateData(FALSE);
	
}

void CLearn_MFCDlg::OnButtonEncrypt() 
{
	// TODO: Add your control notification handler code here
	Aes aes;
	char pCipTxt[TXT_SIZE];
	char pResTxt[TXT_SIZE];
	CString		iv;
	char hexStrBuf[600];
	char inputBuf[600];
	char keyBuf[17];
	char ivBuf[17];
	Uint8	*pPadPtr;
	int		inStrSize;

	UpdateData(TRUE);
	switch(m_input_type)
	{
	case 0:
		m_encrypted_txt = "Clear text";
		break;
	case 1:
		m_encrypted_txt = "Cipher text";
		break;
	default: 
		m_encrypted_txt = "No option";
		break;
	}

	// convert inputs hex string to char string
	pPadPtr = (Uint8 *)(LPCTSTR)m_input_txt;
	inStrSize = m_input_txt.GetLength();
	if (inStrSize % 2)
	{
		//(LPCTSTR)m_input_txt += 1;
		inStrSize++;
		pPadPtr--;
		*pPadPtr = '0';

	}

	//HexStr2CharStr((LPCTSTR)m_input_txt, inputBuf, m_input_txt.GetLength());
	HexStr2CharStr((LPCTSTR)pPadPtr, inputBuf, inStrSize);
	inputBuf[inStrSize / 2 ] = '\0';
	HexStr2CharStr((LPCTSTR)m_init_vector, ivBuf, m_init_vector.GetLength());
	ivBuf[17] = '\0';
	HexStr2CharStr((LPCTSTR)m_key, keyBuf, m_key.GetLength());
	keyBuf[17]= '\0';

	if (strlen(keyBuf) -1 != BLK_SIZE)
	{
		MessageBox("Key length must be 16 bytes",
					"Warning",
					MB_OK);
		return;
	}

	memset(pResTxt, 0, TXT_SIZE); // clear output entry

	aes.aesSetIV((unsigned char *)ivBuf); // set the IV vector
	//aes.aesSetIV((unsigned char *)(LPCTSTR)m_init_vector); // set the IV vector

//	aes.aesEncrypt((unsigned char *)(LPCTSTR)m_input_txt, 
	aes.aesEncrypt((unsigned char *)inputBuf, 
					(unsigned char *)keyBuf, 
					(unsigned char *)pCipTxt);
	CharStr2HexStr(pCipTxt, hexStrBuf, strlen(pCipTxt));
	m_encrypted_txt = (CString)hexStrBuf; // encrypted text
	//m_encrypted_txt = (CString)pCipTxt; // encrypted text
	//m_decrypted_txt = (CString)pResTxt; // decrypted text
	UpdateData(FALSE);
}




/***************************************************
 * FUNCTION: OnButtonDecrypt
 *
 * DESCRIPTION
 *
 ***************************************************/
void CLearn_MFCDlg::OnButtonDecrypt() 
{
	// TODO: Add your control notification handler code here
	Aes aes;
	char pCipTxt[TXT_SIZE];
	char pResTxt[TXT_SIZE];
	char	cipherBuf[600];
	char	keyBuf[17];
	char	ivBuf[17];
	int tmp, tmp2;
	char hexStrBuf[600];
	
	UpdateData(TRUE);

	
	memset(pResTxt, 0, TXT_SIZE); // clear output entry

	// convert inputs hex string to char string
	tmp2 = m_init_vector.GetLength ();
	
	HexStr2CharStr((LPCTSTR)m_key, keyBuf, m_key.GetLength ());
	HexStr2CharStr((LPCTSTR)m_init_vector, ivBuf, m_init_vector.GetLength ());		
	HexStr2CharStr((LPCTSTR)m_encrypted_txt, cipherBuf, m_encrypted_txt.GetLength ()); 

	ivBuf[17] = '\0';
	keyBuf[17] = '\0';
	cipherBuf[m_encrypted_txt.GetLength ()/2 ] = '\0';
	tmp = strlen(keyBuf);
	if (strlen(keyBuf) -1 != BLK_SIZE)
	{
		MessageBox("Key length must be 17 bytes",
					"Warning",
					MB_OK);
		return;
	}


	//aes.aesSetIV((unsigned char *)(LPCTSTR)m_init_vector); // set the IV vector
	aes.aesSetIV((unsigned char *)ivBuf); // set the IV vector
	
	//aes.aesDecrypt((unsigned char *)(LPCTSTR)m_encrypted_txt, 
	aes.aesDecrypt((unsigned char *)cipherBuf,
					(unsigned char *)keyBuf, 
					(unsigned char *)pResTxt);
	
	CharStr2HexStr(pResTxt, hexStrBuf, strlen(pResTxt));
	m_decrypted_txt = (CString)hexStrBuf; // decrypted text
	//m_decrypted_txt = (CString)pResTxt; // decrypted text
	UpdateData(FALSE);	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -