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

📄 elgmaltest1.cpp

📁 这个程序是网络信息安全概论课的课程实践,自己动手编写一个具于1024位大数 运算的ELGamal加密系统。 ELGamal 依赖大数运算
💻 CPP
字号:
// ELGmaltest1.cpp : implementation file
//

#include "stdafx.h"
#include "elgamaltool.h"
#include "ELGmaltest1.h"
#include "ELGamaltoolDlg.h"
#include "BigInt.h"
#include "CCommon.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CELGmaltest dialog


CELGmaltest::CELGmaltest(CWnd* pParent /*=NULL*/)
	: CDialog(CELGmaltest::IDD, pParent)
{
	//{{AFX_DATA_INIT(CELGmaltest)
	m_in = _T("");
	m_out = _T("");
	//}}AFX_DATA_INIT
  // pParent=(CELGamaltoolDlg *)GetParent();

}


void CELGmaltest::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CELGmaltest)
	DDX_Text(pDX, IDC_EDIT1, m_in);
	DDX_Text(pDX, IDC_EDIT2, m_out);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CELGmaltest, CDialog)
	//{{AFX_MSG_MAP(CELGmaltest)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CELGmaltest message handlers
//class CELGamaltoolDlg : public CDialog
void CELGmaltest::OnOK() 
{
	// TODO: Add extra validation here
 

  
  CELGamaltoolDlg *pParent=(CELGamaltoolDlg *)GetParent();
 		if(pParent->ready==0)
	{
		m_out=_T("请先生成随机密钥");
        CDialog::UpdateData(FALSE);
		return;
	}
	CDialog::UpdateData(TRUE);
	length=m_in.GetLength();
   if(length==0)
   	{
		m_out=_T("请先输入明文");
        CDialog::UpdateData(FALSE);
		return;
	}

   if(m_out.GetLength()>0)
	   m_out=_T("");


   //CString caption;
   //AfxGetMainWnd( )->GetWindowText(caption);
   AfxGetApp()->BeginWaitCursor();
 //  AfxGetMainWnd( )->SetWindowText("正在生成密文!请等待...");*/
   CTime t0=CTime::GetCurrentTime();
   /*CELGmaltest *ptest=(CELGmaltest *)GetDlgItem(IDD_ELGmaltest);
    GetDlgItemText(IDD_ELGmaltest, caption);
	ptest->BeginWaitCursor();
	SetDlgItemText(IDD_ELGmaltest,"正在生成密文!请等待..."); 
 */
   str_mw="";
    int c1;
	for(int i=0;i<length;i++)
	{
		c1=m_in[i];
		if(c1<0)
			c1+=256;
		str_mw+=Int10_To_Hexstr(c1,2);//十进制转换成十六进制字符串
	}

  /*	 m_out=str_mw;;
     CDialog::UpdateData(FALSE);  */

  length=str_mw.GetLength();
  CBigInt temp;
  CBigInt temp1;
  CString s,str;
 int j=0;
 for( i=0;i<length;i+=2)
   {
	   
       temp.Mov(pParent->B.RsaTrans(pParent->a,pParent->p));
	   str=str_mw.Mid(i,2);
       temp1.Mov(Hexstr_To_Int10(str));
	   result[j].Mov(temp.Mul(temp1));
	   result[j].Mov(result[j].Mod(pParent->p));
	   result[j].Put(s);
	   j++;
	   m_out+=s;
     CDialog::UpdateData(FALSE);  
   }

 	       // 结束等待光标
    AfxGetApp()->EndWaitCursor();
	//AfxGetMainWnd( )->SetWindowText(caption); 
	CTime t1=CTime::GetCurrentTime();
	CTimeSpan t=t1-t0;
	CString s_time;
	s_time.Format("%d",t.GetTotalSeconds());
	s_time+="秒";
	MessageBox(s_time,"生成密文所用时间",MB_OK);


//	CDialog::OnOK();
}

void CELGmaltest::OnCancel() 
{
	// TODO: Add extra cleanup here

 CDialog::OnCancel(); 
}

void CELGmaltest::OnButton1() 
{
	// TODO: Add your control notification handler code here
  CELGamaltoolDlg *pParent=(CELGamaltoolDlg *)GetParent();
	   if((pParent->ready ==0)||(length==0))
	{
		m_out=_T("请先进行加密");
        CDialog::UpdateData(FALSE);
		return;
	}
   if(m_out.GetLength()>0)
	   m_out=_T("");

 //生成等待光标
 AfxGetApp()->BeginWaitCursor();
 CTime t0=CTime::GetCurrentTime();
 

 CString s=_T(""),plaintext=_T("");
 CBigInt temp,temp1;
 temp.Mov(pParent->p.Sub(pParent->b));
 temp.Mov(temp.Sub(1));
 temp.Mov(pParent->A.RsaTrans(temp,pParent->p));
 for(int t=0;t<length/2;t++)
 {
   temp1.Mov(result[t].Mul(temp));
   result[t].Mov(temp1.Mod(pParent->p));
   result[t].Put(s);
   plaintext+=s;
 }

 char c1;
 CString str;
  length=plaintext.GetLength();
 	for( int i=0;i<length;i+=2)
	{
		str=plaintext.Mid(i,2);
		c1=Hexstr_To_Int10(str); 
        if(c1<0)
			c1+=256;
		m_out+=c1;
	}
 CDialog::UpdateData(FALSE);

  // 结束等待光标
   AfxGetApp()->EndWaitCursor();
	//AfxGetMainWnd( )->SetWindowText(caption); 
	CTime t1=CTime::GetCurrentTime();
	CTimeSpan t2=t1-t0;
	CString s_time;
	s_time.Format("%d",t2.GetTotalSeconds());
	s_time+="秒";
	MessageBox(s_time,"生成密文所用时间",MB_OK);

}

⌨️ 快捷键说明

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