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

📄 zhouweidlg.cpp

📁 对应MSP430椭圆曲线加密器的客户端应用程序
💻 CPP
字号:
// zhouweiDlg.cpp : implementation file
//

#include <stdafx.h>
#include "zhouwei.h"
#include "zhouweiDlg.h"
#include <iostream.h>

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

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

/////////////////////////////////////////////////////////////////////////////
// CZhouweiDlg dialog

CZhouweiDlg::CZhouweiDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CZhouweiDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CZhouweiDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CZhouweiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CZhouweiDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CZhouweiDlg, CDialog)
	//{{AFX_MSG_MAP(CZhouweiDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON3, Onencryption)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON4, Ondecode)
	ON_BN_CLICKED(IDC_BUTTON1, Onbeginencryption)
	ON_BN_CLICKED(IDC_BUTTON9, Onbegindecode)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZhouweiDlg message handlers
int compare(unsigned char *preadBuf)
{
	int i;
	for(i=0;preadBuf[i]!='\0';i++)
	{
		if(preadBuf[i]=='0')
			return 0;
	}
	return 1;
}
BOOL CZhouweiDlg::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 CZhouweiDlg::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 CZhouweiDlg::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 CZhouweiDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CZhouweiDlg::Onencryption() 
{
	int want;
	// TODO: Add your control notification handler code here
    unsigned char command_key=2;
	unsigned char command_en=3;
	unsigned char longth_key=4;
	unsigned char longth_en_send=3;
	unsigned char longth_en_receive=14;


    
	GetDlgItem(IDC_EDIT3)->GetWindowText(ch3,20);//输入加密方公钥
	GetDlgItem(IDC_EDIT4)->GetWindowText(ch4,20);
    //this->file1->Write(preadBuf_text,dwFileLen);
	//this->file1->Close;
	//MessageBox((char*)preadBuf_text);
	HANDLE com;
	SerialDriver SD;
	unsigned long l;
	com=SD.initCOM(1);
	SD.writeCom(com,&command_key,1,&l);
	SD.writeCom(com,&longth_key,1,&l);
	SD.writeCom(com,(unsigned char*)ch3,1,&l);
	SD.writeCom(com,(unsigned char*)ch4,1,&l);
	l=0;
	
    want = 2;
	while (want != 0)
	{
		SD.readCom(com,&preadBuf_ser[4 - want],want,&l);
	    want = want - l;
	}


	FILE *fp1;

	fp1=fopen(csFilename,"wb"); 



	for(unsigned int i=0;i<(dwFileLen);i++)
	{
		SD.writeCom(com,&command_en,1,&l);
		SD.writeCom(com,&longth_en_send,1,&l);
		SD.writeCom(com,(preadBuf_text+i),1,&l);
		l=0;
		want=8;
		
		while (want != 0)
		{
			SD.readCom(com,&pwriteBuf_text[8 - want],want,&l);
			want = want - l;
		}
        for(int j=0;j<6;j++)
		//*(pwriteBuf+6*i+j)=pwriteBuf_text[2+j];
		pwriteBuf[j]=pwriteBuf_text[2+j];
		//this->file1->Write(pwriteBuf_text+2,6);
		 
		fseek(fp1,0,2);
	
		fwrite(pwriteBuf,6,1,fp1);
			
		
	
			//	this->MessageBox((const char *)pwriteBuf);
	}
	/*if((dwFileLen%2)!=0)
	{
		SD.writeCom(com,&command_en,1,&l);
		SD.writeCom(com,&command_en,1,&l);//传3个字节
		SD.writeCom(com,(preadBuf_text+dwFileLen),1,&l);
		l=0;
		want=8;
		while (want != 0)
		{
			SD.readCom(com,&pwriteBuf_text[8 - want],want,&l);
			want = want - l;
		}
		this->file1->Write(pwriteBuf_text+2,6);
	}*/

	fclose(fp1);	
    SD.closeCom(com);
    

}

void CZhouweiDlg::OnButton5() //选择加密明文
{
	// TODO: Add your control notification handler code here
	CString	m_sFilename;
	CFileDialog fileDlg(TRUE);
	fileDlg.m_ofn.lpstrTitle="我的文件打开对话框";
	fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	
	if(IDOK==fileDlg.DoModal())
	{
		CFile file(fileDlg.GetFileName(),CFile::modeRead);		
		//DWORD dwFileLen;
		dwFileLen=file.GetLength();
		preadBuf_text=new unsigned  char[dwFileLen+1];
		preadBuf_text[dwFileLen]=0;
		file.Read(preadBuf_text,dwFileLen);
		m_sFilename = fileDlg.GetPathName();
		GetDlgItem(IDC_EDIT9)->SetWindowText(m_sFilename);
		file.Close();
	//	MessageBox(pBuf);
	}
}

void CZhouweiDlg::OnButton6() 
{
	// TODO: Add your control notification handler code here
	CString	m_sFilename;
	CFileDialog fileDlg(FALSE);
	fileDlg.m_ofn.lpstrTitle="我的文件保存对话框";
	fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	fileDlg.m_ofn.lpstrDefExt="txt";

	if(IDOK==fileDlg.DoModal())
	{
		CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
		//CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
		//file.Write();
		csFilename=file.GetFileName();
		file.Close();
	}
	m_sFilename = fileDlg.GetPathName();
	GetDlgItem(IDC_EDIT10)->SetWindowText(m_sFilename);
}

void CZhouweiDlg::OnButton7() 
{
	// TODO: Add your control notification handler code here
	CString	m_sFilename;
	CFileDialog fileDlg(TRUE);
	fileDlg.m_ofn.lpstrTitle="我的文件打开对话框";
	fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	
	if(IDOK==fileDlg.DoModal())
	{
		CFile file(fileDlg.GetFileName(),CFile::modeRead);
		//char *pBuf;
		//DWORD dwFileLen;
		dwFileLen=file.GetLength();
		preadBuf_text=new unsigned  char[dwFileLen+1];
		preadBuf_text[dwFileLen]=0;
		file.Read(preadBuf_text,dwFileLen);
		m_sFilename = fileDlg.GetPathName();
	    GetDlgItem(IDC_EDIT11)->SetWindowText(m_sFilename);
		file.Close();
		//MessageBox(pBuf);
	}
}

void CZhouweiDlg::OnButton8() 
{
	// TODO: Add your control notification handler code here
	CString	m_sFilename;
	CFileDialog fileDlg(FALSE);
	fileDlg.m_ofn.lpstrTitle="我的文件保存对话框";
	fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	fileDlg.m_ofn.lpstrDefExt="txt";
	if(IDOK==fileDlg.DoModal())
	{
		CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
		//file.Write();csFilename2
		ctFilename=fileDlg.GetPathName();
		file.Close();
		
	}
	m_sFilename = fileDlg.GetPathName();
	GetDlgItem(IDC_EDIT12)->SetWindowText(m_sFilename);
}

void CZhouweiDlg::Ondecode() 
{
	int want;
	
	unsigned char command_key=5;
	unsigned char command_de=6;
	unsigned char longth_key=4;
	unsigned char longth_de_send=8;
	unsigned char longth_de_receive=3;

    GetDlgItem(IDC_EDIT5)->GetWindowText(ch5,20);//输入加密方公钥
	GetDlgItem(IDC_EDIT6)->GetWindowText(ch6,20);
 
	HANDLE com;
	SerialDriver SD;
	unsigned long l;
	com=SD.initCOM(1);
	SD.writeCom(com,&command_key,1,&l);
	SD.writeCom(com,&longth_key,1,&l);
	SD.writeCom(com,(unsigned char*)ch5,1,&l);
	SD.writeCom(com,(unsigned char*)ch6,1,&l);
	l=0;
	want = 2;
	while (want != 0)
	{
		SD.readCom(com,&preadBuf_ser[2 - want],want,&l);
	    want = want - l;
	}
	FILE *fp2;
//csFilename2="c.txt";
    //fp2=fopen("c.txt","r+");   
	fp2=fopen(ctFilename,"wb");
	for(unsigned int i=0;i<(dwFileLen/6);i++)
	{
		SD.writeCom(com,&command_de,1,&l);
		SD.writeCom(com,&longth_de_send,1,&l);
		SD.writeCom(com,(preadBuf_text+6*i),6,&l);
		l=0;
		want=3;
		
		while (want != 0)
		{
			SD.readCom(com,&pwriteBuf_text[3 - want],want,&l);
			want = want - l;
		}
		//pwriteBuf2
		pwriteBuf2[0]=pwriteBuf_text[2];
		
		
	fseek(fp2,0,2);
	fwrite(pwriteBuf2,1,1,fp2);
			
	
	}
    fclose(fp2);
	

    SD.closeCom(com);





}

void CZhouweiDlg::Onbeginencryption() 
{
	// TODO: Add your control notification handler code here
	HANDLE com;
	unsigned char command=1;
	unsigned char length_send=3;
	unsigned char length_receive=4;
	SerialDriver SD;
	unsigned long l;
	int i=0;
	int want;
	com=SD.initCOM(1);
	SYSTEMTIME sysTm;
	::GetLocalTime(&sysTm);
	unsigned char time_ws,time_wm;
	//time_ws=(unsigned char)sysTm.wMilliseconds;
    time_ws=(unsigned char)sysTm.wMinute;
	//time_ws=(unsigned char)sysTm.wSecond;
	//time_wm=time_wm<<4;
	//time_ws=time_ws&time_wm;
	SD.writeCom(com,&command,1,&l);
	SD.writeCom(com,&length_send,1,&l);
	SD.writeCom(com,&time_ws,1,&l);
	l=0;
	want = 4;
	while (want != 0)
	{
		SD.readCom(com,&preadBuf_ser[4 - want],want,&l);
	    want = want - l;
	}
	ch1[0]=(char)preadBuf_ser[2];
	ch2[0]=(char)preadBuf_ser[3];
	for(i=1;i<=47;i++)
	{
		ch2[i]=0;
		ch1[i]=0;
	}
    for(i=1;i<=24;i++)
	{
		srand(time_ws++); 
		ch1[i]=rand()%10+0x30;
	}
	for(i=1;i<=24;i++)
	{
		srand(time_ws++); 
		ch2[i]=rand()%10+0x30;
	}
	GetDlgItem(IDC_EDIT1)->SetWindowText(ch1);//输出加密方公钥
	GetDlgItem(IDC_EDIT2)->SetWindowText(ch2);


	SD.closeCom(com);
}

void CZhouweiDlg::Onbegindecode() 
{
	// TODO: Add your control notification handler code here
	HANDLE com;
	unsigned char command=4;
	unsigned char length_send=3;
	unsigned char length_receive=4;

	int i;
    SYSTEMTIME sysTm;
	::GetLocalTime(&sysTm);
	unsigned char time_ws,time_wm;
	//time_ws=(unsigned char)sysTm.wMilliseconds;
	time_wm=(unsigned char)sysTm.wMinute;
	time_ws=(unsigned char)sysTm.wSecond;
	time_wm=time_wm<<4;
	time_ws=time_ws&time_wm;
    int want;
	SerialDriver SD;
	unsigned long l;
	com=SD.initCOM(1);
	SD.writeCom(com,&command,1,&l);
	SD.writeCom(com,&length_send,1,&l);
	SD.writeCom(com,&time_ws,1,&l);
	l=0;
	want = 4;
	while (want != 0)
	{
		/*if(SD.readCom(com,preadBuf,4,&l)==-1)
			this->MessageBox("Read com ERORR");*/
		/*else if((l == 4)&&(preadBuf[0]==command)&&(preadBuf[1]==length_receive))
		{
			//this->MessageBox((const char *)&w);
			ch1[0]=(char)preadBuf[2];
		    ch2[0]=(char)preadBuf[3];
			/*this->MessageBox(ch1);
			this->MessageBox(ch2);
		}*/
		SD.readCom(com,&preadBuf_ser[4 - want],want,&l);
	    want = want - l;
	}
	ch7[0]=(char)preadBuf_ser[2];
	ch8[0]=(char)preadBuf_ser[3];
	for(i=1;i<=47;i++)
	{
		ch7[i]=0;
		ch8[i]=0;
	}
    for(i=1;i<=24;i++)
	{
		srand(time_ws++); 
		ch7[i]=rand()%10+0x30;
	}
	for(i=1;i<=24;i++)
	{
		srand(time_ws++); 
		ch8[i]=rand()%10+0x30;
	}
	GetDlgItem(IDC_EDIT7)->SetWindowText(ch7);//输出加密方公钥
	GetDlgItem(IDC_EDIT8)->SetWindowText(ch8);


	SD.closeCom(com);
	
	


}

⌨️ 快捷键说明

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