VC++中实现DES和RSA加密时候用到的源程序

源代码在线查看: rsa加解密算法dlg.cpp

软件大小: 46 K
上传用户: Numb_pqc
关键词: DES RSA VC 加密
下载地址: 免注册下载 普通下载 VIP

相关代码

				// 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;
					//}}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_DECRYPT, OnDecrypt)
					//}}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					{
						if((m_N[i]						   ((m_N[i]>'9')&&(m_N[i]						   ((m_N[i]>'F')&&(m_N[i]						   (m_N[i]>'f'))
						{
							m_OUT=_T("N必须为0-9或A-F或a-f组成的整数");
							UpdateData(FALSE);
							return;
						}
					}
					for(i=0;i					{
						if((m_E[i]						   ((m_E[i]>'9')&&(m_E[i]						   ((m_E[i]>'F')&&(m_E[i]						   (m_E[i]>'f'))
						{
							m_OUT=_T("E必须为0-9或A-F或a-f组成的整数");
							UpdateData(FALSE);
							return;
						}
					}
					for(i=0;i					{
						if((m_D[i]						   ((m_D[i]>'9')&&(m_D[i]						   ((m_D[i]>'F')&&(m_D[i]						   (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)					{
						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				    CTime t0=CTime::GetCurrentTime();
					P.Mov(0);
					Q.Mov(0);
					N.Mov(0);
					E.Mov(0);
					P.GetPrime(16);
					Q.GetPrime(16);
					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);
				    if(m_IN.GetLength()>256)
					{
						m_OUT=_T("N不得大于256位");
						UpdateData(FALSE);
						return;
					}
					for(int i=0;i					{
						if((m_IN[i]						   ((m_IN[i]>'9')&&(m_IN[i]						   ((m_IN[i]>'F')&&(m_IN[i]						   (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);
					UpdateData(FALSE);
				}
				
				void CRSADlg::OnDecrypt() 
				{
					if((ready==0)||(Q.m_ulValue[0]==0))
					{
						m_OUT=_T("请先进行加密");
				        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
					
				}
							

相关资源