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

📄 eccdlg.cpp

📁 这是基于VC6.0开发的椭圆密钥生成和加解密算法-ECC
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		{

			fread(miwenx,1,enlongtemp,fp);//读入字符串
			miwenx[enlongtemp]=char(255);
        

			fread(miweny,1,Residue-enlongtemp,fp);//读入字符串
			miweny[Residue-enlongtemp]=char(255);

			 putin(&mx, miwenx,enlongtemp+1);//文件存入 

			putin(&my, miweny,Residue-enlongtemp+1);//文件存入 
		}

		Ecc_points_mul(&c2x,&c2y,px,py,&r,a,p);//加密

	    Ecc_points_mul(&tempx,&tempy,qx,qy,&r,a,p); 

        
		Two_points_add(&mx,&my,&tempx,&tempy,&c1x,&c1y,a,zero,p);

	 
        //保存密文      
	    chmistore(&c1x,fq); 

		chmistore(&c1y,fq);

		chmistore(&c2x,fq);

		chmistore(&c2y,fq);  
	}

	//  buf=""; 
 //strcat(buf,"ok!加密完毕!:\r\n");
		cout<<"\nok!加密完毕!"<<endl;
 //strcat(buf,"密文以二进制保存:\r\n");
	    cout<<"密文以二进制保存"<<endl;
 //strcat(buf,"密文存放路径为 :\r\n");
//strcat(buf,filemi);
	    cout<<"密文存放路径为  "<<filemi<<endl ;


	    fclose(fq);
        fclose(fp);
        mp_clear(&mx);
		mp_clear(&my);
	    mp_clear(&c1x);
	    mp_clear(&c1y);
	    mp_clear(&c2x);
	    mp_clear(&c2y);
        mp_clear(&r);
	    mp_clear(&tempx);
		mp_clear(&tempy);
     

}

//取密文

int miwendraw(mp_int *a,char *ch,int chlong)
{
    mp_digit *temp;
    int i,j,res;

    if(a->alloc<chlong/4)
	{
		if((res=mp_grow(a,chlong/4))!=MP_OKAY)
			return res;
	}

	a->alloc=chlong/4;
    a->sign=0;
	mp_zero(a);
	temp=a->dp;
	i=0;

	for(j=0;j<chlong/4;j++)
	{
		i+=4;
		*temp |= (mp_digit)(ch[i-4] & 255);
		*temp <<= (mp_digit)CHAR_BIT;
        *temp |= (mp_digit)(ch[i-3] & 255);
		*temp <<= (mp_digit)CHAR_BIT;
        *temp |= (mp_digit)(ch[i-2] & 255);
		*temp <<= (mp_digit)CHAR_BIT;
        *temp++ |= (mp_digit)(ch[i-1] & 255); 
	}
    a->used=chlong/4;
    return MP_OKAY;
}

//实现将mp_int数a中的比特串还原为字符串并赋给字符串ch:
int chdraw(mp_int *a,char *ch)
{
	int i,j;
	mp_digit *temp,xx,yy;

	temp=a->dp;
	i=0;
	yy=(mp_digit)255;  //用于位与运算,取八位比特串
	xx=(mp_digit)15;  //用于位与运算,取四位比特串

	for(j=0;j<a->used/2;j++)  //以两个单元为循环,把两个单元的比特串赋给7个字符
	{
		i+=7;
        ch[i-4]=(char)(*++temp & xx);
        ch[i-3]=(char)((*temp >> (mp_digit)4) & yy);	
		ch[i-2]=(char)((*temp >> (mp_digit)12) & yy);
        ch[i-1]=(char)((*temp-- >> (mp_digit)20) & yy);

		ch[i-7]=(char)(*temp & yy);
		ch[i-6]=(char)((*temp >> (mp_digit)8) & yy);
		ch[i-5]=(char)((*temp >> (mp_digit)16) & yy);
		ch[i-4] <<= 4;
		ch[i-4]+=(char)((*temp++ >> (mp_digit)24) & xx);
		temp++;
	}
	if(a->used%2!=0)  //剩于一个单元的处理
	{
		ch[i++] = (char)(*temp & yy);
		ch[i++] = (char)((*temp >> (mp_digit)8) & yy);
		ch[i++] = (char)((*temp >> (mp_digit)16) & yy);
	}
	--i;
    while(int(ch[i]&0xFF) != 255 && i>0) i--;
	return i;
   
}

