📄 rsadlg.cpp
字号:
// RSADlg.cpp : implementation file
//
#include "stdafx.h"
#include "RSA.h"
#include "RSADlg.h"
#include "math.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_p = 0;
m_q = 0;
m_n = 0;
m_code = 0;
m_decode = 0;
m_dtxt = _T("");
m_etxt = _T("");
m_ptxt = _T("");
//}}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_EDIT_P, m_p);
DDX_Text(pDX, IDC_EDIT_Q, m_q);
DDX_Text(pDX, IDC_EDIT_N, m_n);
DDX_Text(pDX, IDC_EDIT_CODE, m_code);
DDX_Text(pDX, IDC_EDIT_DECODE, m_decode);
DDX_Text(pDX, IDC_EDIT_DecryptText, m_dtxt);
DDX_Text(pDX, IDC_EDIT_EncryptText, m_etxt);
DDX_Text(pDX, IDC_EDIT_PLAINTEXT, m_ptxt);
//}}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_BUTTON_PRODUCE, OnButtonProduce)
ON_BN_CLICKED(IDCANCEL3, OnCode)
ON_BN_CLICKED(IDCANCEL2, OnDecode)
//}}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::OnButtonProduce()
{
// TODO: Add your control notification handler code here
UpdateData(true);
while(true)
{
srand(time(0));
m_p = rand() % 100;
m_q = rand() % 100;
if (isPrime(m_p) && isPrime(m_q))
break;
}
m_n = m_p * m_q;
int fiN = (m_p - 1) * (m_q - 1);
//int e;
for (int i = 3; i < fiN; i++)
{
if (isPrime(i) && gcd(fiN, i) == 1)
{
m_code = i;
break;
}
}
Euler(m_code, fiN);
UpdateData(false);
}
int CRSADlg::power(int a, int n, int m)
{
int z=1, t;
for(t=a; n>0; n>>=1)
{
if (n%2==1) z=(z*t) % m;
t=(t*t) % m;
}
return(z);
}
BOOL CRSADlg::isPrime(int x)
{
int i;
for (i = 2; i <= (int)sqrt(x); i++)
{
if (x % i == 0)
break;
}
if (i > (int)sqrt(x))
return true;
return false;
}
int CRSADlg::gcd(int a, int b)
{
if (a == 0)
{
return b;
}
else
{
return gcd(b % a, a);
}
}
void CRSADlg::Euler(int e, int fin)
{
int u1 = 1;
int u2 = 0;
int u3 = fin;
int v1 = 0;
int v2 = 1;
int v3 = e;
int v = 1;
int t1, t2, t3;
int q;
int uu, vv;
int inverse, z;
while (v3 != 0)
{
q = (int)(u3 /v3);
t1 = u1 - q * v1;
t2 = u2 - q * v2;
t3 = u3 - q * v3;
u1 = v1;
u2 = v2;
u3 = v3;
v1 = t1;
v2 = t2;
v3 = t3;
z = 1;
}
uu = u1;
vv = u2;
if (vv < 0)
inverse = vv + fin;
else
inverse = vv;
m_decode = inverse;
}
void CRSADlg::OnCode()
{
CString temp;
UpdateData(true);
CString encSt = "";
CString e2st = "";
if (m_ptxt.IsEmpty())
return;
for (int i = 0; i < m_ptxt.GetLength(); i++)
{
temp = encSt;
encSt.Format("%s%d%s",temp,power((int)m_ptxt.GetAt(i), m_code, m_n),"+");
//
//encSt + CSring(power((int)m_ptxt.GetAt(i), m_code, m_n)) + "+";
//str.Format("%s%d", str, 123);
}
m_etxt = encSt;
UpdateData(false);
}
void CRSADlg::OnDecode()
{
int ptr;
int tok;
CString temp = "";
UpdateData(true);
CString DecSt = "";
//int t = m_etxt.GetLength();
for (int i = 0; i < m_etxt.GetLength();)
{
ptr = m_etxt.Find("+", i);
tok = atoi(m_etxt.Mid(i, ptr - i));
temp = DecSt;
DecSt.Format("%s%c", temp, char(power(tok, m_decode, m_n)));
i = i + num(tok) + 1;
}
m_dtxt = DecSt;
UpdateData(false);
}
int CRSADlg::num(int x)
{
int n = 0;
while (x)
{
n++;
x /= 10;
}
return n;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -