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

📄 aesdlg.cpp

📁 DES加解密 AES(使用了文件读写的缓冲和多线程技术)
💻 CPP
字号:
// AESDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AES.h"
#include "AESDlg.h"
#include "Link.h"	// Added by ClassView


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


#include"link.h"
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CLink	m_QqId;
	CLink	m_EmailLink;
	//}}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)
	virtual BOOL OnInitDialog();
	//}}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)
	DDX_Control(pDX, IDC_QQ_STATIC, m_QqId);
	DDX_Control(pDX, IDC_EMAIL_STATIC, m_EmailLink);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAESDlg dialog

CAESDlg::CAESDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAESDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAESDlg)
	m_keylen = _T("");
	m_kltype = _T("");
	m_key = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAESDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAESDlg)
	DDX_Control(pDX, IDC_TAB, m_tab);
	DDX_Control(pDX, IDC_LENTYPE, m_lentype);
	DDX_Text(pDX, IDC_KEYLEN, m_keylen);
	DDX_Text(pDX, IDC_KEYLENTYPE, m_kltype);
	DDX_Text(pDX, IDC_KEY, m_key);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAESDlg, CDialog)
	//{{AFX_MSG_MAP(CAESDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_LENTYPE, OnSelchangeLentype)
	ON_BN_CLICKED(IDC_INFOBUT, OnInfoBut)
	ON_EN_CHANGE(IDC_KEY, OnChangeKey)
	ON_BN_CLICKED(IDC_BUTIMPORT, OnButImport)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAESDlg message handlers

BOOL CAESDlg::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  CFileDlg   CStrDlg
	CString strdate;
	m_lentype.AddString("128位");
	m_lentype.AddString("192位");
	m_lentype.AddString("256位");
	m_key.Format("Rijndael Encrypt");
	m_lentype.SetCurSel(0);
	m_keylen.Format("16");
	m_kltype.Format("128位");
	m_tab.InsertItem(0,"加密/解密字符串");
	m_tab.InsertItem(1,"加密/解密文件");
	m_tab.lpStrDlg=new CStrDlg();
	m_tab.lpStrDlg->Create(IDD_STRDLG,&m_tab);
	m_tab.lpFileDlg=new CFileDlg();
	m_tab.lpFileDlg->Create(IDD_FILEDLG,&m_tab);
	CRect rect;
	m_tab.GetClientRect(&rect);
	rect.InflateRect(-1,-30,-2,-2);
	m_tab.lpStrDlg->MoveWindow(rect);
	m_tab.lpStrDlg->ShowWindow(SW_SHOW);
	m_tab.lpFileDlg->ShowWindow(SW_HIDE);
////////////////////////////设置状态栏///////////////////////	
	t=CTime::GetCurrentTime();
	m_StatusBar.EnableAutomation();
	m_StatusBar.Create(WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),this,0);//ID_STATUSBAR);
	int width[]={1,380,556,1};
	m_StatusBar.SetParts(4, &width[0]);
	m_StatusBar.SetText("梦幻组合--魏嘉银、张科、许孝勇",0,1);
	strdate.Format("当前时间:%s",t.Format("%Y年%m月%d日"));
	m_StatusBar.SetText(strdate,0,2);
////////////////////////////设置状态栏///////////////////////

	UpdateData(false);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

//DEL void CAESDlg::SetFlieLen(long len,long count)
//DEL {
//DEL 	m_FileLen=len;
//DEL 	m_Count=count;	
//DEL }

void CAESDlg::OnSelchangeLentype() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	switch(m_lentype.GetCurSel())
	{
	case 0:m_kltype.Format("128位");m_keylen.Format("16");break;
	case 1:m_kltype.Format("192位");m_keylen.Format("24");break;
	case 2:m_kltype.Format("256位");m_keylen.Format("32");break;
	}
	UpdateData(false);	
}

void CAESDlg::OnInfoBut() 
{
	// TODO: Add your control notification handler code here
	CAboutDlg about;
	about.DoModal();
}

void CAESDlg::OnChangeKey() 
{
	// 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

	CString temp;
	int len,count;
	temp=m_key;
	UpdateData(true);
	len=m_key.GetLength();
	if(len==0)//长度为0时直接返回
		return;
	if(len>atoi(m_keylen))//长度超过规定长度
	{
		m_key=temp;
		MessageBox("所给的密钥长度超过规定!","警告",MB_OK|MB_ICONWARNING);
	}
	else
		if(len<atoi(m_keylen))//长度不足规定长度
		{
			count=atoi(m_keylen)/len-1;//计算进行多少次的密钥直接复制到临时密钥的末尾
			m_keytemp=m_key;
			for(len=0;len<count;len++)
			{
				temp.Format("%s%s",m_keytemp,m_key);
				m_keytemp=temp;
			}
			count=atoi(m_keylen)-m_keytemp.GetLength();
			
			if(count!=0)//进行count次的密钥直接复制后还未达到规定长度
			{
				temp.Format("%s%s",m_keytemp,m_key.Left(count));
				m_keytemp=temp;				
			}
		}
	UpdateData(false);
}

void CAESDlg::OnButImport() 
{
	// TODO: Add your control notification handler code her	
	UpdateData(true);
	CString lpszFilter,KeyFileName;
	CFile KeyFile;
	char *kfname,*Key;
	int i,len;
	lpszFilter.Format("文本文件(*.txt)|*.txt|密钥文件(*.key)|*.key|所有文件(*.*)|*.*||");
	CFileDialog filedlg(true,NULL,NULL,OFN_HIDEREADONLY,lpszFilter,NULL);
	if(filedlg.DoModal()==IDOK)
	{
		KeyFileName=filedlg.GetPathName();
		kfname=new char[(KeyFileName.GetLength()+1)];
		if(kfname==NULL)
		{
			MessageBox("分配内存失败,请稍候再运行本程序!");
			return ;
		}
		for(i=0;i<KeyFileName.GetLength();i++)
			kfname[i]=KeyFileName.GetAt(i);
		kfname[i]='\0';	
		if(KeyFile.Open(kfname,CFile::modeRead|CFile::shareDenyRead)==0)
		{
			MessageBox("无法打开密钥文件!","错误",MB_OK|MB_ICONWARNING);
			delete []kfname;
			return ;//打开文件失败		
		}
		delete []kfname;
		len=KeyFile.GetLength();//取得文件长度(字节)
		if(atoi(m_keylen)>len)
		{
			MessageBox("密钥文件的文件长度小于所选密钥类型的长度要求!","错误",MB_OK|MB_ICONWARNING);
			KeyFile.Close();
			return ;//打开文件失败		
		}
		len=atoi(m_keylen)+1;
		Key=new char[len];
		if(Key==NULL)
		{
			MessageBox("分配内存失败,请稍候再运行本程序!");
			return ;
		}
		KeyFile.Read(Key,len);//从密钥文件中读入密钥
		for(i=0;i<len;i++)//将无法显示的字符转换成空格
			if(Key[i]<=20)Key[i]=' ';
		Key[len-1]='\0';
		m_key.Format("%s",Key);
		KeyFile.Close();
		delete []Key;
	}
	UpdateData(false);	
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_EmailLink.SetToolTipText("欢迎给我发电子邮件");
	m_QqId.SetToolTipText("欢迎与我用QQ进行交流");
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

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