📄 eccdlg.cpp
字号:
// ECCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ECC.h"
#include "ECCDlg.h"
#include"tommath.h"
#include"tommath_class.h"
#include"tommath_superclass.h"
#include"iostream.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#define P_LONG 200
#define KEY_LONG 128
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//****************************************************************
mp_int GX;
mp_int GY;
mp_int K;//私有密钥
mp_int A;
mp_int B;
mp_int QX;
mp_int QY;
mp_int P;//Fp中的p(有限域P)
mp_init(&GX);
mp_init(&GY);
mp_init(&K);
mp_init(&A);
mp_init(&B);
mp_init(&QX);
mp_init(&QY);
mp_init(&P);
time_t t;
srand( (unsigned) time( &t ) );
Ecc_encipher(&QX,&QY,&GX,&GY,&A,&P);//加密
Ecc_decipher(&K,&A,&P);//解密
char cc;
cout<<"\n\n请击一键退出!\n";
cin>>cc;
mp_clear(&GX);
mp_clear(&GY);
mp_clear(&K);//私有密钥
mp_clear(&A);
mp_clear(&B);
mp_clear(&QX);
mp_clear(&QY);
mp_clear(&P);//Fp中的p(有限域P)
//*******************************************************************
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()
};
int myrng(unsigned char *dst, int len, void *dat)
{
int x;
for (x = 0; x < len; x++) dst[x] = rand() & 0xFF;
return len;
}
int GetPrime(mp_int *m,int lon){
mp_prime_random_ex(m, 10, lon,
(rand()&1)?LTM_PRIME_2MSB_OFF:LTM_PRIME_2MSB_ON,myrng, NULL);
return MP_OKAY;
}
int Two_points_add(mp_int *x1,mp_int *y1,mp_int *x2,mp_int *y2,mp_int *x3,mp_int *y3,mp_int *a,bool zero,mp_int *p)
{
mp_int x2x1;
mp_int y2y1;
mp_int tempk;
mp_int tempy;
mp_int tempzero;
mp_int k;
mp_int temp1;
mp_int temp2;
mp_int temp3;
mp_int temp4;
mp_int temp5;
mp_int temp6;
mp_int temp7;
mp_int temp8;
mp_int temp9;
mp_int temp10;
mp_init(&x2x1);
mp_init(&y2y1);
mp_init(&tempk);
mp_init(&tempy);
mp_init(&tempzero);
mp_init(&k);
mp_init(&temp1);
mp_init(&temp2);
mp_init_set(&temp3,2);
mp_init(&temp4);
mp_init(&temp5);
mp_init(&temp6);
mp_init(&temp7);
mp_init(&temp8);
mp_init(&temp9);
mp_init(&temp10);
if(zero)
{
mp_copy(x1, x3);
mp_copy(y1, y3);
zero=false;
goto L;
}
mp_zero(&tempzero);
mp_sub(x2, x1, &x2x1);
if(mp_cmp(&x2x1,&tempzero)==-1)
{
mp_add(&x2x1, p, &temp1);
mp_zero(&x2x1);
mp_copy(&temp1, &x2x1);
}
mp_sub(y2, y1, &y2y1);
if(mp_cmp(&y2y1,&tempzero)==-1)
{
mp_add(&y2y1, p, &temp2);
mp_zero(&y2y1);
mp_copy(&temp2, &y2y1);
}
if(mp_cmp(&x2x1, &tempzero)!=0)
{
mp_invmod(&x2x1,p,&tempk);
mp_mulmod(&y2y1, &tempk, p, &k);
}
else
{
if(mp_cmp(&y2y1, &tempzero)==0)
{
mp_mulmod(&temp3,y1,p,&tempy);
mp_invmod(&tempy,p,&tempk);
mp_sqr(x1, &temp4);
mp_mul_d(&temp4, 3, &temp5);
mp_add(&temp5, a, &temp6);
mp_mulmod(&temp6, &tempk, p, &k);
}
else
{
zero=true;
goto L;
}
}
mp_sqr(&k, &temp7);
mp_sub(&temp7, x1, &temp8);
mp_submod(&temp8, x2, p, x3);
mp_sub(x1, x3, &temp9);
mp_mul(&temp9, &k, &temp10);
mp_submod(&temp10, y1, p, y3);
L:
mp_clear(&x2x1);
mp_clear(&y2y1);
mp_clear(&tempk);
mp_clear(&tempy);
mp_clear(&tempzero);
mp_clear(&k);
mp_clear(&temp1);
mp_clear(&temp2);
mp_clear(&temp3);
mp_clear(&temp4);
mp_clear(&temp5);
mp_clear(&temp6);
mp_clear(&temp7);
mp_clear(&temp8);
mp_clear(&temp9);
mp_clear(&temp10);
return 1;
}
bool Ecc_points_mul(mp_int *qx,mp_int *qy, mp_int *px, mp_int *py,mp_int *d,mp_int *a,mp_int *p)
{
mp_int X1, Y1;
mp_int X2, Y2;
mp_int X3, Y3;
mp_int XX1, YY1;
mp_int A,P;
int i;
bool zero=false;
char Bt_array[800]={0};
char cm='1';
mp_toradix(d,Bt_array,2);
mp_init_set_int(&X3, 0);
mp_init_set_int(&Y3, 0);
mp_init_copy(&X1, px);
mp_init_copy(&X2, px);
mp_init_copy(&XX1, px);
mp_init_copy(&Y1, py);
mp_init_copy(&Y2, py);
mp_init_copy(&YY1, py);
mp_init_copy(&A, a);
mp_init_copy(&P, p);
for(i=1;i<=KEY_LONG-1;i++)
{
mp_copy(&X2, &X1);
mp_copy(&Y2, &Y1);
Two_points_add(&X1,&Y1,&X2,&Y2,&X3,&Y3,&A,zero,&P);
mp_copy(&X3, &X2);
mp_copy(&Y3, &Y2);
if(Bt_array[i]==cm)
{
mp_copy(&XX1, &X1);
mp_copy(&YY1, &Y1);
Two_points_add(&X1,&Y1,&X2,&Y2,&X3,&Y3,&A,zero,&P);
mp_copy(&X3, &X2);
mp_copy(&Y3, &Y2);
}
}
if(zero)
{
cout<<"It is Zero_Unit!";
return false;//如果Q为零从新产生D
}
mp_copy(&X3, qx);
mp_copy(&Y3, qy);
mp_clear(&X1);
mp_clear(&Y1);
mp_clear(&X2);
mp_clear(&Y2);
mp_clear(&X3);
mp_clear(&Y3);
mp_clear(&XX1);
mp_clear(&YY1);
mp_clear(&A);
mp_clear(&P);
return true;
}
void Get_B_X_Y(mp_int *x1,mp_int *y1,mp_int *b, mp_int *a, mp_int *p)
{
mp_int tempx,tempy;
mp_int temp;
mp_int compare;
mp_int temp1;
mp_int temp2;
mp_int temp3;
mp_int temp4;
mp_int temp5;
mp_int temp6;
mp_int temp7;
mp_int temp8;
mp_init_set_int (&compare, 0);
mp_init(&tempx);
mp_init(&tempy);
mp_init(&temp);
mp_init(&temp1);
mp_init(&temp2);
mp_init(&temp3);
mp_init(&temp4);
mp_init(&temp5);
mp_init(&temp6);
mp_init(&temp7);
mp_init(&temp8);
while(1)
{
//4a3+27b2≠0 (mod p)
GetPrime(b,40);
mp_expt_d(a, 3, &temp1);
mp_sqr(b, &temp2);
mp_mul_d(&temp1, 4, &temp3);
mp_mul_d(&temp2, 27, &temp4);
mp_add(&temp3, &temp4, &temp5);
mp_mod(&temp5,p,&temp);
if(mp_cmp(&temp, &compare)!=0 )
{
break;
}
}
//y2=x3+ax+b,随机产生X坐标,根据X坐标计算Y坐标
GetPrime(x1,30);
mp_expt_d(x1, 3, &temp6);
mp_mul(a, x1, &temp7);
mp_add(&temp6, &temp7, &temp8);
mp_add(&temp8, b, &tempx);
mp_sqrt(&tempx, y1);
mp_clear(&tempx);
mp_clear(&tempy);
mp_clear(&temp);
mp_clear(&temp1);
mp_clear(&temp2);
mp_clear(&temp3);
mp_clear(&temp4);
mp_clear(&temp5);
mp_clear(&temp6);
mp_clear(&temp7);
mp_clear(&temp8);
}
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()
/////////////////////////////////////////////////////////////////////////////
// CECCDlg dialog
CECCDlg::CECCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CECCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CECCDlg)
m_canshu = _T("");
m_scjm = _T("");
m_scjmwj = _T("");
m_srjm = _T("");
m_srjmwj = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CECCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CECCDlg)
DDX_Control(pDX, IDjiem, m_jiem);
DDX_Control(pDX, IDjiam, m_jiam);
DDX_Text(pDX, IDC_canshu, m_canshu);
DDX_Text(pDX, IDC_scjm, m_scjm);
DDX_Text(pDX, IDC_scjmwj, m_scjmwj);
DDX_Text(pDX, IDC_srjm, m_srjm);
DDX_Text(pDX, IDC_srjmwj, m_srjmwj);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CECCDlg, CDialog)
//{{AFX_MSG_MAP(CECCDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_canshu, OnChangecanshu)
ON_EN_CHANGE(IDC_srjm, OnChangesrjm)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CECCDlg message handlers
BOOL CECCDlg::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 CECCDlg::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 CECCDlg::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 CECCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CECCDlg::OnChangecanshu()
{
UpdateData(TRUE);
GetPrime(&P,P_LONG);
char temp[800]="";
mp_toradix(&P,temp,10);
GetPrime(&A,30);
char tempA[800]={0};
mp_toradix(&A,tempA,10);
Get_B_X_Y(&GX,&GY,&B,&A,&P);
char tempB[800]={0};
mp_toradix(&B,tempB,10);
char tempGX[800]={0};
mp_toradix(&GX,tempGX,10);
char tempGY[800]={0};
mp_toradix(&GY,tempGY,10);
GetPrime(&K,KEY_LONG);
char tempK[800]={0};
mp_toradix(&K,tempK,10);
Ecc_points_mul(&QX,&QY,&GX,&GY,&K,&A,&P);
char tempQX[800]={0};
mp_toradix(&QX,tempQX,10);
char tempQY[800]={0};
mp_toradix(&QY,tempQY,10);
m_canshu="椭圆曲线的参数如下(以十进制显示):";
m_canshu+="\r\n有限域 P: ";
m_canshu+=temp;
m_canshu+="\r\n曲线参数 A: ";
m_canshu+=tempA;
m_canshu+="\r\n曲线参数 B: ";
m_canshu+=tempB;
m_canshu+="\n曲线G点X坐标: ";
m_canshu+=tempGX;
m_canshu+="\n曲线G点Y坐标: ";
m_canshu+=tempGY;
m_canshu+="\n私钥 K: ";
m_canshu+=tempK;
m_canshu+="\n公钥X坐标: ";
m_canshu+=tempQX;
m_canshu+="\n公钥Y坐标: ";
m_canshu+=tempQY;
m_canshu+="\n\n";
UpdateData(FALSE);
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void CECCDlg::OnChangesrjm()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -