📄 rsadlg.cpp
字号:
// RSADlg.cpp : implementation file
//
#include <stdlib.h>
#include "mrprime.c"
#include "mirdef.h"
#include "miracl.h"
#include "mrsmall.c"
#include "stdafx.h"
#include "RSA.h"
#include "RSADlg.h"
#define random(min,max) (rand()%(max+1-min)+min)
#ifdef _DEBUG
#define new DEBUG_NEW
#define mr_small unsigned int
#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()//END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRSADlg dialog
CRSADlg::CRSADlg(CWnd* pParent /*=NULL*/)
: CDialog(CRSADlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRSADlg)
m_d = 0;
m_c = 0;
m_e = 0;
m_m = 0;
m_mm = 0;
m_p = 0;
m_q = 0;
m_n = 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_C, m_c);
DDX_Text(pDX, IDC_E, m_e);
DDX_Text(pDX, IDC_M, m_m);
DDX_Text(pDX, IDC_MM, m_mm);
DDX_Text(pDX, IDC_P, m_p);
DDX_Text(pDX, PIDC_Q, m_q);
DDX_Text(pDX, QIDC_N, m_n);
//}}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_DECRYPT, OnDecrypt)
ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
ON_BN_CLICKED(IDC_HAND, OnHand)
ON_BN_CLICKED(IDC_RANDOM, OnRandom)
//}}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::OnDecrypt()
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_mm=powmod(m_c,m_d,m_n);
UpdateData(false);
}
void CRSADlg::OnEncrypt()
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_c=powmod(m_m,m_e,m_n);
UpdateData(false);
}
void CRSADlg::OnHand()
{
// TODO: Add your control notification handler code here
long temp;
UpdateData(true);
m_n=m_p*m_q;
temp=(m_p-1)*(m_q-1);
m_e=invmod(m_d,temp);
UpdateData(false);
}
void CRSADlg::OnRandom()
{
// TODO: Add your control notification handler code here
long temp;
srand((unsigned)time(NULL));
m_p=getprime(100);
m_q=getprime(100);
m_n=m_p*m_q;
temp=(m_p-1)*(m_q-1);
m_d=getprime(temp);
m_e=invmod(m_d,temp);
UpdateData(false);
}
long CRSADlg::powmod(long a, long b, long n)
{
/*long A=1;
while(b!=0)
{
if(b%2)A=(A*a)%n;
a=(a*a)%n;
b=b>>1;
}*/
return (mr_small spmd(mr_small a,mr_small b,mr_small n));
}
int CRSADlg::testprime(long n)
{
/*int T=10;
long i,j,s,t,a,y;
if(n%2==0)return 0;
s=0;t=n-1;
while(t%2==0)
{
t=t/2;
s++;
}
for(i=0;i<T;i++)
{
a=random(2,n-2);
y=powmod(a,t,n);
if(y!=1&&y!=n-1)
{
j=1;
while(j<=s&&y!=n-1)
{
y=(y*y)%n;
if(y==1)
return 0;
j++;
}
if(y!=n-1)
return 0;
}
}
return 1;*/
return(isprime(n));
}
long CRSADlg::getprime(long N)
{
long n;
for(;;)
{
n=random(3,n);
if(testprime(n))
return n;
}
}
long CRSADlg::invmod(long a, long b)
{
/*long s0=1,s1=0,s2,q,t,b0;
b0=b;
while(b)
{
q=a/b;
s2=s0-q*s1;
if(s2>=0)s2=s2%b0;
else s2=b0-(-s2)%b0;
s0=s1;
s1=s2;
t=b;b=a%b;a=t;
}
if(a==1)
return s0;
else return 0;*/
return(invers(a,b));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -