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

📄 实验2dlg.cpp

📁 用从c/c++实现RSA加密算法的一个实验小程序
💻 CPP
字号:
// 实验2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "实验2.h"
#include "实验2Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMy2Dlg dialog

CMy2Dlg::CMy2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy2Dlg)
	m_n = _T("");
	m_e = _T("");
	m_d = _T("");
	m_tdisplay = _T("");
	m_out = _T("");
	m_in = _T("");
	m_len = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy2Dlg)
	DDX_Text(pDX, IDC_N, m_n);
	DDX_Text(pDX, IDC_E, m_e);
	DDX_Text(pDX, IDC_D, m_d);
	DDX_Text(pDX, IDC_TIME, m_tdisplay);
	DDX_Text(pDX, IDC_O, m_out);
	DDX_Text(pDX, IDC_I, m_in);
	DDX_CBIndex(pDX, IDC_COMBO, m_len);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy2Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON4, OnButtoninput)
	ON_BN_CLICKED(IDC_BUTTON3, OnButtonRnd)
	ON_BN_CLICKED(IDC_EN, OnEncryption)
	ON_BN_CLICKED(IDC_DN, OnDecryption)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy2Dlg message handlers

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

void CMy2Dlg::OnButtoninput() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	ready=0;
	if(m_n.GetLength()>256)
	{
		MessageBox("N不得大于256位","提示:",MB_OK);
		UpdateData(FALSE);
		return;
	}
	if(m_e.GetLength()>256)
	{
		MessageBox("E不得大于256位","提示:",MB_OK);
		UpdateData(FALSE);
		return;
	}
    if(m_d.GetLength()>10)
	{
		MessageBox("N不得大于10位","提示:",MB_OK);
		UpdateData(FALSE);
		return;
	}
	for(int i=0;i<m_n.GetLength();i++)
	{
		if((m_n[i]<'0')||
		   ((m_n[i]>'9')&&(m_n[i]<'A'))||
		   ((m_n[i]>'F')&&(m_n[i]<'a'))||
		   (m_n[i]>'f'))
		{
			MessageBox("N必须为0-9或A-F或a-f组成的整数","提示:",MB_OK);
			UpdateData(FALSE);
			return;
		}
	}
	for(i=0;i<m_e.GetLength();i++)
	{
		if((m_e[i]<'0')||
		   ((m_e[i]>'9')&&(m_e[i]<'A'))||
		   ((m_e[i]>'F')&&(m_e[i]<'a'))||
		   (m_e[i]>'f'))
		{
			MessageBox("E必须为0-9或A-F或a-f组成的整数","提示:",MB_OK);
			UpdateData(FALSE);
			return;
		}
	}
	for(i=0;i<m_d.GetLength();i++)
	{
		if((m_d[i]<'0')||
		   ((m_d[i]>'9')&&(m_d[i]<'A'))||
		   ((m_d[i]>'F')&&(m_d[i]<'a'))||
		   (m_d[i]>'f'))
		{
	    	MessageBox("D必须为0-9或A-F或a-f组成的整数","提示:",MB_OK);
			UpdateData(FALSE);
			return;
		}
	}
	N.Get(m_n);
	D.Get(m_d);
	E.Get(m_e);
	if((N.Cmp(E)<=0)||(N.Cmp(D)<=0))
	{
		MessageBox("N必须大于D、E","提示:",MB_OK);
		UpdateData(FALSE);
		return;
	}
	ready=1;
	Q.m_ulValue[0]=0;	
	GetDlgItem(IDC_I)->EnableWindow(true);
	GetDlgItem(IDC_EN)->EnableWindow(true);
	GetDlgItem(IDC_DN)->EnableWindow(true);
	GetDlgItem(IDC_O)->EnableWindow(true);
}

void CMy2Dlg::OnButtonRnd() 
{
	// TODO: Add your control notification handler code here
	ready=1;
	UpdateData(TRUE);
	int len=2;
	for(int i=0;i<m_len;i++){len*=2;}
    CTime t0=CTime::GetCurrentTime();
	P.Mov(0);
	Q.Mov(0);
	N.Mov(0);
	E.Mov(0);
	P.GetPrime(len);
	Q.GetPrime(len);
	N.Mov(P.Mul(Q));
	N.Put(m_n);
	P.m_ulValue[0]--;
	Q.m_ulValue[0]--;
	P.Mov(P.Mul(Q));
	D.Mov(0x10001);
	m_d="10001";
	E.Mov(D.Euc(P));
	E.Put(m_e);
    CTime t1=CTime::GetCurrentTime();
    CTimeSpan t=t1-t0;
	m_tdisplay.Format("%d",t.GetTotalSeconds());
	m_tdisplay+=" 秒";
	Q.m_ulValue[0]=0;
	UpdateData(FALSE);// TODO: Add your control notification handler code here
	GetDlgItem(IDC_I)->EnableWindow(true);
	GetDlgItem(IDC_EN)->EnableWindow(true);
	GetDlgItem(IDC_DN)->EnableWindow(true);
	GetDlgItem(IDC_O)->EnableWindow(true);

}

void CMy2Dlg::OnEncryption() 
{
	// TODO: Add your control notification handler code here
	if(ready==0)
	{
		MessageBox("请先输入或生成N、D、E","提示:",MB_OK);
        UpdateData(FALSE);
		return;
	}
	UpdateData(TRUE);
    if(m_in.GetLength()>256)
	{
		MessageBox("N不得大于256位","提示:",MB_OK);
		m_in="";
		UpdateData(FALSE);
		return;
	}
	for(int i=0;i<m_in.GetLength();i++)
	{
		if((m_in[i]<'0')||
		   ((m_in[i]>'9')&&(m_in[i]<'A'))||
		   ((m_in[i]>'F')&&(m_in[i]<'a'))||
		   (m_in[i]>'f'))
		{
			MessageBox("待加密数据必须为0-9或A-F或a-f组成的整数","提示:",MB_OK);
			m_in="";
			UpdateData(FALSE);
			return;
		}
	}
	P.Get(m_in);
	if(P.Cmp(N)>=0)
	{
		MessageBox("待加密数据必须小于N","提示:",MB_OK);
		m_in="";
        UpdateData(FALSE);
		return;
	}
	Q.Mov(P.RsaTrans(E,N));
	Q.Put(m_out);
	UpdateData(FALSE);
}

void CMy2Dlg::OnDecryption() 
{
	// TODO: Add your control notification handler code here
	if((ready==0)||(Q.m_ulValue[0]==0))
	{
		MessageBox("请先进行加密","提示:",MB_OK);
        UpdateData(FALSE);
		return;
	}
	Q.Get(m_out);
	P.Mov(Q.RsaTrans(D,N));
	P.Put(m_out);
	UpdateData(FALSE);// TODO: Add your control notification handler code here
}

⌨️ 快捷键说明

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