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

📄 数字签名dlg.cpp

📁 本程序实现DSA数字签名 是学习和研究密码学的良好资源 欢迎下载
💻 CPP
字号:
// 数字签名Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "数字签名.h"
#include "数字签名Dlg.h"
#include "BigInt.h"
#include "MD5.h"
#include "SHA1.h"

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

CBigInt sha(CBigInt y);
CBigInt pow2( const int x, int y);
CBigInt makerandnumber( unsigned int len);
CBigInt pow3(CBigInt x, CBigInt y, CBigInt m);

/////////////////////////////////////////////////////////////////////////////
// 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)
	virtual void OnOK();
	//}}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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DMyDlg dialog

DMyDlg::DMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(DMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(DMyDlg)
	m_P = _T("");
	m_Q = _T("");
	m_G = _T("");		//mpdified in 2007.6.10
	m_Y = _T("");
	m_S = _T("");
	m_V = _T("");
	m_Len = 0;
	m_OUT = _T("");
	m_IN = _T("");
	ready = 0;
	m_file = _T("");
	m_string = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void DMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DMyDlg)
	DDX_Text(pDX, IDC_P, m_P);
	DDX_Text(pDX, IDC_Q, m_Q);
	DDX_Text(pDX, IDC_G, m_G);		// mpdified in 2007.6.10
	DDX_Text(pDX, IDC_Y, m_Y);
	DDX_Text(pDX, IDC_S, m_S);
	DDX_Text(pDX, IDC_V, m_V);
	DDX_CBIndex(pDX, IDC_COMBO, m_Len);
	DDX_Text(pDX, IDC_OUTPUT, m_OUT);
	DDX_Text(pDX, IDC_INPUT, m_IN);
	DDX_Text(pDX, IDC_EDITFILE, m_file);
	DDX_Text(pDX, IDC_STRING, m_string);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(DMyDlg, CDialog)
	//{{AFX_MSG_MAP(DMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_GET, OnButtonGet)
	ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
	ON_BN_CLICKED(IDC_DECRYPT, OnDecrypt)
	ON_BN_CLICKED(IDC_Btnfile, OnBtnfile)
	ON_EN_CHANGE(IDC_EDITFILE, OnChangeEditfile)
	ON_EN_CHANGE(IDC_STRING, OnChangeString)
	ON_BN_CLICKED(IDC_SHA1_RADIO, OnSha1Radio)
	ON_BN_CLICKED(IDC_MD5_RADIO, OnMd5Radio)
	ON_CBN_EDITCHANGE(IDC_COMBO, OnEditchangeCombo)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DMyDlg message handlers

BOOL DMyDlg::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

	CheckRadioButton(IDC_MD5_RADIO,IDC_SHA1_RADIO,IDC_MD5_RADIO);
	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void DMyDlg::OnButtonGet() 
{
	// TODO: Add your control notification handler code here
	ready=1;
	UpdateData(TRUE);
    CTime t0=CTime::GetCurrentTime();
/*
	int len=8;  //int len=2; modified in 2007 6. 9
	for(int i=0;i<m_Len;i++){len*=2;}
	P.Mov(0);
	Q.Mov(0);
	N.Mov(0);
	E.Mov(0);
	P.GetPrime(len);
	Q.GetPrime(len);
	N.Mov(P.Mul(Q));
	N.Put(m_P);
	P.m_ulValue[0]--;
	Q.m_ulValue[0]--;
	P.Mov(P.Mul(Q));
	D.Mov(0x10001);		//mpdified in 2007.6.10
//	m_D="10001";		//mpdified in 2007.6.10
	E.Mov(D.Euc(P));
	E.Put(m_Q);
    CTime t1=CTime::GetCurrentTime();
    CTimeSpan t=t1-t0;
	m_OUT.Format("%d",t.GetTotalSeconds());
	m_OUT+=" 秒";
	Q.m_ulValue[0]=0;
*/
	CBigInt r,s,u,v[4],w,p,q,t; //,H,G,X,Y;
	int n=3,b=31,L=512,g=160,T=1;
	t.Mov(pow2(2,g));
	while (T)
	{
		s.Mov(makerandnumber(g));
//			r.Mov(s);
//			r.Mov(r.Add(1));
//			r.Mov(r.Mod(t));
//			u.Mov(sha(s));
		q.Mov(sha(s));
		q.m_ulValue[0]=q.m_ulValue[0]|0x1;
		q.m_ulValue[4]=q.m_ulValue[4]|0x80000000; 
		while (q.Rab()==0)  
			q.Mov(q.Add(2));
		int C=0,N=2;
		for (int k=0;k<4;k++)
		{
			v[k].Mov(s);
			v[k].Mov(v[k].Add(N+k));
			v[k].Mov(v[k].Mod(t));
			v[k].Mov(sha(v[k]));
		}
		w.Mov(0);
		r.Mov(1);
		for ( k=0;k<3;k++)
		{
			v[k].Mov(v[k].Mul(r));
			w.Mov(w.Add(v[k]));
			r.Mov(r.Mul(t));
		}
		s.Mov(pow2(2,b));
		v[k].Mov(v[k].Mod(s));
		v[k].Mov(v[k].Mul(r));
		w.Mov(w.Add(v[k]));
		r.Mov(w);
		r.Mov(r.Add(1));
		w.Mov(w.Mod(q));
		r.Mov(r.Sub(w));
		p.Mov(r);
		while (p.Rab()==0) 
		{
			C=C+1;
			N=N+n+1;
			if (C==4096) break;
			p.Mov(p.Add(q));
			p.Mov(p.Add(q));
		}
		if (C<4096)	break;
	}
	P.Mov(p);
	Q.Mov(q);
	H.Mov(makerandnumber(g));
	X.Mov(makerandnumber(g));
	X.Mov(X.Mod(Q));
	t.Mov(P.Sub(1));
	t.Mov(t.Div(Q));
	G.Mov(H.RsaTrans(t, Q));
	Y.Mov(G.RsaTrans(X, P));
	P.Put(m_P);
	Q.Put(m_Q);
	G.Put(m_G);
	Y.Put(m_Y);
//	m_D="10001";
    CTime t1=CTime::GetCurrentTime();
    CTimeSpan t2=t1-t0;
	m_OUT.Format("%d",t2.GetTotalSeconds());
	m_OUT+=" 秒";
	UpdateData(FALSE);
}

