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

📄 mytestaesdlg.cpp

📁 这是一个简单的AES加密解密的程序
💻 CPP
字号:
// mytestaesDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mytestaes.h"
#include "mytestaesDlg.h"
#include <math.h>

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

#include "AesInterface.h"
#pragma comment(lib,"MyAesLib.lib")

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMytestaesDlg dialog

CMytestaesDlg::CMytestaesDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMytestaesDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMytestaesDlg)
		// 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 CMytestaesDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMytestaesDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMytestaesDlg, CDialog)
	//{{AFX_MSG_MAP(CMytestaesDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMytestaesDlg message handlers

BOOL CMytestaesDlg::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 CMytestaesDlg::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 CMytestaesDlg::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 CMytestaesDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//Function to convert unsigned char to string of length 2
void Char2Hex(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;
}

//Function to convert string of length 2 to unsigned char
void Hex2Char(char const* 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
			break;
	}
}    

//Function to convert string of unsigned chars to string of chars
void CharStr2HexStr(unsigned char const* 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);
	}
}

//Function to convert string of chars to string of unsigned chars
void HexStr2CharStr(char const* pszHexStr, unsigned char* pucCharStr, int iSize)
{
	int i;
	unsigned char ch;
	for(i=0; i<iSize; i++)
	{
		Hex2Char(pszHexStr+2*i, ch);
		pucCharStr[i] = ch;
	}
}


const char AES_KEY[] = {"1234567890123456"};
enum CRYPT_FLAG
{
	ENCRPT = 0,
	DECRPT
};
int GetCryptLen(int _val)
{
	int iReturnVal = 0;
	int keyLen = strlen(AES_KEY);
	if(_val%keyLen==0)
		iReturnVal = _val/keyLen;
	else
		iReturnVal = _val/keyLen + 1;

	return iReturnVal * keyLen;
}
CString CryptString(LPCTSTR _str,CRYPT_FLAG _flag)
{
	int keyLen = strlen(AES_KEY);
	int len = strlen(_str);
	int newLen = GetCryptLen(len);
	CString result = "";
			
	TCHAR* outTmp = new TCHAR[newLen+1];
	memset(outTmp, 0, newLen+1);
	//outTmp[newLen] = '\0';

	IAesInterface* IAes = CreateAesInstance();
	if(IAes&&_str)
	{
		try
		{
			IAes->MakeKey(AES_KEY, keyLen, keyLen);
			if(_flag==ENCRPT)
				IAes->Encrypt(_str, outTmp, newLen, IAes->ECB);
			else
				IAes->Decrypt(_str, outTmp, newLen, IAes->ECB);
			result = outTmp;
			DestroyAesInstance(IAes);
		}
		catch(CException&)
		{
			DestroyAesInstance(IAes);
		}
	}	
	delete[] outTmp;
	return result;
}

void CMytestaesDlg::OnOK() 
{
	// TODO: Add extra validation here
	CString info = _T("* Internet Explorer 版本为: 6.0.2900.218\r\n * 密码输入控件版本: 1.0.1.0\r\n");
	//info += _T("* 日志输入控件版本为: 1.0.1.0\r\n");
	//info += _T("* 签名通控件版本为: 1.0.1.0\r\n");
	//info += _T("* CSP (加密服务提供商):\r\n");
	//info += _T("1:Dean Base Cryptographic Provider v1.0\r\n");

	/*
	int len2 = info.GetLength();
	//const char* in = "hellohellohello";
	CString str = CryptString(info,ENCRPT);
	//save to the file
	CFile file;
	file.Open("c:\\mylog.htp",CFile::modeCreate|CFile::modeWrite);
	int len1 = str.GetLength();
	file.Write(str,len1);
	file.Close();

	file.Open("c:\\mylog.htp",CFile::modeRead);

	TCHAR* strout = new TCHAR[len1+1];
	strout[len1] = TEXT('\0');
	file.Read(strout,len1*sizeof(TCHAR));
	CString lastout = CryptString(strout,DECRPT);
	int len = lastout.GetLength();

	file.Close();

	DeleteFile("c:\\mylog.htp");
	*/
	//get data from the file
	
	int len1 = info.GetLength();
	CString str = CryptString(info,ENCRPT);
	int len2 = str.GetLength();
	LPTSTR out = str.GetBuffer(str.GetLength());
	str = CryptString(out,DECRPT);
	int len = str.GetLength();
	CDialog::OnOK();
}

⌨️ 快捷键说明

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