/****************************************************************/ /* */ /* LYFZIPReceiveApp.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 "LYFZIPReceiveApp.h" #include "ApplicationDlg.h" #include "LYFZIPReceive.h" #include "MyLock.h" #include <wincrypt.h> #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; SoftKey ytSoftKey; BOOL g_bDogOK=0; BEGIN_MESSAGE_MAP(CLYFZIPReceiveApp, CWinApp) //{{AFX_MSG_MAP(CLYFZIPReceiveApp) 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() ///////////////////////////////////////////////////////////////////////////// // CLYFZIPReceiveApp construction CLYFZIPReceiveApp::CLYFZIPReceiveApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CLYFZIPReceiveApp object CLYFZIPReceiveApp theApp; CLYFZIPReceive theServer; ///////////////////////////////////////////////////////////////////////////// // CLYFZIPReceiveApp initialization int g_port=8387; BOOL CLYFZIPReceiveApp::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=8387+atoi(m_lpCmdLine); } 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); CString localname=server; CDatabase g_masterdb; ll: try { CString strCon; strCon.Format(_T("driver={SQL Server};Server=%s;DATABASE=master;UID=sa;PWD=ly1234;"), "192.168.1.223,1011"); g_masterdb.OpenEx(strCon,CDatabase::noOdbcDialog); bMasterDb=1; } catch(CDBException * e) { } if(bMasterDb==0) { WriteLogin("sql���ݿ�δ����!"); ::Sleep (1000); goto ll; } try { CString strCon; strCon.Format(_T("driver={SQL Server};Server=%s;DATABASE=dbmsg;UID=sa;PWD=ly1234;"), "192.168.1.223,1011"); g_db.OpenEx(strCon,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-LYFZIPReceive")); // 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_LYFZIPReceive), // 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 CLYFZIPReceiveApp::OnAppShow() { if (m_pMainWnd) m_pMainWnd->ShowWindow(SW_SHOW); } void CLYFZIPReceiveApp::OnAppExit() { m_pMainWnd->DestroyWindow(); } void CLYFZIPReceiveApp::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 CLYFZIPReceiveApp::OnHelpIndex() { // launch help // ::WinHelp(AfxGetMainWnd()->m_hWnd, AfxGetApp()->m_pszHelpFilePath, HELP_CONTENTS, 0L); } 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; for(int i=0; i<count; i++) { pbBuffer=buffer+i*dwBufferLen; // �������� if(!CryptEncrypt( hKey, //��Կ 0, //�������ͬʱ����ɢ�кͼ��ܣ����ﴫ��һ��ɢ�ж��� 0, //��������һ�������ܵĿ飬����TRUE.�����������FALSE. //����ͨ���ж��Ƿ��ļ�β�������Ƿ�Ϊ���һ�顣 0, //���� pbBuffer, //���뱻�������ݣ�������ܺ������ &dwCount, //���뱻��������ʵ�ʳ��ȣ�������ܺ����ݳ��� dwBufferLen)) //pbBuffer�Ĵ�С�� { return 0; } } if(leng%dwBlockLen) { pbBuffer=buffer+i*dwBufferLen; dwCount=leng-i*dwBufferLen; if(!CryptEncrypt( hKey, //��Կ 0, //�������ͬʱ����ɢ�кͼ��ܣ����ﴫ��һ��ɢ�ж��� TRUE, //��������һ�������ܵĿ飬����TRUE.�����������FALSE. //����ͨ���ж��Ƿ��ļ�β�������Ƿ�Ϊ���һ�顣 0, //���� pbBuffer, //���뱻�������ݣ�������ܺ������ &dwCount, //���뱻��������ʵ�ʳ��ȣ�������ܺ����ݳ��� dwBufferLen)) //pbBuffer�Ĵ�С�� { return 0; } } //-------------------------------------------------------------------- // Destroy session key. if(hKey) CryptDestroyKey(hKey); //-------------------------------------------------------------------- // Destroy hash object. if(hHash) CryptDestroyHash(hHash); //-------------------------------------------------------------------- // Release provider handle. if(hCryptProv) CryptReleaseContext(hCryptProv, 0); return(TRUE); } catch(...) { } } void WriteLogin(CString str) { MyLock lock("LYFZIPReceiveWriteLogin"); CStdioFile fp; CString path=g_mainpath+"\\LYFZIPReceive.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+"\r\n" ); fp.Close (); } void WriteLogin2(CString str) { MyLock lock("LYFZIPReceiveWriteLogin"); CStdioFile fp; CString path=g_mainpath+"\\LYFZIPReceive.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+"\r\n" ); fp.Close (); }