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

📄 desdlgdlg.cpp

📁 DES和MAC加解密算法
💻 CPP
字号:
// DesDlgDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DesDlg.h"
#include "DesDlgDlg.h"
#include "des.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()

/////////////////////////////////////////////////////////////////////////////
// CDesDlgDlg dialog

CDesDlgDlg::CDesDlgDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDesDlgDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDesDlgDlg)
	m_Key = _T("");
	m_Data = _T("");
	m_Edit3 = _T("");
	m_Edit4 = _T("");
	m_Radio = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDesDlgDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDesDlgDlg)
	DDX_Control(pDX, IDC_EDIT2, m_DataCtrl);
	DDX_Control(pDX, IDC_EDIT1, m_KeyCtrl);
	DDX_Text(pDX, IDC_EDIT1, m_Key);
	DDV_MaxChars(pDX, m_Key, 32);
	DDX_Text(pDX, IDC_EDIT2, m_Data);
	DDV_MaxChars(pDX, m_Data, 16);
	DDX_Text(pDX, IDC_EDIT3, m_Edit3);
	DDV_MaxChars(pDX, m_Edit3, 16);
	DDX_Text(pDX, IDC_EDIT4, m_Edit4);
	DDV_MaxChars(pDX, m_Edit4, 16);
	DDX_Radio(pDX, IDC_RADIO1, m_Radio);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDesDlgDlg, CDialog)
	//{{AFX_MSG_MAP(CDesDlgDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_UPDATE(IDC_EDIT1, OnUpdateEdit1)
	ON_EN_UPDATE(IDC_EDIT2, OnUpdateEdit2)
	ON_BN_CLICKED(ID_OK, OnOk)
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDesDlgDlg message handlers

BOOL CDesDlgDlg::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
	if(m_Radio==0)m_Key.Format("0000000000000000");
	else m_Key.Format("00000000000000000000000000000000");
	m_Data.Format("0000000000000000");
	m_Edit3.Format("");
	m_Edit4.Format("");
	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CDesDlgDlg::OnUpdateEdit1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_Key.GetLength()>0)
	{
		for(int i=0;i<m_Key.GetLength();i++)
		{
			if(
				!(
					((m_Key.GetAt(i)>='0')&&(m_Key.GetAt(i)<='9'))||
					((m_Key.GetAt(i)>='a')&&(m_Key.GetAt(i)<='f'))||
					((m_Key.GetAt(i)>='A')&&(m_Key.GetAt(i)<='F'))
				)
			  )
			{
				m_Key.Delete(i,1);
				UpdateData(FALSE);
				m_KeyCtrl.SetSel(i,i);
			}
		}
	}
}

void CDesDlgDlg::OnUpdateEdit2() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(m_Data.GetLength()>0)
	{
		for(int i=0;i<m_Data.GetLength();i++)
		{
			if(
				!(
					((m_Data.GetAt(i)>='0')&&(m_Data.GetAt(i)<='9'))||
					((m_Data.GetAt(i)>='a')&&(m_Data.GetAt(i)<='f'))||
					((m_Data.GetAt(i)>='A')&&(m_Data.GetAt(i)<='F'))
				)
			  )
			{
				m_Data.Delete(i,1);
				UpdateData(FALSE);
				m_DataCtrl.SetSel(i,i);
			}
		}
	}
}

void CDesDlgDlg::OnOk() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	unsigned char k[16],d[8],o1[8],o2[8];
	int i;
	char *stops,s[3];
	if((m_Key.GetLength()==16)&&(m_Data.GetLength()==16)&&(m_Radio==0))
	{
		for(i=0;i<8;i++)
		{
			s[0]=m_Key.GetAt(i*2); s[1]=m_Key.GetAt(i*2+1); s[3]=0x0;
			k[i]=(unsigned char)strtoul(s,&stops,16);
			s[0]=m_Data.GetAt(i*2); s[1]=m_Data.GetAt(i*2+1); s[3]=0x0;
			d[i]=(unsigned char)strtoul(s,&stops,16);
		}
		des8(d,k,o1,EN0,1);
		des8(o1,k,o2,DE1,1);
		m_Edit3.Format("%02X%02X%02X%02X%02X%02X%02X%02X",o1[0],o1[1],o1[2],o1[3],o1[4],o1[5],o1[6],o1[7]);
		m_Edit4.Format("%02X%02X%02X%02X%02X%02X%02X%02X",o2[0],o2[1],o2[2],o2[3],o2[4],o2[5],o2[6],o2[7]);
		UpdateData(FALSE);
	}
	if((m_Key.GetLength()==32)&&(m_Data.GetLength()==16)&&(m_Radio==1))
	{
		for(i=0;i<16;i++)
		{
			s[0]=m_Key.GetAt(i*2); s[1]=m_Key.GetAt(i*2+1); s[3]=0x0;
			k[i]=(unsigned char)strtoul(s,&stops,16);
		}
		for(i=0;i<8;i++)
		{
			s[0]=m_Data.GetAt(i*2); s[1]=m_Data.GetAt(i*2+1); s[3]=0x0;
			d[i]=(unsigned char)strtoul(s,&stops,16);
		}
		des16(d,k,o1,EN0,1);
		des16(o1,k,o2,DE1,1);
		m_Edit3.Format("%02X%02X%02X%02X%02X%02X%02X%02X",o1[0],o1[1],o1[2],o1[3],o1[4],o1[5],o1[6],o1[7]);
		m_Edit4.Format("%02X%02X%02X%02X%02X%02X%02X%02X",o2[0],o2[1],o2[2],o2[3],o2[4],o2[5],o2[6],o2[7]);
		UpdateData(FALSE);
	}
}

void CDesDlgDlg::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	if(m_Radio!=0)
	{
		UpdateData(TRUE);
		m_Key.Format("0000000000000000");
		m_Edit3.Format("");
		m_Edit4.Format("");
		UpdateData(FALSE);
	}
}

void CDesDlgDlg::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	if(m_Radio!=1)
	{
		UpdateData(TRUE);
		m_Key.Format("00000000000000000000000000000000");
		m_Edit3.Format("");
		m_Edit4.Format("");
		UpdateData(FALSE);
	}
}

⌨️ 快捷键说明

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