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

📄 rsa加解密算法dlg.cpp

📁 rsa加密算法
💻 CPP
字号:
// RSA加解密算法Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "RSA加解密算法.h"
#include "RSA加解密算法Dlg.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_OUT = _T("");
	m_Len=0;
	flag=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_D, m_D);
	DDX_Text(pDX, IDC_E, m_E);
	DDX_Text(pDX, IDC_INPUT, m_IN);
	DDX_Text(pDX, IDC_N, m_N);
	DDX_Text(pDX, IDC_OUTPUT, m_OUT);
	DDX_CBIndex(pDX, IDC_COMBO, 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_PUT, OnButtonPut)
	ON_BN_CLICKED(IDC_GET, OnButtonGet)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}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
	
	// 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::OnButtonPut() 
{
UpdateData(TRUE);
	ready=0;
	if(m_N.GetLength()>256)
	{
		m_OUT=_T("N不得大于256位");
		UpdateData(FALSE);
		return;
	}
	if(m_E.GetLength()>256)
	{
		m_OUT=_T("E不得大于256位");
		UpdateData(FALSE);
		return;
	}
    if(m_D.GetLength()>10)
	{
		m_OUT=_T("N不得大于10位");
		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'))
		{
			m_OUT=_T("N必须为0-9或A-F或a-f组成的整数");
			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'))
		{
			m_OUT=_T("E必须为0-9或A-F或a-f组成的整数");
			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'))
		{
			m_OUT=_T("D必须为0-9或A-F或a-f组成的整数");
			UpdateData(FALSE);
			return;
		}
	}
	N.Get(m_N);
	D.Get(m_D);
	E.Get(m_E);
	if((N.Cmp(E)<=0)||(N.Cmp(D)<=0))
	{
		m_OUT=_T("N必须大于D、E");
		UpdateData(FALSE);
		return;
	}
	ready=1;
	Q.m_ulValue[0]=0;	
}

void CRSADlg::OnButtonGet() 
{
	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(8);
	Q.GetPrime(8);
	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_OUT.Format("%d",t.GetTotalSeconds());
	m_OUT+=" 秒";
	Q.m_ulValue[0]=0;
	UpdateData(FALSE);// TODO: Add your control notification handler code here
	
}

void CRSADlg::OnOK() 
{	
	if(ready==0)
	{
		m_OUT=_T("请先输入或生成N、D、E");
        UpdateData(FALSE);
		return;
	}
	UpdateData(TRUE);
	CFile file,file1,file2;
	file.Open(name,CFile::modeRead);
	if(name=="")
	{
		MessageBox("请选择加密文件!");return;
	}
	file1.Open("miwen.rsa",CFile::modeCreate|CFile::modeWrite);
	file2.Open("jiemi.rsa",CFile::modeCreate|CFile::modeWrite);
	int len=file.GetLength();
	CString str1,str2,str3;

	str1.Format("%d,len");
	file1.Write(str1,4);
	file1.Write(name,256);

	int m=len/32;
	bool n=!(len%32);
	for(int j=0;j<m+n;j++)
	{
		file.Read(m_IN.GetBuffer(32),32);
		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'))
			{
				m_OUT=_T("待加密数据必须为0-9或A-F或a-f组成的整数");
				UpdateData(FALSE);
				return;
			}
		}
		P.Get(m_IN);
		if(P.Cmp(N)>=0)
		{
			m_OUT=_T("待加密数据必须小于N");
			UpdateData(FALSE);
			return;
		}
		Q.Mov(P.RsaTrans(E,N));
		Q.Put(m_OUT);	
		file1.Write(m_OUT.GetBuffer(32),m_OUT.GetLength());
		Q.Get(m_OUT);
		P.Mov(Q.RsaTrans(D,N));
		P.Put(m_OUT);
		file2.Write(m_OUT.GetBuffer(32),m_OUT.GetLength());
	}
	file.Close();
	file1.Close();
	
	file2.Close();
	if(!flag)
	{MessageBox("加密数据已全部完毕,请您核对!");flag=1;}
	else if(flag)
	{MessageBox("解密数据已全部完毕,请您核对!");flag=0;}
	UpdateData(FALSE);
}



void CRSADlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
		CFileDialog ofn(TRUE, NULL, NULL,NULL,"All Files (*.*)|*.*||");
		//通用对话框类CfileDialog用来打开或保存文件,此处定义的该类对象ofn	
	   CString path;
	   if(ofn.DoModal()==IDOK)
	   {  	CString fn=ofn.GetPathName();
		    path=fn;
	   }
	   name=path;
}

⌨️ 快捷键说明

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