void DMyDlg::OnEncrypt() 
{
	// TODO: Add your control notification handler code here
	CBigInt g,t;
	if(ready==0)
	{
		m_OUT=_T("请先输入或生成N、D、E");
        UpdateData(FALSE);
		return;
	}
	UpdateData(TRUE);
    if(m_IN.GetLength()>256)
	{
		m_OUT=_T("N不得大于256位");
		UpdateData(FALSE);
		return;
	}
	K.Mov(makerandnumber(160));
	R.Mov(G.RsaTrans(K,P));
	R.Mov(R.Mod(Q));
	S.Mov(K.Euc(Q));
	g.Get(m_IN);
	t.Mov(X.Mul(R));
	t.Mov(t.Mod(Q));
	t.Mov(t.Add(g));
	S.Mov(S.Mul(t));
	S.Mov(S.Mod(Q));
	R.Put(m_OUT);
	S.Put(m_S);
/*	for(int i=0;i<m_IN.GetLength();i++)
	{
		if((m_IN[i]<'0')||
		   ((m_IN[i]>'9')&&(m_IN[i]<'A'))||
		   ((m_IN[i]>'F')&&(m_IN[i]<'a'))||
		   (m_IN[i]>'f'))
		{
			m_OUT=_T("待加密数据必须为0-9或A-F或a-f组成的整数");
			UpdateData(FALSE);
			return;
		}
	}*/
//	P.Get(m_IN);
//	if(P.Cmp(N)>=0)
//	{
//		m_OUT=_T("待加密数据必须小于N");
//        UpdateData(FALSE);
//		return;
//	}
//	Q.Mov(P.RsaTrans(E,N));
//	Q.Put(m_OUT);
	UpdateData(FALSE);
}

void DMyDlg::OnDecrypt() 
{
	CBigInt W,U1,U2,t;
	// TODO: Add your control notification handler code here
	if((ready==0)||(Q.m_ulValue[0]==0))
	{
		m_OUT=_T("请先进行数字签名");
        UpdateData(FALSE);
		return;
	}
	W.Mov(S.Euc(Q));
	U1.Get(m_IN);
	U1.Mov(U1.Mul(W));
	U1.Mov(U1.Mod(Q));
	U2.Mov(R.Mul(W));
	U2.Mov(U2.Mod(Q));
	V.Mov(G.RsaTrans(U1,P));
	V.Mov(V.Mod(P));
	t.Mov(Y.RsaTrans(U2,P));
	t.Mov(t.Mod(P));
	V.Mov(V.Mul(t));
	V.Mov(V.Mod(P));
	V.Mov(V.Mod(Q));
	if (V.Cmp(R)==0) V.Put(m_V);
	R.Put(m_V);


//	Q.Get(m_OUT);
//	P.Mov(Q.RsaTrans(D,N));
//	P.Put(m_OUT);
	UpdateData(FALSE);
}

