?? asdlg.cpp
字號:
// ASDlg.cpp : 實(shí)現(xiàn)文件
//
#include "stdafx.h"
#include "AS.h"
#include "ASDlg.h"
#include "Winsock2.h"
#include "QueryDel.h"
#pragma comment(lib,"Ws2_32.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define WM_MY_UPDATEDATA (WM_USER + 110)
void CString2Char(char* lpStr,int nLen,CString s);
void Encrypt(char* strPlainText,char* strKey);
void KeyGenerator(char* strKey);
void Decrypt(char* strCypherText,char* strKey);
void GetKeyS(const char* szLeft,char* szKeyS);
void GetSenderName(const char* szLeft,char* szSenderName);
// 用于應(yīng)用程序“關(guān)于”菜單項(xiàng)的 CAboutDlg 對話框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 對話框數(shù)據(jù)
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 實(shí)現(xiàn)
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CASDlg 對話框
CASDlg::CASDlg(CWnd* pParent /*=NULL*/)
: CDialog(CASDlg::IDD, pParent)
, m_sContent(_T(""))
, m_sKeyTGS(_T("234567"))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CASDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_sContent);
DDX_Text(pDX, IDC_EDIT2, m_sKeyTGS);
}
BEGIN_MESSAGE_MAP(CASDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CASDlg::OnBnClickedButton1)
ON_EN_CHANGE(IDC_EDIT2, &CASDlg::OnEnChangeEdit2)
ON_BN_CLICKED(IDC_BUTTON2, &CASDlg::OnBnClickedButton2)
ON_MESSAGE(WM_MY_UPDATEDATA,&CASDlg::MyUpdatedata)
END_MESSAGE_MAP()
// CASDlg 消息處理程序
BOOL CASDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 將“關(guān)于...”菜單項(xiàng)添加到系統(tǒng)菜單中。
// IDM_ABOUTBOX 必須在系統(tǒng)命令范圍內(nèi)。
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);
}
}
// 設(shè)置此對話框的圖標(biāo)。當(dāng)應(yīng)用程序主窗口不是對話框時,框架將自動
// 執(zhí)行此操作
SetIcon(m_hIcon, TRUE); // 設(shè)置大圖標(biāo)
SetIcon(m_hIcon, FALSE); // 設(shè)置小圖標(biāo)
// TODO: 在此添加額外的初始化代碼
return TRUE; // 除非將焦點(diǎn)設(shè)置到控件,否則返回 TRUE
}
void CASDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向?qū)υ捒蛱砑幼钚』粹o,則需要下面的代碼
// 來繪制該圖標(biāo)。對于使用文檔/視圖模型的 MFC 應(yīng)用程序,
// 這將由框架自動完成。
void CASDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于繪制的設(shè)備上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使圖標(biāo)在工作矩形中居中
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;
// 繪制圖標(biāo)
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//當(dāng)用戶拖動最小化窗口時系統(tǒng)調(diào)用此函數(shù)取得光標(biāo)顯示。
//
HCURSOR CASDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
CASDlg* pDlg;
pDlg = (CASDlg*)lpParam;
BOOL bMatch;
CString strTemp;
char szTempBuf1[500];
char szTempBuf2[500];
//用于連接數(shù)據(jù)庫
HRESULT hr = S_OK;
_ConnectionPtr pConn = NULL;
_RecordsetPtr pRst = NULL;
TCHAR szExePath[400] = {'\0'};
PTCHAR pLast = NULL;
//連接字符串
CString strOpen;
strOpen = "DRIVER={Microsoft Access Driver (*.mdb)};UID=;PWD=;DBQ=Person.mdb";
//查詢語句
CString strQuery;
//初始化連接
CoInitialize(NULL);
hr = pConn.CreateInstance(__uuidof(Connection));
if(hr != S_OK)
{
AfxMessageBox(_T("打開Connection失敗"));
return 0;
}
hr = S_OK;
//設(shè)置exe文件所在路徑為當(dāng)前路徑
GetModuleFileName(NULL,szExePath,400);
if(pLast = _tcsstr(szExePath,_T("\\AS.exe")))
{
*pLast = '\0';
}
SetCurrentDirectory(szExePath);
//連接數(shù)據(jù)庫
try
{
pConn->Open((_bstr_t)strOpen,"","",adConnectUnspecified);
}
catch(_com_error e)
{
AfxMessageBox(_T("Opening Connection...")+e.Description());
return 0;
}
//加載套接字庫
WORD wVersionRequested;
WSADATA wsaData;
int nErr;
wVersionRequested = MAKEWORD(1,1);
nErr = WSAStartup(wVersionRequested,&wsaData);
if(nErr != 0)
{
AfxMessageBox(_T("加載套接字庫失敗!"));
return 0;
}
if(LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
{
WSACleanup();
return 0;
}
//創(chuàng)建用于監(jiān)聽的套接字
SOCKET sockSrv = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port = htons(8083);
//綁定套接字
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
//將套接字設(shè)為監(jiān)聽模式,準(zhǔn)備接受客戶請求
listen(sockSrv,5);
SOCKADDR_IN addrClient;
int nLen = sizeof(SOCKADDR);
SOCKET sockConn;
while(TRUE)
{
Sleep(10);
sockConn = accept(sockSrv,(SOCKADDR*)&addrClient,&nLen);
//發(fā)送數(shù)據(jù)
//wsprintfA(pDlg->szSendBuffer,"Welcom %s to server",inet_ntoa(addrClient.sin_addr));
//send(sockConn,pDlg->szSendBuffer,(int)strlen(pDlg->szSendBuffer)+1,0);
//接收數(shù)據(jù)
pDlg->szRecBuffer[0] = '\0';
recv(sockConn,pDlg->szRecBuffer,500,0);
if(pDlg->szRecBuffer[0])
{
pDlg->m_sContent += _T("\r\n");
CString sRec(pDlg->szRecBuffer);
pDlg->m_sContent += sRec;
if(sRec.Find(_T("p3"))!=-1 || sRec.Find(_T("P3"))!=-1)
{
sRec = sRec.Mid(3);
pDlg->sLeft = sRec.Left(sRec.Find(',',sRec.Find(',')+1));
//AfxMessageBox(pDlg->sLeft+_T("\r\n"));
sRec = sRec.Mid(sRec.Find(',',sRec.Find(',')+1)+1);
pDlg->sMid = sRec.Left(sRec.Find(','));
//AfxMessageBox(pDlg->sMid+_T("\r\n"));
pDlg->sRight = sRec.Mid(sRec.Find(',')+1);
//AfxMessageBox(pDlg->sRight+_T("\r\n"));
//查詢數(shù)據(jù)庫
strQuery.Format(_T("select * from User"));
pRst = pConn->Execute((_bstr_t)strQuery,NULL,adCmdText);
bMatch = FALSE;
while(!pRst->rsEOF)
{
strTemp = (CString)(pRst->GetCollect("Name"));
if(strTemp.Compare(pDlg->sMid) == 0)
{
bMatch = TRUE;
CString2Char(pDlg->szTargetName,50,strTemp);
strTemp = (CString)(pRst->GetCollect("Key"));
CString2Char(pDlg->szTargetKey,7,strTemp);
break;
}
pRst->MoveNext();
}
if(bMatch)
{
KeyGenerator(pDlg->szKeyAB);
szTempBuf1[0] = '\0';
szTempBuf2[0] = '\0';
CString2Char(szTempBuf2,500,pDlg->m_sKeyTGS);
CString2Char(szTempBuf1,500,pDlg->sLeft);
Decrypt(szTempBuf1,szTempBuf2);
GetKeyS(szTempBuf1,pDlg->szKeyS);
GetSenderName(szTempBuf1,pDlg->szSenderName);
wsprintfA(szTempBuf1,"%s,%s\0",pDlg->szTargetName,pDlg->szKeyAB);
Encrypt(szTempBuf1,pDlg->szKeyS);
wsprintfA(szTempBuf2,"%s,%s\0",pDlg->szSenderName,pDlg->szKeyAB);
Encrypt(szTempBuf2,pDlg->szTargetKey);
pDlg->szSendBuffer[0] = '\0';
wsprintfA(pDlg->szSendBuffer,"p4;%s,%s",szTempBuf1,szTempBuf2);
send(sockConn,pDlg->szSendBuffer,(int)strlen(pDlg->szSendBuffer)+1,0);
pDlg->m_sContent += _T("已通過驗(yàn)證");
}
}
PostMessage(pDlg->m_hWnd,WM_MY_UPDATEDATA,0,0);
}//循環(huán)結(jié)束
//關(guān)閉套接字
closesocket(sockConn);
}
closesocket(sockSrv);
WSACleanup();
//關(guān)閉數(shù)據(jù)庫
try
{
pConn->Close();
}
catch(_com_error e)
{
AfxMessageBox(_T("Closing Connection...")+e.Description());
return 0;
}
pConn.Release();
CoUninitialize();
return 0;
}
void CASDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知處理程序代碼
m_sContent += _T("服務(wù)器端已啟動");
UpdateData(FALSE);
CreateThread(NULL,0,ThreadProc,(LPVOID)this,0,NULL);
}
void Decrypt(char* strCypherText,char* strKey)
{
int i,nKey;
nKey = atoi(strKey);
for(i=0;i<(int)strlen(strCypherText);i++)
{
if(strCypherText[i]>='0' && strCypherText[i]<='9')
{//數(shù)字
if(strCypherText[i]-(nKey%26)%10<'0')
{
strCypherText[i] = strCypherText[i]-(nKey%26)%10+10;
}
else
{
strCypherText[i] = strCypherText[i]-(nKey%26)%10;
}
}
else if(strCypherText[i]>='A' && strCypherText[i]<='Z')
{//大寫字母
if(strCypherText[i]-nKey%26<'A')
{
strCypherText[i] = strCypherText[i]-nKey%26+26;
}
else
{
strCypherText[i] = strCypherText[i]-nKey%26;
}
}
else if(strCypherText[i]>='a' && strCypherText[i]<='z')
{//小寫字母
if(strCypherText[i]-nKey%26<'a')
{
strCypherText[i] = strCypherText[i]-nKey%26+26;
}
else
{
strCypherText[i] = strCypherText[i]-nKey%26;
}
}
}
return;
}
void Encrypt(char* strPlainText,char* strKey)
{
int i,nKey;
nKey = atoi(strKey);
for(i=0;i<(int)strlen(strPlainText);i++)
{
if(strPlainText[i]>='0' && strPlainText[i]<='9')
{//數(shù)字
if(strPlainText[i]+(nKey%26)%10>'9')
{
strPlainText[i] = strPlainText[i]+(nKey%26)%10-10;
}
else
{
strPlainText[i] = strPlainText[i]+(nKey%26)%10;
}
}
else if(strPlainText[i]>='A' && strPlainText[i]<='Z')
{//大寫字母
if(strPlainText[i]+nKey%26>'Z')
{
strPlainText[i] = strPlainText[i]+nKey%26-26;
}
else
{
strPlainText[i] = strPlainText[i]+nKey%26;
}
}
else if(strPlainText[i]>='a' && strPlainText[i]<='z')
{//小寫字母
if(strPlainText[i]+nKey%26>'z')
{
strPlainText[i] = strPlainText[i]+nKey%26-26;
}
else
{
strPlainText[i] = strPlainText[i]+nKey%26;
}
}
}
return;
}
void KeyGenerator(char* strKey)
{
SYSTEMTIME SysTime;
int i;
GetLocalTime(&SysTime);
srand((int)SysTime.wMilliseconds);
for(i=0;i<6;i++)
{
itoa(rand()%10,strKey+i,10);
}
strKey[i] = '\0';
return;
}
void CASDlg::OnEnChangeEdit2()
{
// TODO: 如果該控件是 RICHEDIT 控件,則它將不會
// 發(fā)送該通知,除非重寫 CDialog::OnInitDialog()
// 函數(shù)并調(diào)用 CRichEditCtrl().SetEventMask(),
// 同時將 ENM_CHANGE 標(biāo)志“或”運(yùn)算到掩碼中。
// TODO: 在此添加控件通知處理程序代碼
int i;
UpdateData(TRUE);
if(m_sKeyTGS.GetLength() > 6)
{
AfxMessageBox(_T("密鑰位數(shù)只能是6位"));
m_sKeyTGS = "";
UpdateData(FALSE);
}
else if(m_sKeyTGS.GetLength() == 6)
{
for(i=0;i<m_sKeyTGS.GetLength();i++)
{
if(m_sKeyTGS.GetAt(i) != '0' && m_sKeyTGS.GetAt(i) != '1' && m_sKeyTGS.GetAt(i) != '2' && m_sKeyTGS.GetAt(i) != '3' && m_sKeyTGS.GetAt(i) != '4' && m_sKeyTGS.GetAt(i) != '5' && m_sKeyTGS.GetAt(i) != '6' && m_sKeyTGS.GetAt(i) != '7' && m_sKeyTGS.GetAt(i) != '8' && m_sKeyTGS.GetAt(i) != '9')
{
AfxMessageBox(_T("密鑰位數(shù)只能由數(shù)字構(gòu)成"));
m_sKeyTGS = "";
UpdateData(FALSE);
}
}
}
}
void CASDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知處理程序代碼
CQueryDel Dlg;
Dlg.DoModal();
}
LRESULT CASDlg::MyUpdatedata(WPARAM wParam, LPARAM lParam)
{
UpdateData(FALSE);
return S_OK;
}
void CString2Char(char* lpStr,int nLen,CString s)
{
int i;
for(i=0;i<s.GetLength()&&i<nLen-1;i++)
{
lpStr[i] = s.GetAt(i);
}
lpStr[i] = '\0';
return;
}
void GetKeyS(const char* szLeft,char* szKeyS)
{
int i,j;
BOOL bStart = FALSE;
j=0;
for(i=0;i<(int)strlen(szLeft);i++)
{
if(bStart)
{
szKeyS[j] = szLeft[i];
j++;
}
if(szLeft[i] == ',')
{
bStart = TRUE;
}
}
szKeyS[j] = '\0';
}
void GetSenderName(const char* szLeft,char* szSenderName)
{
int i;
for(i=0;i<(int)strlen(szLeft);i++)
{
if(szLeft[i] == ',')
{
break;
}
szSenderName[i] = szLeft[i];
}
szSenderName[i] = '\0';
return;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -