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

📄 rsasystemdlg.cpp

📁 RSA算法是第一个能同时用于加密和数字签名的算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	m_priadd.SetWindowText("d:\\prikey.txt");

	m_source.SetWindowText("d:\\source.txt");

	m_encode.SetWindowText("d:\\source.rsa");

	m_decode.SetWindowText("d:\\decode.txt");

	m_Tab.InsertItem(0,"由系统时间生成随机数");
	m_Tab.InsertItem(1,"生成秘钥");
	m_Tab.InsertItem(2,"加密解密");

			m_static1.ShowWindow(SW_NORMAL);
			m_seed1c.ShowWindow(SW_NORMAL);
			m_seed2c.ShowWindow(SW_NORMAL);
			m_seed3c.ShowWindow(SW_NORMAL);
			m_seed4c.ShowWindow(SW_NORMAL);
			m_BUTTON7.ShowWindow(SW_NORMAL);

			m_static2.ShowWindow(SW_NORMAL);
			m_priadd.ShowWindow(SW_NORMAL);
			m_pubadd.ShowWindow(SW_NORMAL);

			m_static3.ShowWindow(SW_HIDE);
			m_keybitc.ShowWindow(SW_HIDE);
			m_BUTTON1.ShowWindow(SW_HIDE);
			m_showpubkeyc.ShowWindow(SW_HIDE);
			m_showprikeyc.ShowWindow(SW_HIDE);
			m_showprikey2c.ShowWindow(SW_HIDE);

			m_static4.ShowWindow(SW_HIDE);
			m_static5.ShowWindow(SW_HIDE);
			m_static6.ShowWindow(SW_HIDE);
			m_static7.ShowWindow(SW_HIDE);
			m_source.ShowWindow(SW_HIDE);
			m_encode.ShowWindow(SW_HIDE);
			m_decode.ShowWindow(SW_HIDE);
			m_BUTTON2.ShowWindow(SW_HIDE);
			m_BUTTON3.ShowWindow(SW_HIDE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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


Big CRsasystemDlg::strongp(int n, long seed1, long seed2)
{	//生成大素数
	Big p;
    int r,r1,r2;
    irand(seed1);
    pd=rand(2*n/3,2);
    pd=nextprime(pd);
    ph=pow((Big)2,n-1)/pd;  
    pl=pow((Big)2,n-2)/pd;
    ph-=pl;
    irand(seed2);
    ph=rand(ph);
    ph+=pl;
    r1=pd%12;
    r2=ph%12;
    r=0;
    while ((r1*(r2+r))%12!=5) r++;
    ph+=r;
    do 
    { 
        p=2*ph*pd+1;
        ph+=12;
    } while (!prime(p));  
    return p;

}



void CRsasystemDlg::OnButton1() 
{
    //按键生产秘钥
	UpdateData(true);
	//更新数据

	TCHAR filename1[256]={0};
	TCHAR filename2[256]={0};

	int filelen1=m_pubadd.GetWindowTextLength();
	int filelen2=m_priadd.GetWindowTextLength();
	m_pubadd.GetWindowText(filename1,filelen1+1);
	m_priadd.GetWindowText(filename2,filelen2+1);

	int k,i;
    long seed[4];
    Big p[2],ke;
    ofstream public_key(filename1);
    ofstream private_key(filename2);
    miracl *mip=&precision;//打开miracl大数库系统。

    gprime(15000);   //产生大素数。
 
	k=m_keybit;//根据用户输入的密钥位数,产生密钥(至少128位)。

   	seed[0]=m_seed1;seed[1]=m_seed2;seed[2]=m_seed3;seed[3]=m_seed4;
	//根据用户输入的四个随机种子,为strongp()函数产生素数用。
    ke=1;
    for (i=0;i<2;i++) 
    {
        p[i]=strongp(k,seed[2*i],seed[2*i+1]);
        ke*=p[i];
    }

    mip->IOBASE=16;//以16进制输出。
    public_key << ke << endl; //公钥保存在ke变量中,并输出到对应文件。

    for (i=0;i<2;i++)
        private_key << p[i] << endl;//将私钥输入到对应的文件中。

	public_key.close();
	private_key.close();
	///////////////////////////////
	/*
  
	CFile   inFile1(filename1,   CFile::modeRead); 
	CArchive   ar1(&inFile1,   CArchive::load); 
    ar1>>m_showpubkey;
 
	CFile   inFile2(filename2,   CFile::modeRead); 
	CArchive   ar2(&inFile2,   CArchive::load); 
    ar2>>m_showprikey;
	*/
	//////////////////////

	/////////////////////////////
	CStdioFile f1;
	f1.Open( filename1, CFile::modeRead);
	char pBuf[513];
	f1.ReadString( pBuf, 513);
	m_showpubkey.Format(_T("%s"),pBuf);
	f1.Close();

	CStdioFile f2;
	f2.Open( filename2, CFile::modeRead);
	char pBuf1[300];
	char pBuf2[300];
	f2.ReadString( pBuf1, 300);
	f2.ReadString( pBuf2, 300);
	m_showprikey.Format(_T("%s"),pBuf1);
	m_showprikey2.Format(_T("%s"),pBuf2);
	f2.Close();


	/////////////////////////////

	UpdateData(FALSE);

	AfxMessageBox("密钥已成功生成!");//提示用户密钥已经生成。


	
}

void CRsasystemDlg::strip(char *name)


{

/* strip off filename extension */
    int i;
    for (i=0;name[i]!='\0';i++)
    {
        if (name[i]!='.') continue;
        name[i]='\0';
        break;
	}
}


void CRsasystemDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here

	/*  encode using public key  */
    Big e,m,y,ke,mn,mx;
	char ifname[13]={0};
	char ofname[13]={0};
	TCHAR filename1[256]={0};
	int filelen=m_pubadd.GetWindowTextLength();
	m_pubadd.GetWindowText(filename1,filelen+1);


	int filelen1=m_source.GetWindowTextLength();
	m_source.GetWindowText(ifname,filelen1+1);
    ifstream public_key(filename1);
    ifstream input_file;
    ofstream output_file;
    static char line[5000];
    static char buff[2560];
    //static char ifname[13],ofname[13];
    BOOL fli,last;
    int i,ipt,klen;
    miracl *mip=&precision;
    mip->IOBASE=16;
    public_key >> ke;
    mn=root(ke,3);
    mx=mn*mn*mn-mn*mn;
 /* find key length in characters */
    klen=bits(mx)/7;
    fli=FALSE;
    if (strlen(ifname)>0) fli=TRUE;
    if (fli)
    { /* set up input file */
        strcpy(ofname,ifname);
        strip(ofname);
        strcat(ofname,".rsa");
        input_file.open(ifname,ios::in); 
        if (!input_file)
        {
            cout << "Unable to open file " << ifname << "\n";

        }

    }

    output_file.open(ofname);
    ipt=0;
    last=FALSE;
    while (!last)
    { /* encode line by line */
        if (fli)
        {
            input_file.getline(&line[ipt],132);
            if (input_file.eof() || input_file.fail()) last=TRUE;
        }

        strcat(line,"\n");
        ipt=strlen(line);
        if (ipt<klen && !last) continue;
        while (ipt>=klen)
        { /* chop up into klen-sized chunks and encode */
            for (i=0;i<klen;i++)
                buff[i]=line[i];
            buff[klen]='\0';
            for (i=klen;i<=ipt;i++)
                line[i-klen]=line[i];
            ipt-=klen;
            mip->IOBASE=128;
            m=buff;
            e=pow(m,3,ke);////////test
            mip->IOBASE=16;
            output_file << e << endl;
        }
        if (last && ipt>0)
        { /* now deal with left overs */
            mip->IOBASE=128;
            m=line;
            if (m<mn)
            { /* pad out with random number if necessary */
                m+=mn*(mn*mn-rand(mn));
            }
            e=pow(m,3,ke);//////////test
            mip->IOBASE=16;
            output_file << e << endl;
        }
    }
	AfxMessageBox("加密成功");
  //  return 0;
	
}

void CRsasystemDlg::OnButton3() 
{
  	// TODO: Add your control notification handler code here
 int i;
    big e,ep[NP],m,ke,kd,p[NP],kp[NP],mn,mx;
    FILE *ifile;
    FILE *ofile;
    char ifname[13]={0};
	char ofname[13]={0};
	TCHAR filename2[256]={0};
	int filelen=m_priadd.GetWindowTextLength();
	m_priadd.GetWindowText(filename2,filelen+1);


	int filelen1=m_encode.GetWindowTextLength();
	m_encode.GetWindowText(ifname,filelen1+1);
	int filelen2=m_decode.GetWindowTextLength();
	m_decode.GetWindowText(ofname,filelen2+1);
    BOOL flo;
    big_chinese ch;
	 miracl *mip=&precision;
    mip=mirsys(100,0);
    for (i=0;i<NP;i++)
    {
        p[i]=mirvar(0);
        ep[i]=mirvar(0);
        kp[i]=mirvar(0);
    }
    e=mirvar(0);
    m=mirvar(0);
    kd=mirvar(0);
    ke=mirvar(0);
    mn=mirvar(0);
    mx=mirvar(0);
    mip->IOBASE=16;
    if ((ifile=fopen(filename2,"rt"))==NULL)
    {
        printf("Unable to open file private.key\n");
//        return 0;
    }
    for (i=0;i<NP;i++)
    {
        cinnum(p[i],ifile);
    }
    fclose(ifile);
 /* generate public and private keys */
    convert(1,ke);
    for (i=0;i<NP;i++)
    {
        multiply(ke,p[i],ke);
    }
    for (i=0;i<NP;i++)
    { /* kp[i]=(2*(p[i]-1)+1)/3 = 1/3 mod p[i]-1 */
        decr(p[i],1,kd);
        premult(kd,2,kd);
        incr(kd,1,kd);
        subdiv(kd,3,kp[i]);
    }

    crt_init(&ch,NP,p);
    nroot(ke,3,mn);
    multiply(mn,mn,m);
    multiply(mn,m,mx);
    subtract(mx,m,mx);
   /* do
    { /* get input file 
        printf("file to be decoded = ");
        gets(ifname);
    } while (strlen(ifname)==0);*/
    strip(ifname);
    strcat(ifname,".rsa");
   // printf("output filename = ");
    gets(ofname);
    flo=FALSE;
    if (strlen(ofname)>0) 
    { /* set up output file */
        flo=TRUE;
        ofile=fopen(ofname,"wt");
    }
    printf("decoding message\n");
    if ((ifile=fopen(ifname,"rt"))==NULL)
    {
        printf("Unable to open file %s\n",ifname);
       // return 0;
    }
    forever
    { /* decode line by line */
        mip->IOBASE=16;
        cinnum(m,ifile);
        if (size(m)==0) break;
        for (i=0;i<NP;i++)
            powmod(m,kp[i],p[i],ep[i]);
        crt(&ch,ep,e);    /* Chinese remainder thereom */
        if (compare(e,mx)>=0) divide(e,mn,mn);
        mip->IOBASE=128;
        if (flo) cotnum(e,ofile);
        cotnum(e,stdout);
    }
    crt_end(&ch);
    fclose(ifile);
    if (flo) fclose(ofile);
  //  printf("message ends\n");
 //   return 0;
AfxMessageBox("解密成功!");

		
}