void DMyDlg::OnBtnfile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog m_FileOpen(TRUE); 
//设置打开窗体的标题 
	m_FileOpen.m_ofn.lpstrTitle = _T("打开文件"); 
	m_FileOpen.m_ofn.lpstrFilter = _T("文件 (*.*)\0*.*\0\0"); 
//点击浏览按钮 
	if(IDOK == m_FileOpen.DoModal()) 
	{ 
	   //获取打开的文件路径 
	   CString m_FileName = m_FileOpen.GetPathName(); 
	   CEdit* pEdit = (CEdit*)(GetDlgItem(IDC_EDITFILE)); 
	   //将选择的文件路径名显示到IDC_EDITFILE表识的EDIT中 
	   pEdit->SetWindowText(m_FileName); 
	} 
}

void byte2strhash(unsigned char *hash,int hashlen,unsigned char *out)
{
int i,x,y;

	for (i = 0;i < hashlen; i++)
	{
	y = hash[i] >> 4;			

	y += 0x30;
	if (y > 0x39) y+= 0x07;

	x = hash[i] & 0x0F;			
	
	x += 0x30;
	if (x > 0x39) x+= 0x07;

	out[i << 1] = y;			
	out[(i << 1)+1] = x;		
	}

	out[hashlen << 1] = 0x00;

}

void DMyDlg::OnChangeEditfile() 
{
	// 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
	UpdateData(TRUE);   
    CString open=m_file;   
    MD5_CTX	md5_ctx;
    SHA1_CTX sha1_ctx;

	int hashradio;
 
	hashradio=GetCheckedRadioButton(IDC_MD5_RADIO,IDC_SHA1_RADIO);
	if(hashradio==IDC_MD5_RADIO)         //MD5
	{   
		int i;
		unsigned char digest[16],output[16];
		FILE* file;
        unsigned char buffer[16384];
	    MD5Init(&md5_ctx);
		file = fopen(open, "rb");
        i = fread(buffer, 1, 16384, file);
        MD5Update(&md5_ctx, buffer, i);
        MD5Final(digest, &md5_ctx);
        fclose(file);
		
		byte2strhash(digest,16,output);
		CString str(output);
		SetDlgItemText(IDC_INPUT,str);		
	}

	else           //SHA-1
	{
		int i;
		unsigned char digest[20],output[20];
		FILE* file;
        unsigned char buffer[16384];
	    SHA1Init(&sha1_ctx);
		file = fopen(open, "rb");
        i = fread(buffer, 1, 16384, file);
        SHA1Update(&sha1_ctx, buffer, i);
        SHA1Final(digest, &sha1_ctx);
        fclose(file);
		
		byte2strhash(digest,20,output);
		CString str(output);
		SetDlgItemText(IDC_INPUT,str);
	}
}

void DMyDlg::OnChangeString() 
{
	// 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
	UpdateData(TRUE);     
    MD5_CTX	md5_ctx;
    SHA1_CTX sha1_ctx;

	int hashradio;
    
	hashradio=GetCheckedRadioButton(IDC_MD5_RADIO,IDC_SHA1_RADIO);
	if(hashradio==IDC_MD5_RADIO)         //MD5
	{
		unsigned char digest[16],output[16];

		UpdateData(TRUE);   
        CString context=m_string; 

        MD5Init(&md5_ctx);
		unsigned char *buf = (unsigned char*)context.GetBuffer(context.GetLength()); 
        MD5Update(&md5_ctx,buf, strlen(context));
        MD5Final(digest, &md5_ctx);
		context.ReleaseBuffer();

		byte2strhash(digest,16,output);
		CString str(output);
		SetDlgItemText(IDC_INPUT,str);

       }

	else       //SHA1
	{
		unsigned char digest[20],output[20];
		UpdateData(TRUE);   
        CString context=m_string; 

        SHA1Init(&sha1_ctx);
		unsigned char *buf = (unsigned char*)context.GetBuffer(context.GetLength()); 
        SHA1Update(&sha1_ctx,buf, strlen(context));
        SHA1Final(digest, &sha1_ctx);
		context.ReleaseBuffer();

		byte2strhash(digest,20,output);	
		CString str(output);
		SetDlgItemText(IDC_INPUT,str);
	}
}

void DMyDlg::OnSha1Radio() 
{
	// TODO: Add your control notification handler code here
	
}

void DMyDlg::OnMd5Radio() 
{
	// TODO: Add your control notification handler code here
	
}

void DMyDlg::OnEditchangeCombo() 
{
	// TODO: Add your control notification handler code here
	
}

void DMyDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();
	
}

void CAboutDlg::OnOK() 
{
	// TODO: Add extra validation here	
	CDialog::OnOK();
}

⌨️ 快捷键说明

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