/****************************************************************/ /* */ /* LYFZReceiveMsgApp.cpp */ /* */ /* Defines the class behaviors for the application. */ /* */ /* Programmed by LYFZ van der Meer */ /* Copyright LYFZ Software Solutions 2002 */ /* http://www.LYFZvandermeer.nl */ /* */ /* Last updated: 10 july 2002 */ /* */ /****************************************************************/ #include "stdafx.h" #include "LYFZReceiveMsgApp.h" #include "ApplicationDlg.h" #include "LYFZReceiveMsg.h" #include "MyLock.h" #include #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING) #define KEYLENGTH 0x00800000 #define ENCRYPT_ALGORITHM CALG_RC4 #define ENCRYPT_BLOCK_SIZE 8 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CDatabase *g_curdb=NULL; CDatabase g_db; CString g_mainpath; SENDHEAD g_sendhead; CString g_localname; BEGIN_MESSAGE_MAP(CLYFZReceiveMsgApp, CWinApp) //{{AFX_MSG_MAP(CLYFZReceiveMsgApp) ON_COMMAND(ID_APP_SHOW, OnAppShow) ON_COMMAND(ID_APP_EXIT, OnAppExit) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) ON_COMMAND(ID_HELP_INDEX, OnHelpIndex) //}}AFX_MSG_MAP ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CLYFZReceiveMsgApp construction CLYFZReceiveMsgApp::CLYFZReceiveMsgApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CLYFZReceiveMsgApp object CLYFZReceiveMsgApp theApp; CLYFZReceiveMsg theServer; ///////////////////////////////////////////////////////////////////////////// // CLYFZReceiveMsgApp initialization int g_port=8380; extern void Test(); BOOL CLYFZReceiveMsgApp::InitInstance() { SetRegistryKey(_T("LYFZ-MSGSend")); CString version = _T ("Version 1.0"); if(GetProfileInt (version, "debug", 0)) return 0; if(m_lpCmdLine[0] == _T('\0')) //没有命令行参数 { return false; } else { g_port=8380+atoi(m_lpCmdLine); } #if 1 // 固定端口号,且单例运行; HANDLE hObject = CreateMutex(NULL,FALSE,_T("lyfzReceiveMsg")); if(GetLastError() == ERROR_ALREADY_EXISTS) { return false; } g_port=8381; #endif char path[MAX_PATH]; ::GetModuleFileName (NULL, path, MAX_PATH); g_mainpath=path; g_mainpath=g_mainpath.Left (g_mainpath.ReverseFind ('\\')); BOOL bMasterDb=0; BOOL bUserDb=0; char server[50]; DWORD leng=50; memset(server, 0, 50); ::GetComputerName (server, &leng); g_localname=server; Global::GetIniInfo(); CDatabase g_masterdb; ll: try { CString strCon; if (Global::g_dwDBServerPort != 0) { if ( _tcscmp(Global::g_szDBAccount, _T("")) == 0 ) strCon.Format(DB_SW_CONN_WITH_PORT, Global::g_szDBSource, Global::g_dwDBServerPort,_T("master")); else strCon.Format(DB_SS_CONN_WITH_PORT,Global::g_szDBSource, Global::g_dwDBServerPort, _T("master"), Global::g_szDBAccount, Global::g_szDBPassWord); } else { if ( _tcscmp(Global::g_szDBAccount, _T("")) == 0 ) strCon.Format(DB_SW_CONN_WITHOUT_PORT, Global::g_szDBSource, _T("master")); else strCon.Format(DB_SS_CONN_WITHOUT_PORT,Global::g_szDBSource, _T("master"), Global::g_szDBAccount, Global::g_szDBPassWord); } g_masterdb.OpenEx(strCon,CDatabase::noOdbcDialog); bMasterDb=1; } catch(CDBException * e) { } if(bMasterDb==0) { WriteLogin("sql数据库未启动!"); ::Sleep (1000); goto ll; } try { g_db.OpenEx(Global::g_szConnectString,CDatabase::noOdbcDialog); bUserDb=1; g_curdb=&g_db; g_masterdb.Close(); } catch(CDBException * e) { WriteLogin("数据库连接失败-"+e->m_strError); e->Delete(); ::Sleep (1000); goto ll; } SetRegistryKey(_T("LYFZ-LYFZReceiveMsg")); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif InitCommonControls(); // Create the tray icon if (!m_TrayIcon.Create(WM_ICON_NOTIFY, // Icon notify message to use _T("利亚方舟-短信接收"), // tooltip LoadIcon(IDI_LYFZReceiveMsg), // Icon IDR_POPUP_MENU)) // ID of tray icon return -1; int nLogLevel = GetProfileInt("Settings", "LogLevel" , 1); CApplicationDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; } void CLYFZReceiveMsgApp::OnAppShow() { if (m_pMainWnd) m_pMainWnd->ShowWindow(SW_SHOW); } void CLYFZReceiveMsgApp::OnAppExit() { m_pMainWnd->DestroyWindow(); } void CLYFZReceiveMsgApp::OnAppAbout() { // CSettingsSheet propsheet("Settings", AfxGetApp()->m_pMainWnd, 2); // remove Help button // propsheet.m_psh.dwFlags &= ~(PSH_HASHELP); // display Settings dialogbox // propsheet.DoModal(); } /********************************************************************/ /* */ /* Function name : OnHelpIndex */ /* Description : Command to show help file. */ /* */ /********************************************************************/ void CLYFZReceiveMsgApp::OnHelpIndex() { // launch help // ::WinHelp(AfxGetMainWnd()->m_hWnd, AfxGetApp()->m_pszHelpFilePath, HELP_CONTENTS, 0L); } BOOL AddConn(CDatabase *m_conndb) { try { m_conndb->OpenEx(Global::g_szConnectString,CDatabase::noOdbcDialog); } catch(CDBException * e) { return 0; } return 1; } BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword) { try { HCRYPTPROV hCryptProv; HCRYPTKEY hKey; HCRYPTHASH hHash; PBYTE pbBuffer; DWORD dwBlockLen; DWORD dwBufferLen; DWORD dwCount; //以下获得一个CSP句柄 if(CryptAcquireContext( &hCryptProv, NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名 NULL, PROV_RSA_FULL, 0)) { printf("A cryptographic provider has been acquired. \n"); } else//密钥容器不存在 { if(CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))//创建密钥容器 { //创建密钥容器成功,并得到CSP句柄 printf("A new key container has been created.\n"); } else { return 0; } } //-------------------------------------------------------------------- // 创建一个会话密钥(session key) // 会话密钥也叫对称密钥,用于对称加密算法。 // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数 // CryptReleaseContext 期间的阶段。) //-------------------------------------------------------------------- // Create a hash object. if(CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash)) { printf("A hash object has been created. \n"); } else { return 0; } //-------------------------------------------------------------------- // 用输入的密码产生一个散列 if(CryptHashData( hHash, (BYTE *)szPassword, strlen(szPassword), 0)) { printf("The password has been added to the hash. \n"); } else { return 0; } //-------------------------------------------------------------------- // 通过散列生成会话密钥(session key) if(CryptDeriveKey( hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey)) { printf("An encryption key is derived from the password hash. \n"); } else { return 0; } //-------------------------------------------------------------------- // Destroy the hash object. CryptDestroyHash(hHash); hHash = NULL; //-------------------------------------------------------------------- // The session key is now ready. //-------------------------------------------------------------------- // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的 // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的 // 数据长度。 dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE; //-------------------------------------------------------------------- // Determine the block size. If a block cipher is used, // it must have room for an extra block. if(ENCRYPT_BLOCK_SIZE > 1) dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE; else dwBufferLen = dwBlockLen; dwCount=dwBufferLen; //-------------------------------------------------------------------- // In a do loop, encrypt the source file and write to the source file. int count; // if(leng%dwBlockLen==0) count=leng/dwBufferLen; // else // count=leng/dwBlockLen+1; int i ; for( i=0; i1024*1024) { fp.Close (); ::DeleteFile (path); return; } fp.SeekToEnd (); } else fp.Open (path, CFile::modeCreate|CFile::modeWrite); fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" ); fp.Close (); } void WriteLogin2(CString str) { MyLock lock("LYFZReceiveMsgWriteLogin"); CStdioFile fp; CString path=g_mainpath+"\\LYFZReceiveMsg.txt"; if(::PathFileExists (path)) { fp.Open (path, CFile::modeWrite); int length=fp.GetLength (); if(length>1024*1024) { fp.Close (); ::DeleteFile (path); return; } fp.SeekToEnd (); } else fp.Open (path, CFile::modeCreate|CFile::modeWrite); fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" ); fp.Close (); } void WriteError(CString str) { MyLock lock("LYFZReceiveMsgWriteError"); CStdioFile fp; CString path=g_mainpath+"\\MsgError.txt"; if(::PathFileExists (path)) { fp.Open (path, CFile::modeWrite); int length=fp.GetLength (); if(length>1024*1024) { fp.Close (); ::DeleteFile (path); return; } fp.SeekToEnd (); } else fp.Open (path, CFile::modeCreate|CFile::modeWrite); fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" ); fp.Close (); } void WritePswWrong(CString str) { MyLock lock("LYFZReceiveMsgWritePswWrong"); CStdioFile fp; CString path=g_mainpath+"\\PswWrong.txt"; if(::PathFileExists (path)) { fp.Open (path, CFile::modeWrite); int length=fp.GetLength (); if(length>1024*1024) { fp.Close (); ::DeleteFile (path); return; } fp.SeekToEnd (); } else fp.Open (path, CFile::modeCreate|CFile::modeWrite); fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" ); fp.Close (); } void WriteNullBalance(CString str) { MyLock lock("LYFZReceiveMsgWriteNullBalance"); CStdioFile fp; CString path=g_mainpath+"\\NullBalance.txt"; if(::PathFileExists (path)) { fp.Open (path, CFile::modeWrite); int length=fp.GetLength (); if(length>1024*1024) { fp.Close (); ::DeleteFile (path); return; } fp.SeekToEnd (); } else fp.Open (path, CFile::modeCreate|CFile::modeWrite); fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" ); fp.Close (); } void WriteUnallowIP(CString str) { CStdioFile fp; CString path=g_mainpath+"\\UnallowIP.txt"; if(::PathFileExists (path)) { fp.Open (path, CFile::modeWrite); int length=fp.GetLength (); if(length>1024*1024) { fp.Close (); ::DeleteFile (path); return; } fp.SeekToEnd (); } else fp.Open (path, CFile::modeCreate|CFile::modeWrite); fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S ")+str+"\n" ); fp.Close (); }