void CRsasystemDlg::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
	int index=m_Tab.GetCurSel();
	switch(index)
	{
	case 0:{
			m_static1.ShowWindow(SW_NORMAL);
			m_seed1c.ShowWindow(SW_NORMAL);
			m_seed2c.ShowWindow(SW_NORMAL);
			m_seed3c.ShowWindow(SW_NORMAL);
			m_seed4c.ShowWindow(SW_NORMAL);
			m_BUTTON7.ShowWindow(SW_NORMAL);

			m_static2.ShowWindow(SW_NORMAL);
			m_priadd.ShowWindow(SW_NORMAL);
			m_pubadd.ShowWindow(SW_NORMAL);

			m_static3.ShowWindow(SW_HIDE);
			m_keybitc.ShowWindow(SW_HIDE);
			m_BUTTON1.ShowWindow(SW_HIDE);
			m_showpubkeyc.ShowWindow(SW_HIDE);
			m_showprikeyc.ShowWindow(SW_HIDE);
			m_showprikey2c.ShowWindow(SW_HIDE);

			m_static4.ShowWindow(SW_HIDE);
			m_static5.ShowWindow(SW_HIDE);
			m_static6.ShowWindow(SW_HIDE);
			m_static7.ShowWindow(SW_HIDE);
			m_source.ShowWindow(SW_HIDE);
			m_encode.ShowWindow(SW_HIDE);
			m_decode.ShowWindow(SW_HIDE);
			m_BUTTON2.ShowWindow(SW_HIDE);
			m_BUTTON3.ShowWindow(SW_HIDE);

			break;
		   }

		case 1:{
			m_static1.ShowWindow(SW_HIDE);
			m_seed1c.ShowWindow(SW_HIDE);
			m_seed2c.ShowWindow(SW_HIDE);
			m_seed3c.ShowWindow(SW_HIDE);
			m_seed4c.ShowWindow(SW_HIDE);
			m_BUTTON7.ShowWindow(SW_HIDE);

			m_static2.ShowWindow(SW_HIDE);
			m_priadd.ShowWindow(SW_HIDE);
			m_pubadd.ShowWindow(SW_HIDE);

			m_static3.ShowWindow(SW_NORMAL);
			m_keybitc.ShowWindow(SW_NORMAL);
			m_BUTTON1.ShowWindow(SW_NORMAL);
			m_showpubkeyc.ShowWindow(SW_NORMAL);
			m_showprikeyc.ShowWindow(SW_NORMAL);
			m_showprikey2c.ShowWindow(SW_NORMAL);

			m_static4.ShowWindow(SW_HIDE);
			m_static5.ShowWindow(SW_HIDE);
			m_static6.ShowWindow(SW_HIDE);
			m_static7.ShowWindow(SW_HIDE);
			m_source.ShowWindow(SW_HIDE);
			m_encode.ShowWindow(SW_HIDE);
			m_decode.ShowWindow(SW_HIDE);
			m_BUTTON2.ShowWindow(SW_HIDE);
			m_BUTTON3.ShowWindow(SW_HIDE);

			break;
		   }
			case 2:{
			m_static1.ShowWindow(SW_HIDE);
			m_seed1c.ShowWindow(SW_HIDE);
			m_seed2c.ShowWindow(SW_HIDE);
			m_seed3c.ShowWindow(SW_HIDE);
			m_seed4c.ShowWindow(SW_HIDE);
			m_BUTTON7.ShowWindow(SW_HIDE);

			m_static2.ShowWindow(SW_HIDE);
			m_priadd.ShowWindow(SW_HIDE);
			m_pubadd.ShowWindow(SW_HIDE);

			m_static3.ShowWindow(SW_HIDE);
			m_keybitc.ShowWindow(SW_HIDE);
			m_BUTTON1.ShowWindow(SW_HIDE);
			m_showpubkeyc.ShowWindow(SW_HIDE);
			m_showprikeyc.ShowWindow(SW_HIDE);
			m_showprikey2c.ShowWindow(SW_HIDE);

			m_static4.ShowWindow(SW_NORMAL);
			m_static5.ShowWindow(SW_NORMAL);
			m_static6.ShowWindow(SW_NORMAL);
			m_static7.ShowWindow(SW_NORMAL);
			m_source.ShowWindow(SW_NORMAL);
			m_encode.ShowWindow(SW_NORMAL);
			m_decode.ShowWindow(SW_NORMAL);
			m_BUTTON2.ShowWindow(SW_NORMAL);
			m_BUTTON3.ShowWindow(SW_NORMAL);

			break;
		   }

	}
}

void CRsasystemDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here

	CTime Time1 = CTime::GetCurrentTime();
	int  t_mouth1=Time1.GetMonth();
	int  t_day1=Time1.GetDay();
	int  t_hour1=Time1.GetHour();
	int  t_min1=Time1.GetMinute();
	int  t_sec1=Time1.GetSecond();
    long m_time=t_mouth1*30*24*3600+t_day1*24*3600+t_hour1*3600+60*t_min1+t_sec1;
	m_seed1=100000000+m_time;
	m_seed2=200000000+m_time+rand();
	m_seed3=300000000+m_time+rand();
	m_seed4=400000000+m_time+rand();
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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