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

📄 rsautildlg.cpp

📁 RSA 高级实现
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// RSAUtilDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RSAUtil.h"
#include "RSAUtilDlg.h"
#include "RSA.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

const char c_str_db_base_name[] = "rsa.util.db";
const char c_str_random_seed_file[] = "random.seed.file.dat";

#define MIN_KEY_SIZE 512
#define MAX_KEY_SIZE 8192

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

/////////////////////////////////////////////////////////////////////////////
// CRSAUtilDlg dialog

CRSAUtilDlg::CRSAUtilDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRSAUtilDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRSAUtilDlg)
	m_strKeySize = _T("1024");
	m_strMessageC = _T("");
	m_strMessageM = _T("(C)NetworkAssociates.(PGP)");
	m_strPrimeP = _T("");
	m_strPrimeQ = _T("");
	m_strPrivateD = _T("");
	m_strMessage = _T("");
	m_strNRealSize = _T("");
	m_strKeyN = _T("");
	m_nCombViewAs = -1;
	m_nEncryptionDecryptionTestTimes = 1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bBackgroundCalculating = false;
}

void CRSAUtilDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRSAUtilDlg)
	DDX_Control(pDX, IDC_COMBO_PUBLIC_E, m_combPublicE);
	DDX_Control(pDX, IDC_PROGRESS_DE, m_pbDE);
	DDX_Control(pDX, IDC_EDIT_PRIME_Q, m_editPrimeQ);
	DDX_Control(pDX, IDC_EDIT_PRIME_P, m_editPrimeP);
	DDX_Control(pDX, IDC_BUTTON_GENERATE_NOW, m_btnGenerate);
	DDX_Control(pDX, IDC_STATIC_RANDOM_MESSAGE, m_staticRandomMessage);
	DDX_Control(pDX, IDC_STATIC_MESSAGE, m_staticMessage);
	DDX_Control(pDX, IDC_COMBO_VIEW_AS, m_combViewAs);
	DDX_Control(pDX, IDC_PROGRESS_RANDOM, m_pbRandom);
	DDX_Text(pDX, IDC_EDIT_KEY_SIZE, m_strKeySize);
	DDX_Text(pDX, IDC_EDIT_MESSAGE_C, m_strMessageC);
	DDX_Text(pDX, IDC_EDIT_MESSAGE_M, m_strMessageM);
	DDX_Text(pDX, IDC_EDIT_PRIME_P, m_strPrimeP);
	DDX_Text(pDX, IDC_EDIT_PRIME_Q, m_strPrimeQ);
	DDX_Text(pDX, IDC_EDIT_PRIVATE_D, m_strPrivateD);
	DDX_Text(pDX, IDC_STATIC_MESSAGE, m_strMessage);
	DDX_Text(pDX, IDC_STATIC_N_REAL_SIZE, m_strNRealSize);
	DDX_Text(pDX, IDC_EDIT_KEY_N, m_strKeyN);
	DDX_CBIndex(pDX, IDC_COMBO_VIEW_AS, m_nCombViewAs);
	DDX_Text(pDX, IDC_EDIT_TEST_TIMES, m_nEncryptionDecryptionTestTimes);
	DDV_MinMaxInt(pDX, m_nEncryptionDecryptionTestTimes, 1, 8192);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRSAUtilDlg, CDialog)
	//{{AFX_MSG_MAP(CRSAUtilDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_EDIT_KEY_SIZE, OnChangeEditKeySize)
	ON_BN_CLICKED(IDC_BUTTON_C_STRING_TO_CLIPBOARD, OnButtonCStringToClipboard)
	ON_BN_CLICKED(IDC_BUTTON_GENERATE_NOW, OnButtonGenerateNow)
	ON_EN_CHANGE(IDC_EDIT_MESSAGE_M, OnChangeEditMessageM)
	ON_BN_CLICKED(IDC_BUTTON_ENCRYPT, OnButtonEncrypt)
	ON_BN_CLICKED(IDC_BUTTON_DECRYPT, OnButtonDecrypt)
	ON_WM_MOUSEMOVE()
	ON_BN_CLICKED(IDC_BUTTON_CALCULATE_ND, OnButtonCalculateND)
	ON_EN_CHANGE(IDC_EDIT_PRIME_P, OnChangeEditPrimeP)
	ON_EN_CHANGE(IDC_EDIT_PRIME_Q, OnChangeEditPrimeQ)
	ON_CBN_SELCHANGE(IDC_COMBO_VIEW_AS, OnSelchangeComboViewAs)
	ON_BN_CLICKED(IDC_BUTTON_SAVE_TO_DB, OnButtonSaveToDb)
	ON_WM_DESTROY()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRSAUtilDlg message handlers

