?? clientdlg.cpp
字號:
// ClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"
#include "rand.h"
#include "ChipstBankApi.h"
#include "md5.h"
#include "des.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()
*/
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
m_filename = _T("");
m_savefile = _T("");
m_filekey = _T("");
m_time = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
DDX_Control(pDX, IDC_EDIT_SEND, m_editSend);
DDX_Control(pDX, IDC_EDIT_RECEIVE, m_editReceive);
DDX_Control(pDX, IDC_EDIT_INFORMATION, m_editInfo);
DDX_Control(pDX, IDC_BUTTON_SENDRAND, m_btnSendRand);
DDX_Control(pDX, IDC_BUTTON_SENDDATA, m_btnSendData);
DDX_Control(pDX, IDC_BUTTON_DECODE, m_btnDeCode);
DDX_Control(pDX, IDC_BUTTON_CREATEKEY, m_btnCreateKey);
DDX_Control(pDX, IDC_BUTTON_CONNECT, m_btnConnect);
DDX_Text(pDX, IDC_EDIT_inputFile, m_filename);
DDX_Text(pDX, IDC_EDIT_outputFile, m_savefile);
DDX_Text(pDX, IDC_EDIT_FilePSW, m_filekey);
DDX_Text(pDX, IDC_EDIT_time, m_time);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_SENDRAND, OnButtonSendrand)
ON_BN_CLICKED(IDC_BUTTON_CREATEKEY, OnButtonCreatekey)
ON_BN_CLICKED(IDC_BUTTON_SENDDATA, OnButtonSenddata)
ON_BN_CLICKED(IDC_BUTTON_DECODE, OnButtonDecode)
ON_BN_CLICKED(IDC_BUTTON_RECV_DATA, OnButtonRecvData)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_inFile, OnBUTTONinFile)
ON_BN_CLICKED(IDC_BUTTON_outFile, OnBUTTONoutFile)
ON_BN_CLICKED(IDC_BUTTON_enFile, OnBUTTONenFile)
ON_BN_CLICKED(IDC_BUTTON_deFile, OnBUTTONdeFile)
ON_BN_CLICKED(IDC_BUTTON_inKey, OnBUTTONinKey)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers
BOOL CClientDlg::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
m_btnCreateKey.EnableWindow(FALSE);
m_btnDeCode.EnableWindow(FALSE);
m_btnSendData.EnableWindow(FALSE);
m_btnSendRand.EnableWindow(FALSE);
unsigned char temp[4] = {'u','n','s',0};
m_usbHandle=OpenUsbDisk(temp);
return TRUE; // return TRUE unless you set the focus to a control
}
void CClientDlg::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 CClientDlg::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 CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CClientDlg::OnButtonConnect()
{
// TODO: Add your control notification handler code here
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);
m_sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
// SOCKADDR_IN clientaddr;
// clientaddr.sin_family = AF_INET;
// clientaddr.sin_port = htons(3344);
// clientaddr.sin_addr.S_addr = htonl(INADDR_LOOPBACK);
// bind(sock,(sockaddr*)&clientaddr,sizeof(clientaddr));
SOCKADDR_IN servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(3344);
servaddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if (connect(m_sock,(sockaddr*)&servaddr,sizeof(servaddr)))
{
m_editInfo.SetWindowText("connect failed");
}
else
{
m_editInfo.SetWindowText("connect successful");
m_btnConnect.EnableWindow(FALSE);
m_btnSendRand.EnableWindow(TRUE);
}
}
void CClientDlg::OnButtonSendrand()
{
// TODO: Add your control notification handler code here
my_srand((unsigned)(time(NULL)));
m_randvalue1 = my_rand();
//加密
/* char prekey[32];
char inbuff[32], outbuff[32];
m_prekey = "12345678";
memcpy(prekey, (LPCTSTR)m_prekey, 8);
sprintf(inbuff,"%d",m_randvalue1);
CString SendRan1;
DES jm;
jm.Des_Go(outbuff, inbuff, sizeof(inbuff), prekey, sizeof(prekey), ENCRYPT);
for (int i=0; i<8; i++)
SendRan1 += outbuff[i];
UpdateData(FALSE);
send(m_sock, SendRan1, SendRan1.GetLength(), 0);
char buf[32];
recv(m_sock,buf,32,0);
m_randvalue2 = atol(buf);
m_editReceive.SetWindowText(buf);
*/
//明文傳輸
CString data;
char buf[32];
ZeroMemory(buf,32);
data.Format("%d",m_randvalue1);
send(m_sock,data.GetBuffer(0),data.GetLength(),0);
m_editInfo.SetWindowText(data);
m_btnCreateKey.EnableWindow(TRUE);
char buff[32];
ZeroMemory(buff,32);
recv(m_sock,buff,32,0);
m_editReceive.SetWindowText(buff);
m_randvalue2 = (ULONG)atol(buff);
}
void CClientDlg::OnButtonCreatekey()
{
// TODO: Add your control notification handler code here
ULONG uValue = m_randvalue2 + m_randvalue1;
CString temp;
temp.Format("%d",uValue);
m_key = MD5String(temp.GetBuffer(0));
WritePrivateUserSector(m_usbHandle,2,(unsigned char *)m_key,1,0);
m_editInfo.SetWindowText(m_key);
m_btnCreateKey.EnableWindow(FALSE);
m_btnSendData.EnableWindow(TRUE);
m_btnDeCode.EnableWindow(TRUE);
}
void CClientDlg::OnButtonSenddata()
{
// TODO: Add your control notification handler code here
CString senddata;
m_editSend.GetWindowText(senddata);
char nowkey[32];
char inbuff[32], outbuff[32];
memcpy(nowkey, (LPCTSTR)m_key, 32);
memcpy(inbuff, (LPCTSTR)senddata, 32);
DES jm;
CString DEsenddata;
jm.Des_Go(outbuff, inbuff, sizeof(inbuff), nowkey, sizeof(nowkey), ENCRYPT);
for (int i=0; i<32; i++)
DEsenddata += outbuff[i];
UpdateData(FALSE);
//send(m_sock,senddata.GetBuffer(0),senddata.GetLength(),0);
// char buf[32];
// memcpy(buf, (LPCTSTR)senddata, 32);
send(m_sock, DEsenddata, DEsenddata.GetLength(), 0);
}
void CClientDlg::OnButtonDecode()
{
// TODO: Add your control notification handler code here
// CString strCode;
// m_editReceive.GetWindowText(strCode);
// CString strdecode;
// //解密
// CString final;
// final = strCode + "\n" +strdecode;
// m_editReceive.SetWindowText(final);
}
void CClientDlg::OnButtonRecvData()
{
// TODO: Add your control notification handler code here
char buf[1024];
ZeroMemory(buf,1024);
recv(m_sock,buf,1024,0);
m_editReceive.SetWindowText(buf);
}
void CClientDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
CloseUsbDisk(m_usbHandle);
shutdown(m_sock,0);
closesocket(m_sock);
}
void CClientDlg::OnButton1()
{
// TODO: Add your control notification handler code here
}
void CClientDlg::OnBUTTONinFile()
{
UpdateData(TRUE);
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY,"ALL Files(*.*)|*.*||");
if (IDOK != dlg.DoModal()) return;
m_filename = dlg.GetPathName();
UpdateData(FALSE); // TODO: Add your control notification handler code here
}
void CClientDlg::OnBUTTONoutFile()
{
UpdateData(TRUE);
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY,"ALL Files(*.*)|*.*||");
if (IDOK != dlg.DoModal()) return;
m_savefile = dlg.GetPathName();
UpdateData(FALSE);
}
void CClientDlg::OnBUTTONenFile()
{
UpdateData(TRUE);
if(m_filename == "") return;
CFile inFile(m_filename, CFile::modeRead);
long inFileLen = inFile.GetLength();
long a = inFileLen/8;
long b = inFileLen%8;
if (m_savefile == "")
m_savefile = m_filename + ".uns";
CFile outFile(m_savefile, CFile::modeCreate|CFile::modeWrite);
inFile.SeekToBegin();
outFile.SeekToBegin();
DES des;
char inbuff[8], outbuff[8], skey[8];
memcpy(skey, (LPCTSTR)m_filekey, 8);
CTime time1, time2;
time1 = CTime::GetCurrentTime();
for (long i=0; i<a; i++)
{
inFile.Read(inbuff, 8);
des.Des_Go(outbuff, inbuff, sizeof(inbuff), skey, sizeof(skey), ENCRYPT);
outFile.Write(outbuff, 8);
}
if (b>0)
{
inFile.Read(inbuff, b);
des.Des_Go(outbuff, inbuff, sizeof(inbuff), skey, sizeof(skey), ENCRYPT);
outFile.Write(outbuff, 8);
}
time2 = CTime::GetCurrentTime();
CTimeSpan timespan = time2 - time1;
CString strtimespan = timespan.Format("Seconds:%S");
m_time = strtimespan + 's';
inFile.Close();
outFile.Close();
MessageBox("加密成功!");
UpdateData(FALSE);
}
void CClientDlg::OnBUTTONdeFile()
{
UpdateData(TRUE);
if(m_filename=="")return;
CFile inFile(m_filename,CFile::modeRead);
long inFileLen=inFile.GetLength();
long a = inFileLen/8;
long b = inFileLen%8;
if (m_savefile == "")
m_savefile = m_filename+".uns.txt";
CFile outFile(m_savefile,CFile::modeCreate|CFile::modeWrite);
inFile.SeekToBegin();
outFile.SeekToBegin();
DES des;
char inbuff[8],oubuff[8],skey[8];
memcpy(skey,(LPCTSTR)m_filekey,8);
CTime time1,time2;
time1 = CTime::GetCurrentTime();
m_time = "解密中...";
for (long i=0; i<a; i++)
{
inFile.Read(inbuff,8);
des.Des_Go(oubuff, inbuff, sizeof(inbuff), skey, sizeof(skey), DECRYPT);
outFile.Write(oubuff,8);
}
if (b>0)
{
MessageBox("文件可能已損壞或非經UNS加密!");
}
time2 = CTime::GetCurrentTime();
CTimeSpan timespan = time2-time1;
CString strspan = timespan.Format("Seconds:%S");
m_time = strspan+"s";
inFile.Close();
outFile.Close();
MessageBox("解密成功!");
UpdateData(FALSE);
}
void CClientDlg::OnBUTTONinKey()
{
// TODO: Add your control notification handler code here
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -