📄 mysundlg.cpp
字号:
// MysunDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Mysun.h"
#include "MysunDlg.h"
#include "d3des.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)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMysunDlg dialog
#define WM_ACCEPT_TRANSFERS WM_USER + 100
///全局变量,用于对线程进行管理
CWinThread* pThreadListen; //监听线程 --> _ListenThread
CWinThread* pThreadSend; //连接线程 --> _ConnectThread
CMysunDlg::CMysunDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMysunDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMysunDlg)
m_key = _T("");
m_strFileName = _T("");
m_strFileSize = _T("0字节");
m_wPort = 9600;
m_nServerType = 0;
m_choosepath = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_psockServer = NULL;
m_psockClient = NULL;
m_strPath = _T("");
m_dwFileSize = 0;
m_bIsStop = FALSE;
m_bIsWait = FALSE;
m_bIsTransmitting = FALSE;
}
void CMysunDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMysunDlg)
DDX_Control(pDX, IDC_PROGRESS, m_ctrlProgress);
DDX_Text(pDX, IDC_KEY, m_key);
DDV_MaxChars(pDX, m_key, 32);
DDX_Text(pDX, IDC_FILE_NAME, m_strFileName);
DDX_Text(pDX, IDC_FILE_SIZE, m_strFileSize);
DDX_Text(pDX, IDC_PORT, m_wPort);
DDX_Radio(pDX, IDC_RADIO_SERVER, m_nServerType);
DDX_Text(pDX, IDC_CHOOSEPATH, m_choosepath);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMysunDlg, CDialog)
//{{AFX_MSG_MAP(CMysunDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RADIO_SERVER, OnRadioServer)
ON_BN_CLICKED(IDC_RADIO_CLIENT, OnRadioClient)
ON_BN_CLICKED(IDC_BEGIN, OnBegin)
ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
ON_BN_CLICKED(IDC_SELECT_FILE, OnSelectFile)
ON_BN_CLICKED(IDC_STOP_TRANSFERS, OnStopTransfers)
ON_MESSAGE(WM_ACCEPT_TRANSFERS, OnAcceptTransfers)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
ON_BN_CLICKED(IDC_DECRYPT, OnDecrypt)
ON_BN_CLICKED(IDC_CHOOSE, OnChoose)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_EN_CHANGE(IDC_KEY, OnChangeKey)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMysunDlg message handlers
BOOL CMysunDlg::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
CString strHostName,strIPAddress;
if(GetLocalHostInfo(strHostName, strIPAddress))
return FALSE;
GetDlgItem(IDC_IPADDRESS)->SetWindowText(strIPAddress);
GetDlgItem(IDC_IPADDRESS)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMysunDlg::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 CMysunDlg::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 CMysunDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/* ---------------------------------- < 加密文件 > ---------------------------------- */
void CMysunDlg::OnChoose()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE,NULL,NULL,OFN_READONLY,"File(*.*)|*.*");
if(dlg.DoModal ()==IDOK)
{
m_choosepath=dlg.GetPathName();
m_name=dlg.GetFileExt();
UpdateData(FALSE);
}
}
void CMysunDlg::OnEncrypt()
{
// TODO: Add your control notification handler code here
DESEncrypt();
}
void CMysunDlg::OnDecrypt()
{
// TODO: Add your control notification handler code here
unsigned char key[17]={0},input[10*1024]={0},output[10*1024]={0};
CString filename;
CFile pfile,lfile;
int len;
UpdateData(TRUE);
if(m_choosepath=="")
{
AfxMessageBox("请选择加密文件!");
return;
}
if(m_key=="")
{
AfxMessageBox("请输入密钥!");
return;
}
else
{
Char_Hex((unsigned char*)m_key.GetBuffer (17),key,16);
AfxMessageBox("请选择保存路径!");
CFileDialog pfdlg(FALSE,NULL,NULL,OFN_READONLY,"File(*.*)|*.*");
if (pfdlg.DoModal()==IDOK) //以模式对话框形式打开
{
pfdlg.m_ofn.lpstrFilter = m_name; //pFileDlg.GetFileExt(); //获取打开文件的后缀名
filename=pfdlg.m_ofn.lpstrFile; //获取文件保存的文件名
filename+="." ;
filename+=m_name;
lfile.Open(filename,CFile::modeCreate|CFile::modeReadWrite);
}
pfile.Open (m_choosepath,CFile::modeReadWrite);
pfile.Read (input,pfile.GetLength ()-1);
my_c3des(input,pfile.GetLength ()-1,output,key,DE1);
pfile.Read (input,1);
input[1]=0x00;
len=atoi((char*)input);
lfile.Write (output,pfile.GetLength ()-1-len);
pfile.Close ();
lfile.Close ();
GetDlgItem(IDC_KEY)->SetWindowText(_T(""));
GetDlgItem(IDC_CHOOSEPATH)->SetWindowText(_T(""));
UpdateData(TRUE);
AfxMessageBox("文件解密成功!");
}
}
void my_c3des(unsigned char *inblock, int len, unsigned char *outblock, unsigned char *m_key, short edf)
{
unsigned char vector[9]={0};
unsigned char temp[9]={0};
int blocknum;
int i,j;
blocknum=len/8;
if((len%8)!=0)
blocknum++;
memset(vector,0x00,8);
if(edf == EN0)
{
for(i=0;i<blocknum;i++)
{
for(j=0;j<8;j++)
vector[j]^=inblock[i*8+j];
my_3des(vector,temp,m_key,EN0);
memcpy(&outblock[i*8],temp,8);
memcpy(vector,temp,8);
}
}
if(edf == DE1)
{
for(i=0;i<blocknum;i++)
{
my_3des(&inblock[i*8],temp,m_key,DE1);
for(j=0;j<8;j++)
vector[j]^=temp[j];
memcpy(&outblock[i*8],vector,8);
memcpy(vector,&inblock[i*8],8);
}
}
}
void my_3des(unsigned char *inblock, unsigned char *outblock, unsigned char *m_key, short edf)
{
unsigned char k1[9]={0},k2[9]={0};
unsigned char temp1[9]={0},temp2[9]={0};
memcpy(k1,m_key,8);
memcpy(k2,m_key+8,8);
my_des(inblock,temp1,k1,edf);
my_des(temp1,temp2,k2,!edf);
my_des(temp2,outblock,k1,edf);
}
void Char_Hex(unsigned char *bufin,unsigned char *bufout,int len)
{
int j=0;
unsigned char temp1=0;
unsigned char temp2=0;
for(int i=0;i<len;i++)
{
temp1=bufin[j];
temp2=bufin[j+1];
if(temp1>=48&&temp1<=57)
temp1-=48;
else if(temp1>=97&&temp1<=102)
temp1-=87;
else if(temp1>=65 &&temp1<=70)
temp1-=55;
if(temp2>=48&&temp2<=57)
temp2-=48;
else if(temp2>=97&&temp2<=102)
temp2-=87;
else if(temp2>=65 &&temp2<=70)
temp2-=55;
bufout[i]=(temp1<<4)|temp2;
j++;
j++;
}
}
void Hex_Char(unsigned char *bufin,unsigned char *bufout,int len)
{
int j=0;
unsigned char temp1=0;
unsigned char temp2=0;
for(int i=0;i<len;i++)
{
temp1=bufin[i]&0x0F;
temp2=bufin[i]>>4;
if(temp1>=0&&temp1<=9)
temp1+=48;
else
temp1+=87;
if(temp2>=0&&temp2<=9)
temp2+=48;
else
temp2+=87;
bufout[j]=temp2;
bufout[j+1]=temp1;
j++;
j++;
}
}
/* ---------------------------------- < 线程 > ---------------------------------- */
//接收文件线程
UINT _ListenThread(LPVOID lparam)
{
CMysunDlg* pDlg = (CMysunDlg*)lparam;
//创建套接字
CSocket sockSrvr;
if(!sockSrvr.Create(pDlg->m_wPort + PORT))
{
pDlg->TransfersFailed();
::MessageBox((HWND)lparam, pDlg->GetError(GetLastError()), _T("错误"), MB_ICONHAND|MB_OK);
return -1;
}
//开始监听
if(!sockSrvr.Listen())
{
pDlg->TransfersFailed();
::MessageBox((HWND)lparam, pDlg->GetError(GetLastError()), _T("错误"), MB_ICONHAND|MB_OK);
return -1;
}
///向主对话框发送一个自定义消息wm_accept_transfers
///发送一个消息告诉发送方可以开始发送文件
pDlg->SendMessage(WM_ACCEPT_TRANSFERS);
//接受连接
CSocket recSo;
if(!sockSrvr.Accept(recSo))
{
::MessageBox((HWND)lparam, pDlg->GetError(GetLastError()), _T("错误"), MB_ICONHAND|MB_OK);
return -1;
}
sockSrvr.Close();
//调用主对话框类中的receivefile成员函数进行文件的接收
pDlg->ReceiveFile(recSo);
return 0;
}
//发送文件线程
UINT _SendThread(LPVOID lparam)
{
CMysunDlg* pDlg = (CMysunDlg*) lparam;
//创建套接字
CSocket sockClient;
if(!sockClient.Create())
{
pDlg->TransfersFailed();
::MessageBox((HWND)lparam, pDlg->GetError(GetLastError()), _T("错误"), MB_ICONHAND|MB_OK);
return -1;
}
CString strIPAddress;
UINT nPort;
pDlg->m_psockClient->GetPeerName(strIPAddress, nPort);
//连接到服务器端
if(!sockClient.Connect(strIPAddress, pDlg->m_wPort + PORT))
{
pDlg->TransfersFailed();
::MessageBox((HWND)lparam, pDlg->GetError(GetLastError()), _T("错误"), MB_ICONHAND|MB_OK);
return -1;
}
//调用主对话框类中的sendfile成员函数进行文件的发送
pDlg->SendFile(sockClient);
return 0;
}
/* ---------------------------------- < 线程 > ---------------------------------- */
void CMysunDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
void CMysunDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(m_bIsWait)
{
MessageBox(_T("等待中,请先停止传送后再退出"), _T("警告"), MB_ICONEXCLAMATION);
return ;
}
if(m_bIsTransmitting)
{
MessageBox(_T("文件传输中,请先停止传送后再退出"), _T("警告"), MB_ICONEXCLAMATION);
return ;
}
OnDisconnect();
CDialog::OnCancel();
}
void CMysunDlg::OnBegin()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
//当程序作为服务器
if(m_nServerType == SERVER)
{
//创建服务器套接字
m_psockServer = new CListenSocket(this);
if(!m_psockServer->Create(m_wPort))
{
delete m_psockServer;
m_psockServer = NULL;
MessageBox(GetError(GetLastError()), _T("错误"), MB_ICONHAND);
return ;
}
//监听客户端连接
if(!m_psockServer->Listen())
{
delete m_psockServer;
m_psockServer = NULL;
MessageBox(GetError(GetLastError()), _T("错误"), MB_ICONHAND);
return ;
}
}
else
{
//当程序作为客户端
if(((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS))->IsBlank())
{
MessageBox("IP 地址不能为空", "错误", MB_ICONHAND);
return ;
}
//创建客户端套接字
m_psockClient = new CClientSocket(this);
if(!m_psockClient->Create())
{
delete m_psockClient;
m_psockClient = NULL;
MessageBox(GetError(GetLastError()), _T("错误"), MB_ICONHAND);
return ;
}
//与服务器建立连接
CString strIPAddress;
GetDlgItem(IDC_IPADDRESS)->GetWindowText(strIPAddress);
if(!m_psockClient->Connect(strIPAddress, m_wPort))
{
delete m_psockClient;
m_psockClient = NULL;
MessageBox(GetError(GetLastError()), _T("错误"), MB_ICONHAND);
return ;
}
//初始化套接字
m_psockClient->Init();
GetDlgItem(IDC_IPADDRESS)->EnableWindow(FALSE);
}
GetDlgItem(IDC_RADIO_SERVER)->EnableWindow(FALSE);
GetDlgItem(IDC_RADIO_CLIENT)->EnableWindow(FALSE);
GetDlgItem(IDC_PORT)->EnableWindow(FALSE);
GetDlgItem(IDC_BEGIN)->EnableWindow(FALSE);
GetDlgItem(IDC_DISCONNECT)->EnableWindow(FALSE);
}
void CMysunDlg::OnDisconnect()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
//关闭客户端套接字m_psockClient
if(m_psockClient != NULL)
{
//告诉对方发送等待被取消
CMessage* pMsg = new CMessage(DISCONNECT);
m_psockClient->SendMsg(pMsg);
delete m_psockClient;
m_psockClient = NULL;
}
//关闭服务器端套接字m_psockServer
if(m_psockServer != NULL)
{
delete m_psockServer;
m_psockServer = NULL;
}
GetDlgItem(IDC_RADIO_SERVER)->EnableWindow(TRUE);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -