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

📄 destestdlg.cpp

📁 实现DES的加密解密,有MFC界面,语言为C
💻 CPP
字号:
// DESTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DESTest.h"
#include "DESTestDlg.h"

#include "Encrypt.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()

/////////////////////////////////////////////////////////////////////////////
// CDESTestDlg dialog

CDESTestDlg::CDESTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDESTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDESTestDlg)
	m_strEncrypt = _T("");
	m_strSource = _T("");
	m_strDecrypt = _T("");
	m_strKey = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDESTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDESTestDlg)
	DDX_Control(pDX, IDC_EDT_KEY, m_edtKey);
	DDX_Control(pDX, IDC_EDT_DECRYPT, m_edtDecrypt);
	DDX_Control(pDX, IDC_EDT_SOURCE, m_edtSource);
	DDX_Control(pDX, IDC_EDT_ENCRYPT, m_edtEncrypt);
	DDX_Text(pDX, IDC_EDT_ENCRYPT, m_strEncrypt);
	DDV_MaxChars(pDX, m_strEncrypt, 255);
	DDX_Text(pDX, IDC_EDT_SOURCE, m_strSource);
	DDV_MaxChars(pDX, m_strSource, 255);
	DDX_Text(pDX, IDC_EDT_DECRYPT, m_strDecrypt);
	DDV_MaxChars(pDX, m_strDecrypt, 255);
	DDX_Text(pDX, IDC_EDT_KEY, m_strKey);
	DDV_MaxChars(pDX, m_strKey, 8);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDESTestDlg, CDialog)
	//{{AFX_MSG_MAP(CDESTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_ENCRYPT, OnBtnEncrypt)
	ON_BN_CLICKED(IDC_BTN_DECRYPT, OnBtnDecrypt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDESTestDlg message handlers

BOOL CDESTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	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);
		}
	}

	SetIcon(m_hIcon, TRUE);			
	SetIcon(m_hIcon, FALSE);		
	
	return TRUE; 
}

void CDESTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CDESTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		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;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CDESTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDESTestDlg::OnBtnEncrypt() 
{
	if (!UpdateData())
	{
		return;
	}
    
	if (m_strSource.IsEmpty())
	{
		AfxMessageBox(_T("请输入明文!"));
		m_edtSource.SetFocus();
		return;
	}

	if (m_strKey.IsEmpty())
	{
		AfxMessageBox(_T("请输入密钥!"));
		m_edtKey.SetFocus();
		return;
	}

	unsigned char key[8];
	memset(key, 0, 8);
	int nLen = min(8, m_strKey.GetLength());
   
	for (int i = 0; i < nLen; i++)
	{
		key[i] = m_strKey.GetAt(i);
	}

	char data[256];

	strcpy(data, m_strSource);

    CEncrypt encrypt;
	m_strEncrypt = encrypt.encrypt(key, data);

	UpdateData(FALSE);
}

void CDESTestDlg::OnBtnDecrypt() 
{
	if (!UpdateData())
	{
		return;
	}

	if (m_strEncrypt.IsEmpty())
	{
		AfxMessageBox(_T("请输入密文!"));
		m_edtEncrypt.SetFocus();
		return;
	}

	if (m_strKey.IsEmpty())
	{
		AfxMessageBox(_T("请输入密钥!"));
		m_edtKey.SetFocus();
		return;
	}

	unsigned char key[8];
	memset(key, 0, 8);
	int nLen = min(8, m_strKey.GetLength());
   
	for (int i = 0; i < nLen; i++)
	{
		key[i] = m_strKey.GetAt(i);
	}

	char data[256];

    strcpy(data, m_strEncrypt);

    CEncrypt encrypt;
	m_strDecrypt = encrypt.decrypt(key, data);

	UpdateData(FALSE);
}

⌨️ 快捷键说明

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