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

📄 rsadlg.cpp

📁 简单的RSA算法的加密解密程序
💻 CPP
字号:
// RSADlg.cpp : implementation file
//

#include "stdafx.h"
#include "RSA.h"
#include "RSADlg.h"
#include "BigInt.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()

/////////////////////////////////////////////////////////////////////////////
// CRSADlg dialog

CRSADlg::CRSADlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRSADlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRSADlg)
	m_D = _T("");
	m_E = _T("");
	m_IN = _T("");
	m_N = _T("");
	m_Q = _T("");
	m_P = _T("");
	m_OUT = _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 CRSADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRSADlg)
	DDX_Text(pDX, IDC_EDITD, m_D);
	DDX_Text(pDX, IDC_EDITE, m_E);
	DDX_Text(pDX, IDC_EDITINPUT, m_IN);
	DDX_Text(pDX, IDC_EDITN, m_N);
	DDX_Text(pDX, IDC_EDITQ, m_Q);
	DDX_Text(pDX, IDC_EDITP, m_P);
	DDX_Text(pDX, IDC_EDITOUTPUT, m_OUT);
	DDX_CBIndex(pDX, IDC_COMBOBOXEX1, m_Len);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRSADlg, CDialog)
	//{{AFX_MSG_MAP(CRSADlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
	ON_BN_CLICKED(IDC_BUTTON_GENERATE, OnButtonGenerate)
	ON_BN_CLICKED(IDC_BUTTON_PUT, OnButtonPut)
	ON_BN_CLICKED(IDC_DECRYPT, OnDecrypt)
	ON_BN_CLICKED(IDC_BUTTON_CANCEL, OnButtonCancel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRSADlg message handlers

BOOL CRSADlg::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
	
	ready=0;
	ready_d=0;
	ready_e=0;
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CRSADlg::OnEncrypt() 
{
	if(ready==0||ready_e==0)
	{
		MessageBox("请先输入或生成N、E!","提示");
        UpdateData(FALSE);
		return;
	}
	UpdateData(TRUE);
    if(m_IN.GetLength()>256)
	{
		MessageBox("输入不得大于256位!","提示");
		//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]>'Z')&&(m_IN[i]<'a'))||
		   (m_IN[i]>'z'))&& m_IN[i]!=32 )
		{
			MessageBox("待加密数据必须为数字或英文字母所组成!","提示");
			//UpdateData(FALSE);
			return;
		}
	}
	P.Get(m_IN,63);
	
	if(P.Cmp(N)>=0)
	{
		MessageBox("待加密数据必须小于N!","提示");
        UpdateData(FALSE);
		return;
	}
	Q.Mov(P.RsaTrans(E,N));
	Q.Put(m_OUT,63);

	
	UpdateData(FALSE);
	// TODO: Add your control notification handler code here
	
}

void CRSADlg::OnButtonGenerate() 
{
	ready=1;
	ready_d=1;
	ready_e=1;
	UpdateData(TRUE);
	int len=2;
	for(int i=0;i<m_Len;i++)
	{
		len*=2;
	}   
	P.Mov(62);
	Q.Mov(62);
	N.Mov(62);
	E.Mov(62);
	P.GetPrime(len);
	Q.GetPrime(len);
	P.Put(m_P);
	Q.Put(m_Q);
	N.Mov(P.Mul(Q));
	N.Put(m_N);
	P.m_ulValue[0]--;
	Q.m_ulValue[0]--;
	P.Mov(P.Mul(Q));    //@(n)
	E.GetPrime(1);      //随机取某一32位二进制素数
	E.Put(m_E);
	D.Mov(E.Euc(P));
	D.Put(m_D);
	UpdateData(FALSE);
	// TODO: Add your control notification handler code here
	
}

void CRSADlg::OnButtonPut() 
{
	UpdateData(TRUE);
	ready=0;
	if(m_N.GetLength()>256)    //判断N的位数
	{
		MessageBox("N不得大于256位!","提示");
		return;
	}
	if(m_E.GetLength()>16)
	{
		MessageBox("E不得大于16位!","提示");   //否则运算太慢	
		return;
	}
    if(m_D.GetLength()>256)
	{
		MessageBox("D不得大于256位!","提示");		
		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组成的整数!","提示");			
			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组成的整数!","提示");
			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组成的整数!","提示");			
			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!","提示");
		UpdateData(FALSE);
		return;
	}
	ready=1;
	if(m_D.GetLength()>0 && m_N.GetLength()>0)
		ready_d=1;
	if(m_E.GetLength()>0 && m_N.GetLength()>0)
		ready_e=1;	
	// TODO: Add your control notification handler code here
	
}

void CRSADlg::OnDecrypt() 
{
	if((ready==0)||(ready_d==0))
	{
		MessageBox("请先产生或输入密钥D和N!","提示");        
		return;
	}
	UpdateData(TRUE);
	Q.Get(m_IN,63);
	P.Mov(Q.RsaTrans(D,N));
    P.Put(m_OUT,63);
	UpdateData(FALSE);
	// TODO: Add your control notification handler code here
	
}

void CRSADlg::OnButtonCancel() 
{
	OnOK();
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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