BOOL CRSAUtilDlg::OnInitDialog()
{
	__int64 i = 0;
	i += 5;

	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
	m_combViewAs.SetCurSel(0);
	m_combPublicE.SetCurSel(0);

	m_fontMessage.CreateFont(13, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET, 0, 0, 0, 0, "Arial Black");
	m_staticMessage.SetFont(&m_fontMessage, false);

	char szFileName[512];
	GetModuleFileName(NULL, szFileName, sizeof(szFileName));

	CString str = szFileName;
	int nIndex = str.ReverseFind('\\');
	m_strAppPath = str.Left(nIndex + 1);
	
	m_rand.LoadSeedFromFile(m_strAppPath + c_str_random_seed_file);

	_UpdateRandomGenetator(0);

	m_rsa.InitRandGenerator(&m_rand);

	m_bCancelGen = false;
	m_bGenWorking = false;
	m_bBackgroundCalculating = false;
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CRSAUtilDlg::OnChangeEditKeySize() 
{
	//UpdateData();
	//UpdateData(false);
}

int _GenProcessing(void* arg, int c)
{
	CRSAUtilDlg* pdlg = (CRSAUtilDlg*)arg;
	CString str;

	pdlg->m_nProcessingPrivate++;
	if (pdlg->m_bCancelGen)
	{
		return -0x10001; //hehe, this is a prime !
	}
	
	static int nLastTickSave = 0;

	int nTicks = GetTickCount();
	if (nTicks < nLastTickSave + 688)
	{
		return 0;
	}
	else
	{
		nLastTickSave = nTicks;
		nTicks -= pdlg->m_dwProcessBeginTick;
		str.Format("Processing: key tried = %02d prime index = %04d time = %d.%02d s) %c ...", 
			pdlg->m_nGenProcessRetryCount, 
			pdlg->m_nProcessingPrivate, 
			nTicks / 1000, nTicks % 1000, c);
		pdlg->m_staticMessage.SetWindowText(str);
	}

	MSG msg;
	
	while(PeekMessage(&msg, pdlg->m_hWnd, 0, 0, PM_NOREMOVE))
	{
		if (WM_CLOSE != msg.message && WM_QUIT != msg.message)
		{
			GetMessage(&msg, pdlg->m_hWnd, 0, 0);

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			return -0x10003; //hehe, this is a prime !
		}
	}

	if (pdlg->m_bBackgroundCalculating)
	{
		Sleep(200);
	}

	return 0;
}

void CRSAUtilDlg::_UpdateRandomGenetator(int nUpdate)
{
	m_rand.Update(nUpdate);
	int n = m_rand.GetUpdateCounter();
	if (0 != nUpdate)
	{
		m_pbRandom.SetRange(0, 100);
		m_pbRandom.SetPos(min(100, n));
		
		CString str;
		str.Format("Rand seeds updated = %d", n);
		m_staticRandomMessage.SetWindowText(str);
	}
}

void CRSAUtilDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	if (m_ptLastMousePos != point)
	{
		_UpdateRandomGenetator((point.x << 16) | point.y);
		m_ptLastMousePos = point;
	}
	CDialog::OnMouseMove(nFlags, point);
}

void CRSAUtilDlg::OnChangeEditPrimeP() 
{
	UpdateData();
	UpdateData(false);
}

void CRSAUtilDlg::OnChangeEditPrimeQ() 
{
	UpdateData();
	UpdateData(false);
}

BOOL CRSAUtilDlg::_GetPublicE(CBigNumber& e)
{
	int n = m_combPublicE.GetCurSel();
	if (-1 != n)
	{
		switch(n)
		{
		case 0: e = 0x11; break;
		case 1: e = 0x10001; break;
		case 2: e = 0x101; break;
		case 3: e = 0x1001; break;
		default:
			return false;
		}
		return true;
	}
	return true;
}

void CRSAUtilDlg::OnButtonGenerateNow() 
{
	UpdateData();
	UpdateData(false);

	int nKeySize = atoi(m_strKeySize);
	if (nKeySize < MIN_KEY_SIZE || nKeySize > MAX_KEY_SIZE)
	{
		AfxMessageBox("Key Size must between 512 -> 4096");
		return;
	}
	
	CBigNumber e;
	if (!_GetPublicE(e))
	{
		m_strPrivateD = 
			"Public exponent is not relatively prime to (p-1)(q-1)!\n"
			"Try again with other primes or choose a different value for E.";
		UpdateData(false);
		return;
	}
	
	if (m_bGenWorking)
	{
		m_bCancelGen = true;
		return;
	}

	m_btnGenerate.SetWindowText("Cancel Gen");
	
	m_bGenWorking = true;

⌨️ 快捷键说明

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