LYFZIPReceiveApp.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /****************************************************************/
  2. /* */
  3. /* LYFZIPReceiveApp.cpp */
  4. /* */
  5. /* Defines the class behaviors for the application. */
  6. /* */
  7. /* Programmed by LYFZ van der Meer */
  8. /* Copyright LYFZ Software Solutions 2002 */
  9. /* http://www.LYFZvandermeer.nl */
  10. /* */
  11. /* Last updated: 10 july 2002 */
  12. /* */
  13. /****************************************************************/
  14. #include "stdafx.h"
  15. #include "LYFZIPReceiveApp.h"
  16. #include "ApplicationDlg.h"
  17. #include "LYFZIPReceive.h"
  18. #include "MyLock.h"
  19. #include <wincrypt.h>
  20. #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
  21. #define KEYLENGTH 0x00800000
  22. #define ENCRYPT_ALGORITHM CALG_RC4
  23. #define ENCRYPT_BLOCK_SIZE 8
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29. CDatabase *g_curdb=NULL;
  30. CDatabase g_db;
  31. CString g_mainpath;
  32. SENDHEAD g_sendhead;
  33. SoftKey ytSoftKey;
  34. BOOL g_bDogOK=0;
  35. BEGIN_MESSAGE_MAP(CLYFZIPReceiveApp, CWinApp)
  36. //{{AFX_MSG_MAP(CLYFZIPReceiveApp)
  37. ON_COMMAND(ID_APP_SHOW, OnAppShow)
  38. ON_COMMAND(ID_APP_EXIT, OnAppExit)
  39. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  40. ON_COMMAND(ID_HELP_INDEX, OnHelpIndex)
  41. //}}AFX_MSG_MAP
  42. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CLYFZIPReceiveApp construction
  46. CLYFZIPReceiveApp::CLYFZIPReceiveApp()
  47. {
  48. // TODO: add construction code here,
  49. // Place all significant initialization in InitInstance
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // The one and only CLYFZIPReceiveApp object
  53. CLYFZIPReceiveApp theApp;
  54. CLYFZIPReceive theServer;
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CLYFZIPReceiveApp initialization
  57. int g_port=8387;
  58. BOOL CLYFZIPReceiveApp::InitInstance()
  59. {
  60. SetRegistryKey(_T("LYFZ-MSGSend"));
  61. CString version = _T ("Version 1.0");
  62. if(GetProfileInt (version, "debug", 0))
  63. return 0;
  64. if(m_lpCmdLine[0] == _T('\0')) //没有命令行参数
  65. {
  66. return false;
  67. }
  68. else
  69. {
  70. g_port=8387+atoi(m_lpCmdLine);
  71. }
  72. char path[MAX_PATH];
  73. ::GetModuleFileName (NULL, path, MAX_PATH);
  74. g_mainpath=path;
  75. g_mainpath=g_mainpath.Left (g_mainpath.ReverseFind ('\\'));
  76. BOOL bMasterDb=0;
  77. BOOL bUserDb=0;
  78. char server[50];
  79. DWORD leng=50;
  80. memset(server, 0, 50);
  81. ::GetComputerName (server, &leng);
  82. CString localname=server;
  83. CDatabase g_masterdb;
  84. ll: try
  85. {
  86. CString strCon;
  87. strCon.Format(_T("driver={SQL Server};Server=%s;DATABASE=master;UID=sa;PWD=ly1234;"), "192.168.1.223,1011");
  88. g_masterdb.OpenEx(strCon,CDatabase::noOdbcDialog);
  89. bMasterDb=1;
  90. }
  91. catch(CDBException * e)
  92. {
  93. }
  94. if(bMasterDb==0)
  95. {
  96. WriteLogin("sql数据库未启动!");
  97. ::Sleep (1000);
  98. goto ll;
  99. }
  100. try
  101. {
  102. CString strCon;
  103. strCon.Format(_T("driver={SQL Server};Server=%s;DATABASE=dbmsg;UID=sa;PWD=ly1234;"), "192.168.1.223,1011");
  104. g_db.OpenEx(strCon,CDatabase::noOdbcDialog);
  105. bUserDb=1;
  106. g_curdb=&g_db;
  107. g_masterdb.Close();
  108. }
  109. catch(CDBException * e)
  110. {
  111. WriteLogin("数据库连接失败-"+e->m_strError);
  112. e->Delete();
  113. ::Sleep (1000);
  114. goto ll;
  115. }
  116. SetRegistryKey(_T("LYFZ-LYFZIPReceive"));
  117. // Standard initialization
  118. // If you are not using these features and wish to reduce the size
  119. // of your final executable, you should remove from the following
  120. // the specific initialization routines you do not need.
  121. #ifdef _AFXDLL
  122. Enable3dControls(); // Call this when using MFC in a shared DLL
  123. #else
  124. Enable3dControlsStatic(); // Call this when linking to MFC statically
  125. #endif
  126. InitCommonControls();
  127. // Create the tray icon
  128. if (!m_TrayIcon.Create(WM_ICON_NOTIFY, // Icon notify message to use
  129. _T("利亚方舟域名接收"), // tooltip
  130. LoadIcon(IDI_LYFZIPReceive), // Icon
  131. IDR_POPUP_MENU)) // ID of tray icon
  132. return -1;
  133. int nLogLevel = GetProfileInt("Settings", "LogLevel" , 1);
  134. CApplicationDlg dlg;
  135. m_pMainWnd = &dlg;
  136. int nResponse = dlg.DoModal();
  137. if (nResponse == IDOK)
  138. {
  139. // TODO: Place code here to handle when the dialog is
  140. // dismissed with OK
  141. }
  142. else if (nResponse == IDCANCEL)
  143. {
  144. // TODO: Place code here to handle when the dialog is
  145. // dismissed with Cancel
  146. }
  147. // Since the dialog has been closed, return FALSE so that we exit the
  148. // application, rather than start the application's message pump.
  149. return FALSE;
  150. }
  151. void CLYFZIPReceiveApp::OnAppShow()
  152. {
  153. if (m_pMainWnd)
  154. m_pMainWnd->ShowWindow(SW_SHOW);
  155. }
  156. void CLYFZIPReceiveApp::OnAppExit()
  157. {
  158. m_pMainWnd->DestroyWindow();
  159. }
  160. void CLYFZIPReceiveApp::OnAppAbout()
  161. {
  162. // CSettingsSheet propsheet("Settings", AfxGetApp()->m_pMainWnd, 2);
  163. // remove Help button
  164. // propsheet.m_psh.dwFlags &= ~(PSH_HASHELP);
  165. // display Settings dialogbox
  166. // propsheet.DoModal();
  167. }
  168. /********************************************************************/
  169. /* */
  170. /* Function name : OnHelpIndex */
  171. /* Description : Command to show help file. */
  172. /* */
  173. /********************************************************************/
  174. void CLYFZIPReceiveApp::OnHelpIndex()
  175. {
  176. // launch help
  177. // ::WinHelp(AfxGetMainWnd()->m_hWnd, AfxGetApp()->m_pszHelpFilePath, HELP_CONTENTS, 0L);
  178. }
  179. BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
  180. {
  181. try
  182. {
  183. HCRYPTPROV hCryptProv;
  184. HCRYPTKEY hKey;
  185. HCRYPTHASH hHash;
  186. PBYTE pbBuffer;
  187. DWORD dwBlockLen;
  188. DWORD dwBufferLen;
  189. DWORD dwCount;
  190. //以下获得一个CSP句柄
  191. if(CryptAcquireContext(
  192. &hCryptProv,
  193. NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
  194. NULL,
  195. PROV_RSA_FULL,
  196. 0))
  197. {
  198. printf("A cryptographic provider has been acquired. \n");
  199. }
  200. else//密钥容器不存在
  201. {
  202. if(CryptAcquireContext(
  203. &hCryptProv,
  204. NULL,
  205. NULL,
  206. PROV_RSA_FULL,
  207. CRYPT_NEWKEYSET))//创建密钥容器
  208. {
  209. //创建密钥容器成功,并得到CSP句柄
  210. printf("A new key container has been created.\n");
  211. }
  212. else
  213. {
  214. return 0;
  215. }
  216. }
  217. //--------------------------------------------------------------------
  218. // 创建一个会话密钥(session key)
  219. // 会话密钥也叫对称密钥,用于对称加密算法。
  220. // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
  221. // CryptReleaseContext 期间的阶段。)
  222. //--------------------------------------------------------------------
  223. // Create a hash object.
  224. if(CryptCreateHash(
  225. hCryptProv,
  226. CALG_MD5,
  227. 0,
  228. 0,
  229. &hHash))
  230. {
  231. printf("A hash object has been created. \n");
  232. }
  233. else
  234. {
  235. return 0;
  236. }
  237. //--------------------------------------------------------------------
  238. // 用输入的密码产生一个散列
  239. if(CryptHashData(
  240. hHash,
  241. (BYTE *)szPassword,
  242. strlen(szPassword),
  243. 0))
  244. {
  245. printf("The password has been added to the hash. \n");
  246. }
  247. else
  248. {
  249. return 0;
  250. }
  251. //--------------------------------------------------------------------
  252. // 通过散列生成会话密钥(session key)
  253. if(CryptDeriveKey(
  254. hCryptProv,
  255. ENCRYPT_ALGORITHM,
  256. hHash,
  257. KEYLENGTH,
  258. &hKey))
  259. {
  260. printf("An encryption key is derived from the password hash. \n");
  261. }
  262. else
  263. {
  264. return 0;
  265. }
  266. //--------------------------------------------------------------------
  267. // Destroy the hash object.
  268. CryptDestroyHash(hHash);
  269. hHash = NULL;
  270. //--------------------------------------------------------------------
  271. // The session key is now ready.
  272. //--------------------------------------------------------------------
  273. // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
  274. // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
  275. // 数据长度。
  276. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
  277. //--------------------------------------------------------------------
  278. // Determine the block size. If a block cipher is used,
  279. // it must have room for an extra block.
  280. if(ENCRYPT_BLOCK_SIZE > 1)
  281. dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
  282. else
  283. dwBufferLen = dwBlockLen;
  284. dwCount=dwBufferLen;
  285. //--------------------------------------------------------------------
  286. // In a do loop, encrypt the source file and write to the source file.
  287. int count;
  288. // if(leng%dwBlockLen==0)
  289. count=leng/dwBufferLen;
  290. // else
  291. // count=leng/dwBlockLen+1;
  292. for(int i=0; i<count; i++)
  293. {
  294. pbBuffer=buffer+i*dwBufferLen;
  295. // 加密数据
  296. if(!CryptEncrypt(
  297. hKey, //密钥
  298. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  299. 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  300. //这里通过判断是否到文件尾来决定是否为最后一块。
  301. 0, //保留
  302. pbBuffer, //输入被加密数据,输出加密后的数据
  303. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  304. dwBufferLen)) //pbBuffer的大小。
  305. {
  306. return 0;
  307. }
  308. }
  309. if(leng%dwBlockLen)
  310. {
  311. pbBuffer=buffer+i*dwBufferLen;
  312. dwCount=leng-i*dwBufferLen;
  313. if(!CryptEncrypt(
  314. hKey, //密钥
  315. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  316. TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  317. //这里通过判断是否到文件尾来决定是否为最后一块。
  318. 0, //保留
  319. pbBuffer, //输入被加密数据,输出加密后的数据
  320. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  321. dwBufferLen)) //pbBuffer的大小。
  322. {
  323. return 0;
  324. }
  325. }
  326. //--------------------------------------------------------------------
  327. // Destroy session key.
  328. if(hKey)
  329. CryptDestroyKey(hKey);
  330. //--------------------------------------------------------------------
  331. // Destroy hash object.
  332. if(hHash)
  333. CryptDestroyHash(hHash);
  334. //--------------------------------------------------------------------
  335. // Release provider handle.
  336. if(hCryptProv)
  337. CryptReleaseContext(hCryptProv, 0);
  338. return(TRUE);
  339. }
  340. catch(...)
  341. {
  342. }
  343. }
  344. void WriteLogin(CString str)
  345. {
  346. MyLock lock("LYFZIPReceiveWriteLogin");
  347. CStdioFile fp;
  348. CString path=g_mainpath+"\\LYFZIPReceive.txt";
  349. if(::PathFileExists (path))
  350. {
  351. fp.Open (path, CFile::modeWrite);
  352. int length=fp.GetLength ();
  353. if(length>1024*1024)
  354. {
  355. fp.Close ();
  356. ::DeleteFile (path);
  357. return;
  358. }
  359. fp.SeekToEnd ();
  360. }
  361. else
  362. fp.Open (path, CFile::modeCreate|CFile::modeWrite);
  363. fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S")+str+"\r\n" );
  364. fp.Close ();
  365. }
  366. void WriteLogin2(CString str)
  367. {
  368. MyLock lock("LYFZIPReceiveWriteLogin");
  369. CStdioFile fp;
  370. CString path=g_mainpath+"\\LYFZIPReceive.txt";
  371. if(::PathFileExists (path))
  372. {
  373. fp.Open (path, CFile::modeWrite);
  374. int length=fp.GetLength ();
  375. if(length>1024*1024)
  376. {
  377. fp.Close ();
  378. ::DeleteFile (path);
  379. return;
  380. }
  381. fp.SeekToEnd ();
  382. }
  383. else
  384. fp.Open (path, CFile::modeCreate|CFile::modeWrite);
  385. fp.WriteString (CTime::GetCurrentTime ().Format ("%Y-%m-%d %H:%M:%S")+str+"\r\n" );
  386. fp.Close ();
  387. }