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

📄 rsaprojectdlg.cpp

📁 /* RSA Demo 1.0 版 * 版权所有 (C) 2004 赵春生 * 2004.04.25 * http://timw.yeah.net * http://timw.126.com * 本程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************/
/* Project : RSA Encryption and Decryption	   	     	 	     	 */
/*           using Miracl				  	     	      	      	 */
/* Name    : Angela TENG - Xavier GRICOURT	     	     	     	 */
/* File    : RSAProjectDlg.cpp			  	     	     	     	 */
/* Date    : 03/14/2002                   	     	     	         */
/* Comment : Miracl source codes are provided by Shamus Software Ltd.*/
/* The encryption exponent is fixed to e =3. If not it takes to long.*/
/* We have tried with exponents we could read from a file like the   */
/* other parameters the encryption was too long.					 */
/*********************************************************************/

#include "stdafx.h"
#include "RSAProject.h"
#include "RSAProjectDlg.h"
#include <fstream.h>
#include <string.h>
#include <big.h>   /* include MIRACL system */
#include <crt.h>   /* chinese remainder thereom */

#define NP 2       /* two primes */

Miracl precision=100;

#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()

/////////////////////////////////////////////////////////////////////////////
// CRSAProjectDlg dialog

CRSAProjectDlg::CRSAProjectDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRSAProjectDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRSAProjectDlg)
	m_PRK_DEC = _T("");
	m_FILE_ENC = _T("");
	m_PBK_DEC = _T("");
	m_PBK_ENC = _T("");
	m_FILE_DEC = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRSAProjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRSAProjectDlg)
	DDX_Text(pDX, IDC_PRIVATE_DEC, m_PRK_DEC);
	DDX_Text(pDX, IDC_FILE_ENC, m_FILE_ENC);
	DDX_Text(pDX, IDC_PUBLIC_DEC, m_PBK_DEC);
	DDX_Text(pDX, IDC_PUBLIC_ENC, m_PBK_ENC);
	DDX_Text(pDX, IDC_FILE_DEC, m_FILE_DEC);
	DDX_Control(pDX, IDC_PT_RADIO, m_PT);
	DDX_Control(pDX, IDC_CT_RADIO, m_CT);
	DDX_Control(pDX, IDC_DCT_RADIO, m_DCT);
	DDX_Control(pDX, IDC_DT_RADIO, m_DT);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRSAProjectDlg, CDialog)
	//{{AFX_MSG_MAP(CRSAProjectDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnOpenFileEnc)
	ON_BN_CLICKED(IDC_BUTTON4, OnOpenFileDec)
	ON_BN_CLICKED(IDC_BUTTON3, OnPBKEnc)
	ON_BN_CLICKED(IDC_BUTTON5, OnPRKDec)
	ON_BN_CLICKED(IDC_BUTTON6, OnPBKDec)
	ON_BN_CLICKED(IDC_BUTTON7, OnEncrypt)
	ON_BN_CLICKED(IDC_BUTTON11, OnOpenDC)
	ON_BN_CLICKED(IDC_BUTTON9, OnOpenEC)
	ON_BN_CLICKED(IDC_BUTTON8, OnDecrypt)
	ON_BN_CLICKED(IDC_GEN, OnGenerate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRSAProjectDlg message handlers

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

void CRSAProjectDlg::OnOpenFileEnc() 
{
	CString sPathToFile;
	CFileDialog dlg( TRUE,
					 "*.dbt",
					 NULL,
					 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
					 "All Files (*.*)|*.*|" );
	
	// initiate dialog with user and check which button was pressed
	if( dlg.DoModal() == IDOK ) {

		// retrieve the path to the file
		sPathToFile = dlg.GetPathName();

		UpdateData(TRUE);
		// save the path to the file
		m_FILE_ENC = sPathToFile;
		UpdateData(FALSE);

	}
	
}



/*************  Implementation *************/


/* Browser funtion to retrieve the file to encrypt */
void CRSAProjectDlg::OnOpenFileDec() 
{
CString sPathToFile;
	
	CFileDialog dlg( TRUE,
					 "*.dbt",
					 NULL,
					 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
					 "RSA Files (*.rsa)|*.rsa|" );

	// initiate dialog with user and check which button was pressed
	if( dlg.DoModal() == IDOK ) {

		// retrieve the path to the file
		sPathToFile = dlg.GetPathName();

		// save the path to the file
		UpdateData(TRUE);
		m_FILE_DEC = sPathToFile;
		UpdateData(FALSE);

	}
}

/* Browser funtion to retrieve the public key file for the encryption part */
void CRSAProjectDlg::OnPBKEnc() 
{
	CString sPathToFile;
	CFileDialog dlg( TRUE,
					 "*.dbt",
					 NULL,
					 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
					 "Key Files (*.key)|*.key|" );
	
	// initiate dialog with user and check which button was pressed
	if( dlg.DoModal() == IDOK ) {

		// retrieve the path to the file
		sPathToFile = dlg.GetPathName();

		UpdateData(TRUE);
		// save the path to the file
		m_PBK_ENC = sPathToFile;
		UpdateData(FALSE);

	}
	
	
}

/* Browser funtion to retrieve the private key file for decryption part */
void CRSAProjectDlg::OnPRKDec() 
{
	CString sPathToFile;
	CFileDialog dlg( TRUE,
					 "*.dbt",
					 NULL,
					 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
					 "Key Files (*.key)|*.key|" );
	
	// initiate dialog with user and check which button was pressed
	if( dlg.DoModal() == IDOK ) {

		// retrieve the path to the file
		sPathToFile = dlg.GetPathName();

		UpdateData(TRUE);
		// save the path to the file
		m_PRK_DEC = sPathToFile;
		UpdateData(FALSE);

	}
		
}

/* Browser funtion to retrieve the public key file for decryption part */
void CRSAProjectDlg::OnPBKDec() 
{
	CString sPathToFile;
	CFileDialog dlg( TRUE,
					 "*.dbt",
					 NULL,
					 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
					 "Key Files (*.key)|*.key|" );
	
	// initiate dialog with user and check which button was pressed
	if( dlg.DoModal() == IDOK ) {

		// retrieve the path to the file
		sPathToFile = dlg.GetPathName();

		UpdateData(TRUE);
		// save the path to the file
		m_PBK_DEC = sPathToFile;
		UpdateData(FALSE);

	}
		
}

/* This function takes the extension of a file name */
void remove(char *name)
{ 
    int i,nlength = strlen(name);
    for (i=nlength;0 < i;i--)
    {
        if (name[i]!='.') continue;
        name[i]='\0';
        break;
    }
}


/**** Encryption function ****/
// This function first retrieves the name of the input file and the public file. 

⌨️ 快捷键说明

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