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

📄 cserversockdlg.cpp

📁 将客户方的文件用DES加密后
💻 CPP
字号:
// CServerSockDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CServerSock.h"
#include "CServerSockDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CCServerSockDlg dialog

CCServerSockDlg::CCServerSockDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCServerSockDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCServerSockDlg)
	m_FileName = _T("");
	m_PassWord = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCServerSockDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCServerSockDlg)
	DDX_Control(pDX, IDC_IPADDRESS, m_IpAddress);
	DDX_Text(pDX, IDC_FileName, m_FileName);
	DDX_Text(pDX, IDC_PassWord, m_PassWord);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCServerSockDlg, CDialog)
	//{{AFX_MSG_MAP(CCServerSockDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SetServer, OnSetServer)
	ON_BN_CLICKED(IDC_FindFile, OnFindFile)
	ON_BN_CLICKED(IDC_Send, OnSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCServerSockDlg message handlers

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

	if(AfxSocketInit(NULL)==NULL)
	{
		AfxMessageBox("error in socket init");
	}

	BYTE f0,f1,f2,f3;
	GetIpAddress(f0,f1,f2,f3);
	
	((CIPAddressCtrl*)(GetDlgItem(IDC_IPADDRESS)))->SetAddress(f0,f1,f2,f3);
	((CIPAddressCtrl*)(GetDlgItem(IDC_IPADDRESS)))->EnableWindow(false);
	// TODO: Add extra initialization here
	m_Init=false;
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CCServerSockDlg::OnSetServer() 
{
	// TODO: Add your control notification handler code here
	
}

void CCServerSockDlg::OnFindFile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fd(true);
	fd.m_ofn.lpstrFilter="All Files|*.*\0";
	if(fd.DoModal()==IDOK)
	{
		m_FileName=fd.GetPathName();
		UpdateData(false);
	}	
}

int CCServerSockDlg::GetIpAddress(BYTE &f0, BYTE &f1, BYTE &f2, BYTE &f3)
{
	char szHostName[256];
	CString sHostName;
	int nRetCode;
	nRetCode=gethostname(szHostName,sizeof(szHostName));
	if(nRetCode!=0)
	{
		//产生错误
		sHostName=_T("没有取得");
		return GetLastError();
	}
	sHostName=szHostName;

	struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
	if(lpHostEnt==NULL)
	{
		//产生错误
		f0=f1=f2=f3=0;
		return GetLastError();
	}
	//获取IP
	LPSTR lpAddr=lpHostEnt->h_addr_list[0];
	if(lpAddr)
	{
		struct in_addr inAddr;
		memmove(&inAddr,lpAddr,4);
		f0=inAddr.S_un.S_un_b.s_b1;
		f1=inAddr.S_un.S_un_b.s_b2;
		f2=inAddr.S_un.S_un_b.s_b3;
		f3=inAddr.S_un.S_un_b.s_b4;
	}
	return 0;
}

void CCServerSockDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
   //jiami
	UpdateData(TRUE);
if(m_FileName=="")return;
CFile ff(m_FileName,CFile::modeRead);
long lFileLen=ff.GetLength();
long c=lFileLen/8;
long d=lFileLen%8;
//if (m_savefile=="")
//m_savefile=m_filename+".aen";
	CString savefile=m_FileName+".aen";

   CFile fp(savefile,CFile::modeCreate|CFile::modeWrite);
   fp.SeekToBegin();
   ff.SeekToBegin();

//CString inbuff;


DES jm;




char inbuff[8],oubuff[8],skey[8];

/*	char *cr=NULL;
for(int j=0;j<16;j++)
 {
CString str=m_filekey.Mid(2*j,2);
skey[j]=unsigned char(strtol(str,&cr,16));
}*/
memcpy(skey,(LPCTSTR)m_PassWord,8);

    CTime tm1,tm2;
	tm1=CTime::GetCurrentTime();

	for(long i=0;i<c;i++)
	{
		ff.Read(inbuff,8);
		jm.Des_Go(oubuff, inbuff, sizeof(inbuff), skey,sizeof(skey), ENCRYPT);
		fp.Write(oubuff,8);
	}
	if(d>0)
	{
		ff.Read(inbuff,d);
		jm.Des_Go(oubuff, inbuff, sizeof(inbuff), skey,sizeof(skey), ENCRYPT);
		fp.Write(oubuff,8);
	}
    tm2=CTime::GetCurrentTime();
	CTimeSpan tmSpan=tm2-tm1;

	CString strspan=tmSpan.Format("Seconds:%S");
 //   m_sd=strspan+"s";
     
	ff.Close();
	fp.Close();
	AfxMessageBox("加密成功!");
	UpdateData(FALSE);

	//send
	if(!m_Init)
	{
 		const UINT port=4000;
 		CServerSock.Create(port);
 		CServerSock.Listen();
 		CServerSock.Accept(CListenSock);	
 		m_Init=true;
	}
 
	CFile myFile;
	if(!myFile.Open(savefile, CFile::modeRead | CFile::typeBinary))
	{
		AfxMessageBox("文件不存在!",MB_OK|MB_ICONERROR);
		return;
	}

	SOCKET_STREAM_FILE_INFO	StreamFileInfo;
	WIN32_FIND_DATA             FindFileData;

	FindClose(FindFirstFile(m_FileName,&FindFileData));
    memset(&StreamFileInfo,0,sizeof(SOCKET_STREAM_FILE_INFO));
    strcpy(StreamFileInfo.szFileTitle,myFile.GetFileName());

    StreamFileInfo.dwFileAttributes     =       FindFileData.dwFileAttributes;
    StreamFileInfo.ftCreationTime       =       FindFileData.ftCreationTime;
    StreamFileInfo.ftLastAccessTime     =       FindFileData.ftLastAccessTime;
    StreamFileInfo.ftLastWriteTime      =       FindFileData.ftLastWriteTime;
    StreamFileInfo.nFileSizeHigh        =       FindFileData.nFileSizeHigh;
    StreamFileInfo.nFileSizeLow         =       FindFileData.nFileSizeLow;

	CListenSock.Send(&StreamFileInfo,sizeof(SOCKET_STREAM_FILE_INFO));

	UINT dwRead=0;
	byte* data = new byte[1024];
	while(dwRead<StreamFileInfo.nFileSizeLow)
	{		
		UINT dw=myFile.Read(data, 1024);
		CListenSock.Send(data, dw);
		dwRead+=dw;
	}
	myFile.Close();

	delete data;
	CListenSock.Close();
	AfxMessageBox("发送完毕!");

// 	CFile sFile;
//	sFile.Open(m_FileName,CFile::modeRead);
/* 
 	CListenSock.Send(m_FileName,m_FileName.GetLength());///send filename
 	int FileLen=sFile.GetLength();
 	CListenSock.Send(&FileLen,4);
 
 	byte *data=new byte[FileLen];
 	if(!data)
 	{
 		AfxMessageBox("No enough memory ");
 		return;
 	}
 
 	sFile.Read(data,FileLen);
 	CListenSock.Send(data,FileLen);
 	sFile.Close();
 	delete data;
 	CListenSock.Close();
*/
}

⌨️ 快捷键说明

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