aesdemodlg.cpp

来自「加密速度快的AES算法源码」· C++ 代码 · 共 320 行

CPP
320
字号
// AESDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AESDemo.h"
#include "AESDemoDlg.h"
#include "windows.h"
#include "KAES.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()

/////////////////////////////////////////////////////////////////////////////
// CAESDemoDlg dialog

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

BEGIN_MESSAGE_MAP(CAESDemoDlg, CDialog)
//{{AFX_MSG_MAP(CAESDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnAesStr)
ON_BN_CLICKED(IDC_BUTTON4, OnFileEnAes)
ON_BN_CLICKED(IDC_BUTTON2, OnSelectFile)
ON_BN_CLICKED(IDC_BUTTON3, OnFileDeAes)
ON_BN_CLICKED(ID_CANCEL, OnOK)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAESDemoDlg message handlers

BOOL CAESDemoDlg::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
	SetDlgItemText(IDC_EDIT1,"请输入需加密字符");
	m_filename="";
	SetDlgItemText(IDC_EDIT2,"请输入密钥");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CAESDemoDlg::OnOK() 
{
	// TODO: Add extra validation here
	PostQuitMessage(0);
	//CDialog::OnOK();
}

void CAESDemoDlg::OnAesStr() 
{
	unsigned char inBuff[25],ouBuff[25];
	memset(inBuff,0,25);
	memset(ouBuff,0,25);
	rijndael rij;
	u1byte *keyBuff=new u1byte[256];
	GetDlgItemText(IDC_EDIT2,(char*)keyBuff,256);
	rij.set_key(keyBuff,256);
	GetDlgItemText(IDC_EDIT1,(char*)inBuff,24);
	if(strlen((char*)inBuff)>16)MessageBox("本例只能加密16字节的字符串,大于截断");
	//memcpy(inBuff,(unsigned char*)keyBuff,16);
	rij.encrypt(inBuff,ouBuff);//"dda97ca4......ec0d7191"
	delete[]keyBuff;
	CString str="",strTmp;
	for(int i=0;i<16;i++)
	{
		strTmp.Format("%02x",ouBuff[i]);
		str+=strTmp;
	}
	MessageBox(str,"加密后");
	//kaes.InvCipher(ouBuff,inBuff);
	//str="";
	//for(i=0;i<16;i++)
	//{
	//	strTmp.Format("%02x",inBuff[i]);
	//	str+=strTmp;
	//}
	//MessageBox(str,"解密后");
}

void CAESDemoDlg::OnFileEnAes() 
{
	if(m_filename=="")return;
	FILE* ff;
	FILE* fp;
	ff=fopen((LPCTSTR)m_filename,"rb");
	if(!ff)return;
	fseek(ff,0,SEEK_END);
	long length=ftell(ff);
	fseek(ff,0,SEEK_SET);
	m_filename+=".aen";
	rijndael rij;//加密类的对象
	long N=length/16;//每16字节一组
	int d=length%16;
	if(d) //作用是当明文长度不是128bit整数倍时调整N值
	{
		length=length+16-length%16;
		N=N+1;
	}
	unsigned char *inBuff=new u1byte[length+1];	
	fread(inBuff,1,length-16+d,ff);
	fclose(ff);
	
    if(d) //如果明文长度不是128bit的倍数用空格补齐剩余的位
	{
		for(int i=0;i<16-d;i++)
		{
			inBuff[length-16+d+i]=' ';
		}
	}
	u1byte *ouBuff;//用于指向加密后的密文
	ouBuff=new u1byte[length];
	u1byte *keyBuff=new u1byte[256];
	GetDlgItemText(IDC_EDIT2,(char*)keyBuff,256);//从文本框中取得密钥,将密钥拷贝到keyBuff指向的内存
	
	rij.set_key(keyBuff,256);//设置密钥
	for(int j=0;j<N;j++)//循环加密N次,每次128bit
	{
		rij.encrypt(inBuff,ouBuff);
		inBuff+=16;
		ouBuff+=16;
	}
	delete[]keyBuff;
	inBuff=inBuff-16*N;//明文指针回到明文头
	delete[]inBuff;
	ouBuff=ouBuff-16*N;//密文指针回到密文头
	fp=fopen((LPCTSTR)m_filename,"wb");
	fwrite(ouBuff,1,length,fp);//输出密文
	delete[]ouBuff;
	fclose(fp);
	// TODO: 在此添加控件通知处理程序代码	
	MessageBox("加密成功!");
	SetDlgItemText(IDC_FILESELECT,m_filename);
}

void CAESDemoDlg::OnSelectFile() 
{
	CString str;
	CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
	if(IDOK!=dlg.DoModal())return;
	m_filename=dlg.GetPathName();	
	SetDlgItemText(IDC_FILESELECT,m_filename);
}

void CAESDemoDlg::OnFileDeAes() 
{
	if(m_filename=="")return;
	CFile c_ff((LPCTSTR)m_filename,CFile::modeRead);
	m_filename+=".adn";
    rijndael rij;//加密类的对象
	long length=c_ff.GetLength();
	long N=length/16;//取得组数做为循环次数
	u1byte *inBuff=new u1byte[length+1];
	c_ff.Read(inBuff,length);//将明文拷贝到inBuff指向的内存
	c_ff.Close();
	u1byte *keyBuff=new u1byte[256];
	GetDlgItemText(IDC_EDIT2,(char*)keyBuff,256);//从文本框中取得密钥
	//将密钥拷贝到keyBuff指向的内存
	rij.set_key(keyBuff,256);//设置密钥
	u1byte *ouBuff;//用于指向加密后的密文
	ouBuff=new u1byte[length];
	
	for(int j=0;j<N;j++) //循环解密N次
	{
		rij.decrypt(inBuff,ouBuff);
		inBuff+=16;
		ouBuff+=16;
	}
	delete[]keyBuff;
	inBuff=inBuff-16*N;
		delete[]inBuff;
	ouBuff=ouBuff-16*N;
	CFile c_fp((LPCTSTR)m_filename,CFile::modeCreate|CFile::modeWrite);
	c_fp.Write(ouBuff,length);
	c_fp.Close();
	delete ouBuff;
	MessageBox("解密成功!");
	SetDlgItemText(IDC_FILESELECT,m_filename);	
}


⌨️ 快捷键说明

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