void Ecc_decipher(mp_int *k, mp_int *a,mp_int *p,CString filehead,CString filefoot){
   CFileDialog fd(TRUE);
   CString filen;
   char fn[40];

	if(IDOK==fd.DoModal())  // 启动用于选择文件的对话框
	{
		//选择了文件
		filen=fd.GetFileName();  //获取选择的文件的文件名
		strcpy(fn,filen.GetBuffer(0));
	}
	else return; //按了取消按钮

	mp_int c1x, c1y;
	mp_int c2x, c2y;
    mp_int tempx, tempy;
	mp_int mx, my;
    mp_int temp;

	mp_init(&temp);
	mp_init(&c1x);
	mp_init(&c1y);
    mp_init(&c2x);
	mp_init(&c2y);
	mp_init(&tempx);
	mp_init(&tempy);
    mp_init(&mx);
	mp_init(&my);

	mp_int tempzero;
	mp_init(&tempzero);

    int i;
	char stemp[700]={0};
    FILE *fp,*fq;
    bool zero=false;


	char filename[85]={0};
    //cout<<"请输入您要解密的文件的存放路径和文件名(如:  c:\\000\\大整数运算  ):"<<endl;
	//cin>>filehead;
    //cout<<"请输入您要解密的文件的扩展名(如:  .doc  ):"<<endl;
	//cin>>filefoot;
	strcpy(filename,filehead);
	strcat(filename,filefoot);

    printf("\n开始解密\n");

    if((fp=fopen(filename,"rb"))==NULL)
	{
		 printf("can not open the file!");
		 exit(1);
	}
 
   //打开保存解密结果文件
    char filemi[80];
strcpy(filemi, filehead);
	strcat(filemi, "解密");
   strcat(filemi, filefoot);

    if((fq=fopen(filemi,"wb"))==NULL)
	{
		 printf("can not open the file!");
		 exit(1);
	}


	rewind(fp);
    while(!feof(fp))
	{
         i=0;
		 while(1)
		{
		     stemp[i]=fgetc(fp);
		     if(i%4==0)
			{
                 if(int(stemp[i]&0xFF) == 255 ) goto L1;
			}
		    i++;
		}
		     
L1:     miwendraw(&c1x, stemp, i);
         i=0;
		 while(1)
		{
		     stemp[i]=fgetc(fp);
		     if(i%4==0)
			{
                 if(int(stemp[i]&0xFF) == 255 ) goto L2;
			}
		    i++;
		}
		     
L2:     miwendraw(&c1y, stemp, i);
	     i=0;
		 while(1)
		{
		     stemp[i]=fgetc(fp);
		     if(i%4==0)
			{
                 if(int(stemp[i]&0xFF) == 255 ) goto L3;
			}
		    i++;
		}
		     
L3:     miwendraw(&c2x, stemp, i);
	            i=0;
		 while(1)
		{
		     stemp[i]=fgetc(fp);
		     if(i%4==0)
			{
                 if(int(stemp[i]&0xFF) == 255 ) goto L4;
			}
		    i++;
		}
		     
L4:     miwendraw(&c2y, stemp, i);

	    mp_zero(&tempzero);
        if(mp_cmp(&c1x, &tempzero)==0) break;

        Ecc_points_mul(&tempx, &tempy, &c2x, &c2y, k, a, p); 

        mp_neg(&tempy, &temp);
        Two_points_add(&c1x,&c1y,&tempx,&temp,&mx,&my,a,zero,p);
	    
		int chtem;
	    chtem=chdraw(&mx,stemp);//从ming中取出字符串
     

		//保存解密结果
    
		for(int kk=0;kk<chtem;kk++)
		{
	         fprintf(fq,"%c",stemp[kk]);
			       
		}

	    chtem=chdraw(&my,stemp);//从ming中取出字符串
    
	         
	     //保存解密结果
		  for(kk=0;kk<chtem;kk++)
		{
	          fprintf(fq,"%c",stemp[kk]);
			
		}
		  
	    
	}

   	cout<<"\nok!解密完毕!"<<endl;
	cout<<"解密后的文字存放路径为  "<<filemi<<endl;

    fclose(fq);
    fclose(fp);
    mp_clear(&c1x);
	mp_clear(&c1y);
	mp_clear(&c2x);
	mp_clear(&c2y);
    mp_clear(&tempx);
	mp_clear(&tempy);
	mp_clear(&mx);
	mp_clear(&my);
    mp_clear(&temp);


}

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

/////////////////////////////////////////////////////////////////////////////
// CECCDlg dialog

CECCDlg::CECCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CECCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CECCDlg)
	m_canshu = _T("");
	m_result = _T("");
	m_foot = _T("");
	m_path = _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, IDC_gc, m_gc);
	DDX_Control(pDX, IDC_exit, m_exit);
	DDX_Control(pDX, IDC_enfile, m_enfile);
	DDX_Control(pDX, IDC_defile, m_defile);
	DDX_Text(pDX, IDC_canshu, m_canshu);
	DDX_Text(pDX, IDC_result, m_result);
	DDX_Text(pDX, IDC_foot, m_foot);
	DDX_Text(pDX, IDC_path, m_path);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CECCDlg, CDialog)
	//{{AFX_MSG_MAP(CECCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_exit, Onexit)
	ON_BN_CLICKED(IDC_gc, Ongc)
	ON_BN_CLICKED(IDC_enfile, Onenfile)
	ON_BN_CLICKED(IDC_defile, Ondefile)
	//}}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::Onexit() 
{
	// TODO: Add your control notification handler code here
exit(1);
}

void CECCDlg::Ongc() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);

	

	mp_init(&GX);
	mp_init(&GY);
	mp_init(&K);
	mp_init(&A);
	mp_init(&B);
	mp_init(&QX);
	mp_init(&QY);
	mp_init(&P);
	
    char buf[10000]="";
    time_t t;           
    srand( (unsigned) time( &t ) );

    strcat(buf,"椭圆曲线的参数如下(以十进制显示):\r\n");	

    GetPrime(&P,P_LONG);
	strcat(buf,"有限域 P 是:\r\n");	


	char temp[800]={0};
    mp_toradix(&P,temp,10);
   	strcat(buf,temp);	
 strcat(buf,"\r\n");

    GetPrime(&A,30);
	char tempA[800]={0};
	strcat(buf,"曲线参数 A 是:\r\n");
	//printf("曲线参数 A 是:\n");	
    mp_toradix(&A,tempA,10);
	strcat(buf,tempA);
 strcat(buf,"\r\n");
    //printf("%s\n",tempA); 
	
	Get_B_X_Y(&GX,&GY,&B,&A,&P);

    char tempB[800]={0};
    strcat(buf,"曲线参数 B 是:\r\n");
	//printf("曲线参数 B 是:\n");	
    mp_toradix(&B,tempB,10);
	strcat(buf,tempB);
 strcat(buf,"\r\n");
    //printf("%s\n",tempB); 
	
	char tempGX[800]={0};
   strcat(buf,"曲线G点X坐标是:\r\n");
	//printf("曲线G点X坐标是:\n");	

   strcat(buf,tempGX);
 strcat(buf,"\r\n");

    //printf("%s\n",tempGX);   

	char tempGY[800]={0};
   strcat(buf,"曲线G点Y坐标是:\r\n");
//	printf("曲线G点Y坐标是:\n");
    mp_toradix(&GY,tempGY,10);
 strcat(buf,tempGY);
 strcat(buf,"\r\n");
   // printf("%s\n",tempGY); 
	

	//------------------------------------------------------------------
    GetPrime(&K,KEY_LONG);
    char tempK[800]={0};
 strcat(buf,"私钥 K 是:\r\n");
	//printf("私钥 K 是:\n");
    mp_toradix(&K,tempK,10);
 strcat(buf,tempK);
 strcat(buf,"\r\n");
   // printf("%s\n",tempK); 

	Ecc_points_mul(&QX,&QY,&GX,&GY,&K,&A,&P);
	

    char tempQX[800]={0};
 strcat(buf,"公钥X坐标是:\r\n");
	//printf("公钥X坐标是:\n");
    mp_toradix(&QX,tempQX,10);
 strcat(buf,tempQX);
 strcat(buf,"\r\n");
    //printf("%s\n",tempQX); 

	char tempQY[800]={0};
strcat(buf,"公钥Y坐标是:\r\n");
//	printf("公钥Y坐标是:\n");
    mp_toradix(&QY,tempQY,10);
  mp_toradix(&QX,tempQY,10);
 strcat(buf,tempQY);
    //printf("%s\n",tempQY); 
  	m_canshu.Format("%s",buf);
    UpdateData(false);
for(int a=0;a<10000;a++)buf[a]='\0';
  return;
}

void CECCDlg::Onenfile() 
{
	// TODO: Add your control notification handler code here
    UpdateData(true);
    //char buf[10000]="";
    Ecc_encipher(&QX,&QY,&GX,&GY,&A,&P,m_path,m_foot);
//	m_result.Format("%s",buf);
AfxMessageBox("Test is over!Please check the result");
    UpdateData(false);

}

void CECCDlg::Ondefile() 
{

	// TODO: Add your control notification handler code here
	    UpdateData(true);
    //char buf[10000]="";

    Ecc_decipher(&K,&A,&P,m_path,m_foot);
//	m_result.Format("%s",buf);
AfxMessageBox("Test is over!Please check the result");
    UpdateData(false);

}

⌨️ 快捷键说明

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